-
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
5
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.
+320
−29
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e15d6c
Fix DDTraceId/DD64bTraceId class-init deadlock at the source
bm1549 ded2c7c
Merge branch 'master' into brian.marks/fix-ddtraceid-clinit-deadlock
bm1549 b94d3d8
Address review: isValid() over isZero(), restore zero cache, extract …
bm1549 b12e3ec
Merge branch 'master' into brian.marks/fix-ddtraceid-clinit-deadlock
bm1549 ec20933
Simplify comments; suppress SpotBugs cross-type equals false positive
bm1549 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
73 changes: 73 additions & 0 deletions
73
dd-trace-api/src/main/java/datadog/trace/api/DDTraceIdConstant.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,73 @@ | ||
| package datadog.trace.api; | ||
|
|
||
| import datadog.trace.api.internal.util.LongStringUtils; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
|
||
| /** | ||
| * Backs {@link DDTraceId#ZERO} and {@link DDTraceId#ONE}. A 64-bit id that is a sibling of {@link | ||
| * DD64bTraceId} (it extends {@link DDTraceId} directly) so initializing {@code DDTraceId} never | ||
| * initializes its subclass; value-equal to the equivalent {@link DD64bTraceId}. | ||
| * | ||
| * <p>Only ever initialize this through {@link DDTraceId}'s constants. Initializing it independently | ||
| * (e.g. a static-member access) would bring back the class-initialization deadlock. | ||
| */ | ||
| final class DDTraceIdConstant extends DDTraceId { | ||
| private final long id; | ||
| private final String str; | ||
| private String hexStr; // cache for hex string representation | ||
|
|
||
| DDTraceIdConstant(long id, String str) { | ||
| this.id = id; | ||
| this.str = str; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return this.str; | ||
| } | ||
|
|
||
| @Override | ||
| public String toHexString() { | ||
| String hexStr = this.hexStr; | ||
| // This race condition is intentional and benign. | ||
| // The worst that can happen is that an identical value is produced and written into the field. | ||
| if (hexStr == null) { | ||
| this.hexStr = hexStr = LongStringUtils.toHexStringPadded(this.id, 32); | ||
| } | ||
| return hexStr; | ||
| } | ||
|
|
||
| @Override | ||
| public String toHexStringPadded(int size) { | ||
| if (size > 16) { | ||
| return toHexString(); | ||
| } | ||
| return LongStringUtils.toHexStringPadded(this.id, size); | ||
| } | ||
|
|
||
| @Override | ||
| public long toLong() { | ||
| return this.id; | ||
| } | ||
|
|
||
| @Override | ||
| public long toHighOrderLong() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressFBWarnings( | ||
| value = "EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS", | ||
| justification = "DD64bTraceId is a sibling type; ZERO/ONE are equal to it by 64-bit value.") | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o instanceof DD64bTraceId) return this.id == ((DD64bTraceId) o).toLong(); | ||
| if (o instanceof DDTraceIdConstant) return this.id == ((DDTraceIdConstant) o).id; | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return (int) (this.id ^ (this.id >>> 32)); | ||
| } | ||
| } |
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()); | ||
| } | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
| 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("00000000000000000000000000000001", DDTraceId.ONE.toHexStringPadded(32)); | ||
| assertEquals(1L, DDTraceId.ONE.toLong()); | ||
| assertEquals(0L, DDTraceId.ONE.toHighOrderLong()); | ||
| } | ||
|
|
||
| @Test | ||
| void isValidReflectsTheValue() { | ||
| assertFalse(DDTraceId.ZERO.isValid()); | ||
| assertFalse(DDTraceId.from(0).isValid()); | ||
| assertFalse(DDTraceId.from("0").isValid()); | ||
| assertFalse(DDTraceId.fromHex("0").isValid()); | ||
| assertFalse(DD64bTraceId.from(0).isValid()); | ||
|
|
||
| assertTrue(DDTraceId.ONE.isValid()); | ||
| assertTrue(DDTraceId.from(1).isValid()); | ||
| assertTrue(DD64bTraceId.from(42).isValid()); | ||
| } | ||
|
|
||
| @Test | ||
| void constantsAreValueEqualToTheEquivalentDD64bTraceId() { | ||
| // ZERO/ONE used to be DD64bTraceId instances; they are now a sibling type but must stay | ||
| // value-equal (both directions, with matching hashCode) to the equivalent DD64bTraceId. | ||
| assertEquals(DDTraceId.ZERO, DD64bTraceId.from(0)); | ||
| assertEquals(DD64bTraceId.from(0), DDTraceId.ZERO); | ||
| assertEquals(DDTraceId.ZERO.hashCode(), DD64bTraceId.from(0).hashCode()); | ||
|
|
||
| assertEquals(DDTraceId.ONE, DD64bTraceId.from(1)); | ||
| assertEquals(DD64bTraceId.from(1), DDTraceId.ONE); | ||
| assertEquals(DDTraceId.ONE.hashCode(), DD64bTraceId.from(1).hashCode()); | ||
| } | ||
| } |
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
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.