-
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
base: master
Are you sure you want to change the base?
Changes from all commits
0e15d6c
ded2c7c
b94d3d8
b12e3ec
ec20933
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do wonder if there's a simpler fix that doesn't require introducing another class, as this is making the equals matrix harder to maintain. I'll need to think this through, but eliminating its construction via the static method might be enough. We may need to allow 2 copies of |
||
| 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)); | ||
| } | ||
| } | ||
| 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()); | ||
| } | ||
| } | ||
| } |
| 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()); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.