Skip to content

Cleanup Zio fiber instrumentation to avoid repeated activation of continuation #8798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
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 @@ -7,58 +7,34 @@
import datadog.trace.bootstrap.instrumentation.api.ScopeState;

public class FiberContext {
private final ScopeState state;
private AgentScope.Continuation continuation;
private AgentScope scope;
private ScopeState oldState;
private final ScopeState scopeState;
private final AgentScope.Continuation continuation;

private FiberContext(ScopeState state) {
this.state = state;
this.scope = null;
this.oldState = null;
this.continuation = captureActiveSpan();
}
private ScopeState oldScopeState;

public static FiberContext create() {
final ScopeState state = AgentTracer.get().newScopeState();
return new FiberContext(state);
public FiberContext() {
// copy scope stack to use for this fiber
this.scopeState = AgentTracer.get().oldScopeState().copy();
// stop enclosing trace from finishing early
this.continuation = captureActiveSpan();
}

public void onEnd() {
if (this.scope != null) {
this.scope.close();
this.scope = null;
}
if (continuation != null) {
continuation.cancel();
continuation = null;
}

if (this.oldState != null) {
this.oldState.activate();
this.oldState = null;
}
public void onResume() {
oldScopeState = AgentTracer.get().oldScopeState();
scopeState.activate(); // swap in the fiber's scope stack
}

public void onSuspend() {
if (this.scope != null && continuation != null) {
this.scope.close();
this.scope = null;
}
if (this.oldState != null) {
this.oldState.activate();
this.oldState = null;
if (oldScopeState != null) {
oldScopeState.activate(); // swap bock the original scope stack
oldScopeState = null;
}
}

public void onResume() {
this.oldState = AgentTracer.get().oldScopeState();

this.state.activate();

if (this.continuation != null) {
this.scope = continuation.activate();
continuation = null;
public void onEnd() {
if (continuation != null) {
// release enclosing trace now the fiber has ended
continuation.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public <R, E, A_> void onStart(
Option<Fiber.Runtime<Object, Object>> parent,
Fiber.Runtime<E, A_> fiber,
Unsafe unsafe) {
FiberContext context = FiberContext.create();
FiberContext context = new FiberContext();
contextStore.put(fiber, context);
}

Expand Down