Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TracingSendHandler(SendHandler delegate, HandlerContext handlerContext) {
@Override
public void onResult(SendResult sendResult) {
final AgentSpan wsSpan = handlerContext.getWebsocketSpan();
try (final ContextScope ignored = activateSpan(wsSpan)) {
try (final ContextScope ignored = wsSpan != null ? activateSpan(wsSpan) : null) {
delegate.onResult(sendResult);
} finally {
if (sendResult.getException() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ private AgentScope activate(
final byte source,
final boolean overrideAsyncPropagation,
final boolean isAsyncPropagating) {
if (span == null) {
log.debug(SEND_TELEMETRY, "Attempted to activate a null span. Returning NoopScope.");
return INVALID_SCOPE;
}

ScopeStack scopeStack = scopeStack();

final ContinuableScope top = scopeStack.top;
Expand All @@ -131,8 +136,6 @@ private AgentScope activate(
}
}

assert span != null;

// Inherit the async propagation from the active scope unless the value is overridden
boolean asyncPropagation =
overrideAsyncPropagation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.NoopScope;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.common.writer.ListWriter;
import datadog.trace.core.CoreTracer;
Expand Down Expand Up @@ -124,6 +125,27 @@ void nonDdspanActivationResultsInAContinuableScope() {
assertNull(scopeManager.active());
}

@Test
void activatingNullSpanReturnsNoopScopeAndDoesNotCorruptStack() {
AgentScope nullScope = scopeManager.activateSpan(null);

assertInstanceOf(NoopScope.class, nullScope);
assertNull(scopeManager.active());

nullScope.close();

// a subsequent activation on the same thread must not NPE, even though the noop
// activation above never pushed a scope with a null context onto the stack
AgentSpan span = tracer.buildSpan("test", "test").start();
AgentScope scope = tracer.activateSpan(span);

assertSame(scope, scopeManager.active());
assertSame(span, scope.span());

scope.close();
span.finish();
}

@Test
void noScopeIsActiveBeforeActivation() throws Exception {
tracer.buildSpan("test", "test").start();
Expand Down
Loading