Skip to content

Commit a4b7a7b

Browse files
authored
Avoid potential race conditions on collecting process tags (#8799)
* Avoid potential race conditions on collecting process tags * Add synchronisation on test methods
1 parent 90ca83f commit a4b7a7b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

internal-api/src/main/java/datadog/trace/api/ProcessTags.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ static void calculate() {
102102
private ProcessTags() {}
103103

104104
// need to be synchronized on writing. As optimization, it does not need to be sync on read.
105-
public static synchronized void addTag(String key, String value) {
105+
public static void addTag(String key, String value) {
106106
if (enabled) {
107-
Lazy.TAGS.put(key, value);
108-
Lazy.serializedForm = null;
109-
Lazy.listForm = null;
107+
synchronized (Lazy.TAGS) {
108+
Lazy.TAGS.put(key, value);
109+
Lazy.serializedForm = null;
110+
Lazy.listForm = null;
111+
}
110112
}
111113
}
112114

@@ -136,15 +138,19 @@ public static UTF8BytesString getTagsForSerialization() {
136138

137139
/** Visible for testing. */
138140
static void empty() {
139-
Lazy.TAGS.clear();
140-
Lazy.serializedForm = null;
141-
Lazy.listForm = null;
141+
synchronized (Lazy.TAGS) {
142+
Lazy.TAGS.clear();
143+
Lazy.serializedForm = null;
144+
Lazy.listForm = null;
145+
}
142146
}
143147

144148
/** Visible for testing. */
145149
static void reset() {
146-
empty();
147-
enabled = Config.get().isExperimentalPropagateProcessTagsEnabled();
148-
Lazy.TAGS.putAll(Lazy.loadTags());
150+
synchronized (Lazy.TAGS) {
151+
empty();
152+
enabled = Config.get().isExperimentalPropagateProcessTagsEnabled();
153+
Lazy.TAGS.putAll(Lazy.loadTags());
154+
}
149155
}
150156
}

0 commit comments

Comments
 (0)