Skip to content

Commit bb44d60

Browse files
mccullsluneo7
andauthored
Avoid collected store ids from leaking to later field-injections when an instrumentation fails (#7403)
Co-authored-by: luneo7 <[email protected]>
1 parent 2f32a35 commit bb44d60

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/context/FieldBackedContextInjector.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import datadog.trace.agent.tooling.bytebuddy.memoize.MemoizedMatchers;
77
import datadog.trace.api.InstrumenterConfig;
8+
import datadog.trace.api.Pair;
89
import datadog.trace.bootstrap.ContextStore;
910
import datadog.trace.bootstrap.FieldBackedContextAccessor;
1011
import datadog.trace.bootstrap.FieldBackedContextStores;
@@ -70,7 +71,7 @@ public final class FieldBackedContextInjector implements AsmVisitorWrapper {
7071
Type.getType(FieldBackedContextAccessor.class);
7172

7273
/** Keeps track of injection requests for the class being transformed by the current thread. */
73-
static final ThreadLocal<BitSet> INJECTED_STORE_IDS = new ThreadLocal<>();
74+
static final ThreadLocal<Pair<String, BitSet>> INJECTED_STORE_IDS = new ThreadLocal<>();
7475

7576
final boolean serialVersionUIDFieldInjection =
7677
InstrumenterConfig.get().isSerialVersionUIDFieldInjection();
@@ -129,7 +130,7 @@ public void visit(
129130

130131
// keep track of all injection requests for the class currently being transformed
131132
// because we need to switch between them in the generated getter/putter methods
132-
int storeId = injectContextStore(keyClassName, contextClassName);
133+
int storeId = injectContextStore(name, keyClassName, contextClassName);
133134
storeFieldName = CONTEXT_STORE_ACCESS_PREFIX + storeId;
134135

135136
if (interfaces == null) {
@@ -484,26 +485,30 @@ private void invokeSuperPut(final MethodVisitor mv, final String superName) {
484485
}
485486

486487
/** Requests injection of a context store for a key and context. */
487-
static int injectContextStore(final String keyClassName, final String contextClassName) {
488+
static int injectContextStore(
489+
final String target, final String keyClassName, final String contextClassName) {
488490
int storeId = getContextStoreId(keyClassName, contextClassName);
489491

490-
BitSet injectedStoreIds = INJECTED_STORE_IDS.get();
491-
if (null == injectedStoreIds) {
492-
injectedStoreIds = new BitSet();
492+
// collect a new set of store ids every time we see a new target
493+
Pair<String, BitSet> injectedStoreIds = INJECTED_STORE_IDS.get();
494+
if (null == injectedStoreIds || !target.equals(injectedStoreIds.getLeft())) {
495+
injectedStoreIds = Pair.of(target, new BitSet());
493496
INJECTED_STORE_IDS.set(injectedStoreIds);
494497
}
495-
injectedStoreIds.set(storeId);
498+
injectedStoreIds.getRight().set(storeId);
496499

497500
return storeId;
498501
}
499502

500503
/** Returns all context store injection requests for the class being transformed. */
501504
static BitSet getInjectedContextStores() {
502-
BitSet injectedStoreIds = INJECTED_STORE_IDS.get();
505+
Pair<String, BitSet> injectedStoreIds = INJECTED_STORE_IDS.get();
503506
if (null != injectedStoreIds) {
504507
INJECTED_STORE_IDS.remove();
508+
return injectedStoreIds.getRight();
509+
} else {
510+
return null;
505511
}
506-
return injectedStoreIds;
507512
}
508513

509514
private static final class SerialVersionUIDInjector

0 commit comments

Comments
 (0)