Skip to content

Commit 46b17cb

Browse files
committed
Synchronize Baggage keyValues
1 parent df386ef commit 46b17cb

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

sentry/src/main/java/io/sentry/Baggage.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.text.DecimalFormatSymbols;
1414
import java.util.ArrayList;
1515
import java.util.Arrays;
16+
import java.util.Collections;
1617
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Locale;
@@ -44,13 +45,13 @@ protected DecimalFormat initialValue() {
4445
private static final DecimalFormatterThreadLocal decimalFormatter =
4546
new DecimalFormatterThreadLocal();
4647

47-
final @NotNull Map<String, String> keyValues;
48-
@Nullable Double sampleRate;
49-
@Nullable Double sampleRand;
48+
private final @NotNull Map<String, String> keyValues;
49+
private @Nullable Double sampleRate;
50+
private @Nullable Double sampleRand;
5051

51-
final @Nullable String thirdPartyHeader;
52+
private final @Nullable String thirdPartyHeader;
5253
private boolean mutable;
53-
private boolean shouldFreeze;
54+
private final boolean shouldFreeze;
5455
final @NotNull ILogger logger;
5556

5657
@NotNull
@@ -217,10 +218,12 @@ public Baggage(
217218
final @Nullable Double sampleRate,
218219
final @Nullable Double sampleRand,
219220
final @Nullable String thirdPartyHeader,
220-
boolean isMutable,
221-
boolean shouldFreeze,
221+
final boolean isMutable,
222+
final boolean shouldFreeze,
222223
final @NotNull ILogger logger) {
223-
this.keyValues = keyValues;
224+
// TODO should we deep-copy the keyValues here?
225+
// if so we could optimize this by only synchronizing in case isMutable is true
226+
this.keyValues = Collections.synchronizedMap(keyValues);
224227
this.sampleRate = sampleRate;
225228
this.sampleRand = sampleRand;
226229
this.logger = logger;
@@ -440,20 +443,15 @@ public void setReplayId(final @Nullable String replayId) {
440443
set(DSCKeys.REPLAY_ID, replayId);
441444
}
442445

443-
@ApiStatus.Internal
444-
public void set(final @NotNull String key, final @Nullable String value) {
445-
set(key, value, false);
446-
}
447-
448446
/**
449-
* Sets / updates a value
447+
* Sets / updates a value, but only if the baggage is still mutable.
450448
*
451449
* @param key key
452450
* @param value value to set
453-
* @param force ignores mutability of this baggage and sets the value anyways
454451
*/
455-
private void set(final @NotNull String key, final @Nullable String value, final boolean force) {
456-
if (mutable || force) {
452+
@ApiStatus.Internal
453+
public void set(final @NotNull String key, final @Nullable String value) {
454+
if (mutable) {
457455
this.keyValues.put(key, value);
458456
}
459457
}

0 commit comments

Comments
 (0)