Repository navigation
Fix Kotlin coroutines losing scopes when resumed on another thread #12864
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
Open
mcculls
wants to merge
5
commits into
master
Choose a base branch
from
mcculls/kotlin-shared-scope-stack
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6314451
Add shared scope stacks for coroutine thread handoffs
mcculls 33d9f9e
Preserve Kotlin coroutine scopes across suspension and resumption
mcculls 448ded4
Fix recursive context lookups when reusing scope stack tokens
mcculls ea1c93e
Preserve start-time parenting for lazy Kotlin coroutines
mcculls a80b481
Preserve active context when initializing Kotlin coroutine children
mcculls 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
...stdog/trace/instrumentation/java/lang/jdk21/VirtualThreadScopeClassLoadingForkedTest.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,53 @@ | ||
| package testdog.trace.instrumentation.java.lang.jdk21; | ||
|
|
||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
| import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import datadog.context.ContextScope; | ||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import java.time.Duration; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import net.bytebuddy.agent.ByteBuddyAgent; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Isolates class-loading assertions from tests that may deliberately load coroutine helpers. */ | ||
| class VirtualThreadScopeClassLoadingForkedTest extends AbstractInstrumentationTest { | ||
| @Test | ||
| void virtualThreadHandoffsDoNotLoadSharedScopeStacks() throws InterruptedException { | ||
| AgentSpan parent = startSpan("test", "parent"); | ||
| AgentSpan[] active = new AgentSpan[2]; | ||
| try (ContextScope ignored = activateSpan(parent)) { | ||
| Thread child = | ||
| Thread.startVirtualThread( | ||
| () -> { | ||
| active[0] = activeSpan(); | ||
| LockSupport.parkNanos(MILLISECONDS.toNanos(1)); | ||
| active[1] = activeSpan(); | ||
| }); | ||
| assertTrue(child.join(Duration.ofSeconds(10))); | ||
| assertSame(parent, active[0]); | ||
| assertSame(parent, active[1]); | ||
| } finally { | ||
| parent.finish(); | ||
| } | ||
| assertTraces(trace(span().root().operationName("parent"))); | ||
|
|
||
| for (Class<?> loaded : ByteBuddyAgent.getInstrumentation().getAllLoadedClasses()) { | ||
| if (loaded != null) { | ||
| String name = loaded.getName(); | ||
| assertFalse( | ||
| "datadog.trace.core.scopemanager.SharedScopeStack".equals(name) | ||
| || "datadog.trace.core.scopemanager.SharedScopeContext".equals(name), | ||
| name); | ||
| } | ||
| } | ||
| } | ||
| } |
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
43 changes: 43 additions & 0 deletions
43
...ation/kotlin-coroutines-1.3/src/latestDepTest/groovy/StandardSuspensionHandoffTest.groovy
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,43 @@ | ||
| import datadog.trace.instrumentation.kotlin.coroutines.AbstractStandardSuspensionHandoffTest | ||
| import datadog.trace.instrumentation.kotlin.coroutines.StandardSuspensionHandoffTests | ||
|
|
||
| class StandardSuspensionHandoffTest extends AbstractStandardSuspensionHandoffTest { | ||
| // withContext uses the context merge hook from coroutines 1.6.1 onward. | ||
| def "context merge preserves an explicitly swapped span"() { | ||
| when: | ||
| new StandardSuspensionHandoffTests().runContextSwapAcrossMerge() | ||
| TEST_WRITER.waitForTraces(1) | ||
| def trace = TEST_WRITER.get(0) | ||
| def spans = trace.collectEntries { [(it.operationName.toString()): it] } | ||
|
|
||
| then: | ||
| trace.size() == 3 | ||
| spans.swapped.parentId == spans.parent.spanId | ||
| spans.child.parentId == spans.swapped.spanId | ||
| } | ||
|
|
||
| def "undispatched coroutine can replay its restoration token"() { | ||
| when: | ||
| new StandardSuspensionHandoffTests().runReplayedRestoration() | ||
|
|
||
| then: | ||
| noExceptionThrown() | ||
| } | ||
|
|
||
| def "scope-touching thread context elements do not corrupt the handed-off stack"() { | ||
| when: | ||
| def violations = new StandardSuspensionHandoffTests().runScopeTouchingElements(1000) | ||
|
|
||
| then: | ||
| // Overlapping workers can temporarily see each other's element scopes. The coroutine must | ||
| // retain its base context, but restore callbacks can observe a different top during overlap. | ||
| def expectedDuringOverlap = [ | ||
| 'element scope leaked from previous worker', | ||
| 'before scope not on top at restore', | ||
| 'after scope not on top at restore', | ||
| 'before restore did not return to previous context', | ||
| 'after restore did not return to previous context' | ||
| ] | ||
| (violations.keySet() - expectedDuringOverlap).isEmpty() | ||
| } | ||
| } |
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
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
4 changes: 4 additions & 0 deletions
4
...nstrumentation/kotlin-coroutines-1.3/src/test/groovy/StandardSuspensionHandoffTest.groovy
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,4 @@ | ||
| import datadog.trace.instrumentation.kotlin.coroutines.AbstractStandardSuspensionHandoffTest | ||
|
|
||
| class StandardSuspensionHandoffTest extends AbstractStandardSuspensionHandoffTest { | ||
| } |
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.