diff --git a/dd-java-agent/instrumentation/pekko/pekko-http-1.0/src/main/java/datadog/trace/instrumentation/pekkohttp/DatadogServerRequestResponseFlowWrapper.java b/dd-java-agent/instrumentation/pekko/pekko-http-1.0/src/main/java/datadog/trace/instrumentation/pekkohttp/DatadogServerRequestResponseFlowWrapper.java index 533a77e1ec4..242f95fe2d0 100644 --- a/dd-java-agent/instrumentation/pekko/pekko-http-1.0/src/main/java/datadog/trace/instrumentation/pekkohttp/DatadogServerRequestResponseFlowWrapper.java +++ b/dd-java-agent/instrumentation/pekko/pekko-http-1.0/src/main/java/datadog/trace/instrumentation/pekkohttp/DatadogServerRequestResponseFlowWrapper.java @@ -146,12 +146,19 @@ public void onUpstreamFailure(final Throwable ex) throws Exception { // Mark the span as failed AgentSpan span = fromContext(scope.context()); DatadogWrapperHelper.finishSpan(span, ex); + + // Clean up the leaked scope like in the normal response path + AgentSpan activeSpan = activeSpan(); + if (activeSpan == span) { + scope.close(); + } } // We will not receive any more responses from the user code, so clean up any // remaining spans scope = scopes.poll(); while (scope != null) { fromContext(scope.context()).finish(); + scope.close(); scope = scopes.poll(); } fail(responseOutlet, ex);