-
Notifications
You must be signed in to change notification settings - Fork 337
Fix DDTraceId/DD64bTraceId class-initialization deadlock #11509
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
bm1549
wants to merge
2
commits into
master
Choose a base branch
from
brian.marks/fix-ddtraceid-clinit-deadlock
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
2 commits
Select commit
Hold shift + click to select a range
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
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
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
88 changes: 88 additions & 0 deletions
88
dd-trace-api/src/test/java/datadog/trace/api/DDTraceIdClinitDeadlockForkedTest.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,88 @@ | ||
| package datadog.trace.api; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import java.util.concurrent.CyclicBarrier; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
|
|
||
| /** | ||
| * Regression test for the {@code DDTraceId} <-> {@code DD64bTraceId} class-initialization | ||
| * deadlock. | ||
| * | ||
| * <p>{@code DD64bTraceId} is a subclass of {@code DDTraceId}, so the JVM must initialize {@code | ||
| * DDTraceId} before {@code DD64bTraceId}. The bug was that {@code DDTraceId.<clinit>} in turn | ||
| * initialized {@code DD64bTraceId} by building its {@code ZERO}/{@code ONE} constants via {@code | ||
| * DD64bTraceId.from(...)}. When the two classes were first touched concurrently from opposite ends | ||
| * (one thread initializing {@code DDTraceId}, another initializing {@code DD64bTraceId}), each | ||
| * thread held one class-initialization lock and waited for the other, hanging trace creation. This | ||
| * surfaced as 30s {@code LogInjectionSmokeTest} timeouts in CI. | ||
| * | ||
| * <p>{@code DDTraceId.ZERO}/{@code ONE} are now instances of a private sibling type (not {@code | ||
| * DD64bTraceId}), so {@code DDTraceId.<clinit>} no longer references {@code DD64bTraceId} and the | ||
| * cycle is gone. This test initializes the two classes for the first time concurrently from | ||
| * opposite ends and asserts neither thread hangs. | ||
| * | ||
| * <p>Runs forked ({@code forkEvery = 1}) so it gets a fresh JVM in which these classes have not yet | ||
| * been initialized by another test. Without the fix it deadlocks and fails via the join check (and | ||
| * the {@code @Timeout} backstop); with the fix it completes immediately. | ||
| */ | ||
| class DDTraceIdClinitDeadlockForkedTest { | ||
|
|
||
| @Test | ||
| @Timeout(value = 60, unit = SECONDS) // backstop; the join below is the primary guard | ||
| void traceIdClassPairInitializesConcurrentlyWithoutDeadlock() throws Exception { | ||
| final ClassLoader cl = getClass().getClassLoader(); | ||
| final CyclicBarrier barrier = new CyclicBarrier(2); | ||
| final AtomicReference<Throwable> error = new AtomicReference<>(); | ||
|
|
||
| // One thread enters via the superclass (mirrors blackholeSpan() -> DDTraceId.ZERO), the other | ||
| // via the subclass (mirrors IdGenerationStrategy.generateTraceId() -> DD64bTraceId.from()). | ||
| Thread viaSuper = | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| barrier.await(); | ||
| Class.forName("datadog.trace.api.DDTraceId", true, cl); | ||
| } catch (Throwable t) { | ||
| error.compareAndSet(null, t); | ||
| } | ||
| }, | ||
| "init-DDTraceId"); | ||
| Thread viaSub = | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| barrier.await(); | ||
| Class.forName("datadog.trace.api.DD64bTraceId", true, cl); | ||
| } catch (Throwable t) { | ||
| error.compareAndSet(null, t); | ||
| } | ||
| }, | ||
| "init-DD64bTraceId"); | ||
| // Daemon so a deadlock cannot block forked-JVM shutdown. | ||
| viaSuper.setDaemon(true); | ||
| viaSub.setDaemon(true); | ||
|
|
||
| viaSuper.start(); | ||
| viaSub.start(); | ||
| viaSuper.join(SECONDS.toMillis(15)); | ||
| viaSub.join(SECONDS.toMillis(15)); | ||
|
|
||
| if (viaSuper.isAlive() || viaSub.isAlive()) { | ||
| fail( | ||
| "DDTraceId/DD64bTraceId class-initialization deadlock: DDTraceId.<clinit> must not " | ||
| + "reference DD64bTraceId (init-DDTraceId.alive=" | ||
| + viaSuper.isAlive() | ||
| + ", init-DD64bTraceId.alive=" | ||
| + viaSub.isAlive() | ||
| + ")."); | ||
| } | ||
| if (error.get() != null) { | ||
| throw new AssertionError( | ||
| "Unexpected error during concurrent class initialization", error.get()); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
dd-trace-api/src/test/java/datadog/trace/api/DDTraceIdConstantsTest.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,40 @@ | ||
| package datadog.trace.api; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Behavior of the {@link DDTraceId#ZERO}/{@link DDTraceId#ONE} sibling constants. */ | ||
| class DDTraceIdConstantsTest { | ||
|
|
||
| @Test | ||
| void zeroAndOneFormatLikeTheEquivalentDD64bTraceId() { | ||
| assertEquals("0", DDTraceId.ZERO.toString()); | ||
| assertEquals("00000000000000000000000000000000", DDTraceId.ZERO.toHexString()); | ||
| assertEquals("0000000000000000", DDTraceId.ZERO.toHexStringPadded(16)); | ||
| assertEquals("00000000000000000000000000000000", DDTraceId.ZERO.toHexStringPadded(32)); | ||
| assertEquals(0L, DDTraceId.ZERO.toLong()); | ||
| assertEquals(0L, DDTraceId.ZERO.toHighOrderLong()); | ||
|
|
||
| assertEquals("1", DDTraceId.ONE.toString()); | ||
| assertEquals("00000000000000000000000000000001", DDTraceId.ONE.toHexString()); | ||
| assertEquals("0000000000000001", DDTraceId.ONE.toHexStringPadded(16)); | ||
| assertEquals(1L, DDTraceId.ONE.toLong()); | ||
| assertEquals(0L, DDTraceId.ONE.toHighOrderLong()); | ||
| } | ||
|
|
||
| @Test | ||
| void isZeroReflectsTheValue() { | ||
| assertTrue(DDTraceId.ZERO.isZero()); | ||
| assertTrue(DDTraceId.from(0).isZero()); | ||
| assertTrue(DDTraceId.from("0").isZero()); | ||
| assertTrue(DDTraceId.fromHex("0").isZero()); | ||
| assertTrue(DD64bTraceId.from(0).isZero()); | ||
|
|
||
| assertFalse(DDTraceId.ONE.isZero()); | ||
| assertFalse(DDTraceId.from(1).isZero()); | ||
| assertFalse(DD64bTraceId.from(42).isZero()); | ||
| } | ||
| } |
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
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
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
40 changes: 40 additions & 0 deletions
40
dd-trace-core/src/test/java/datadog/trace/api/TraceIdIsZeroTest.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,40 @@ | ||
| package datadog.trace.api; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import datadog.trace.core.propagation.B3TraceId; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * {@link DDTraceId#isZero()} across every {@link DDTraceId} subtype, including the {@code ZERO}/ | ||
| * {@code ONE} sibling constants and {@link B3TraceId} (defined in dd-trace-core). | ||
| * | ||
| * <p>{@code isZero()} is the value-based replacement for the old {@code == DDTraceId.ZERO} identity | ||
| * checks, so it must recognize a zero id regardless of concrete type or how it was created (a zero | ||
| * parsed via the 64-bit factories is a distinct instance from the {@code ZERO} singleton). | ||
| */ | ||
| class TraceIdIsZeroTest { | ||
|
|
||
| @Test | ||
| void zeroValuedIdsOfEveryTypeAreZero() { | ||
| assertTrue(DDTraceId.ZERO.isZero()); | ||
| assertTrue(DDTraceId.from(0).isZero()); | ||
| assertTrue(DDTraceId.from("0").isZero()); | ||
| assertTrue(DDTraceId.fromHex("0").isZero()); | ||
| assertTrue(DD64bTraceId.from(0).isZero()); | ||
| assertTrue(DD128bTraceId.from(0, 0).isZero()); | ||
| assertTrue(B3TraceId.fromHex("0").isZero()); | ||
| } | ||
|
|
||
| @Test | ||
| void nonZeroIdsAreNotZero() { | ||
| assertFalse(DDTraceId.ONE.isZero()); | ||
| assertFalse(DDTraceId.from(1).isZero()); | ||
| assertFalse(DD64bTraceId.from(42).isZero()); | ||
| assertFalse(DD128bTraceId.from(0, 1).isZero()); | ||
| // High-order bits set, low-order zero: still not a zero TraceId. | ||
| assertFalse(DD128bTraceId.from(7, 0).isZero()); | ||
| assertFalse(B3TraceId.fromHex("2a").isZero()); | ||
| } | ||
| } |
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
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.