Repository navigation
Fix carrier starvation in scope diagnostics #13063
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
amarziali
merged 4 commits into
master
from
andrea.marziali/fix-scope-diagnostics-carrier-deadlock
Oct 27, 2026
+648
−108
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
...ation-testing/src/main/java/datadog/trace/agent/test/scopediag/DiagnosticEventBuffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package datadog.trace.agent.test.scopediag; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.locks.LockSupport; | ||
|
|
||
| /** | ||
| * Ordered callback reservations with a single consumer protected by the diagnostic lifecycle lock. | ||
| */ | ||
| final class DiagnosticEventBuffer { | ||
| private static final Runnable NOOP = () -> {}; | ||
| private static final Event CLOSED = new Event(); | ||
|
|
||
| private Event consumed = new Event(); | ||
| // A successful CAS reserves FIFO order before stack capture; the volatile next/action writes | ||
| // publish the link and payload separately. Only the consumer touches consumed and draining. | ||
| private final AtomicReference<Event> tail = new AtomicReference<>(consumed); | ||
|
|
||
| @SuppressFBWarnings( | ||
| value = "AT_STALE_THREAD_WRITE_OF_PRIMITIVE", | ||
| justification = "Only consumer methods access this field, while holding the lifecycle lock") | ||
| private boolean draining; | ||
|
|
||
| Event reserve() { | ||
| Event event = new Event(); | ||
| Event previous; | ||
| do { | ||
| previous = tail.get(); | ||
| if (previous == CLOSED) { | ||
| return null; | ||
| } | ||
| } while (!tail.compareAndSet(previous, event)); | ||
| previous.next = event; | ||
| return event; | ||
| } | ||
|
|
||
| /** Stops admission atomically; all successful reservations remain available to the consumer. */ | ||
| void close() { | ||
| Event previous = tail.getAndSet(CLOSED); | ||
| if (previous != CLOSED) { | ||
| previous.next = CLOSED; | ||
| } | ||
| } | ||
|
|
||
| boolean hasPending() { | ||
| Event last = tail.get(); | ||
| return consumed != last && consumed.next != CLOSED; | ||
| } | ||
|
|
||
| boolean isNext(Event event) { | ||
| return !draining && consumed.next == event; | ||
| } | ||
|
|
||
| /** Drains one finite prefix; opportunistic consumers never wait for a producer. */ | ||
| void drain(boolean timed, long deadline, boolean waitForPublication) { | ||
| if (draining) { | ||
| return; | ||
| } | ||
| draining = true; | ||
| try { | ||
| Event boundary = tail.get(); | ||
| while (consumed != boundary && (!timed || System.nanoTime() - deadline < 0)) { | ||
| Event event = consumed.next; | ||
| if (event == CLOSED) { | ||
| return; | ||
| } | ||
| Runnable action = event == null ? null : event.action; | ||
| if (action == null) { | ||
| if (!waitForPublication) { | ||
| return; | ||
| } | ||
| // Only control/report threads wait. Callback threads always use non-waiting drains and | ||
| // tryLock, so holding the lifecycle lock here cannot block a carrier callback. | ||
| LockSupport.parkNanos(this, 100_000); | ||
|
amarziali marked this conversation as resolved.
|
||
| continue; | ||
| } | ||
| consumed.next = null; | ||
| consumed = event; | ||
| event.action = NOOP; | ||
| action.run(); | ||
| } | ||
| } finally { | ||
| draining = false; | ||
| } | ||
| } | ||
|
|
||
| static final class Event { | ||
| private volatile Event next; | ||
| private volatile Runnable action; | ||
|
|
||
| void publish(Runnable action) { | ||
| this.action = action; | ||
| } | ||
|
|
||
| void publishIfAbsent() { | ||
| if (action == null) { | ||
| action = NOOP; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.