Skip to content

Commit 004d861

Browse files
runningcodeclaude
andauthored
fix(core): Make feature flag buffer merging thread-safe (#5989)
* fix(core): Make feature flag buffer merging thread-safe (JAVA-704) FeatureFlagBuffer.merged() read size() and then indexed into the live CopyOnWriteArrayList held by each buffer. add() mutates in three steps (remove, add, remove), so the list transiently shrinks and a merging thread could index past its end. Scopes.captureEventInternal swallows the resulting ArrayIndexOutOfBoundsException, so the event is silently dropped, including events from the uncaught exception handler. Capturing the list reference did not help: the field was never reassigned, so every reader saw the same live list. Hold an immutable list behind the volatile field instead and swap it under the existing lock, so one volatile read yields a snapshot that can no longer change. This also makes clone() free, since it now shares the list rather than copying it, and cuts add() from three full array copies down to one. CopyOnWriteArrayList is no longer needed and maxSize becomes final, which it always should have been. Entries carrying identical timestamps now resolve in favour of the most specific scope, CURRENT over ISOLATION over GLOBAL, rather than the reverse. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * changelog --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a5555ea commit 004d861

3 files changed

Lines changed: 115 additions & 67 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
- Prevents inclusion of `null.` prefix before default-package class names when parsing Java and JNI frames from Android ANR thread dumps ([#5979](https://github.com/getsentry/sentry-java/pull/5979))
5454
- Prevent duplicated breadcrumbs on tombstone-merged native crash events ([#5888](https://github.com/getsentry/sentry-java/pull/5888))
55+
- Prevent events from being dropped when feature flags are added while an event is being captured ([#5989](https://github.com/getsentry/sentry-java/pull/5989))
5556
- Prevent a class of Session Replay deadlocks by confining lifecycle state changes to Android's main thread ([#5965](https://github.com/getsentry/sentry-java/pull/5965))
5657
- Symbolicate tombstone native frames for libraries loaded directly from APKs ([#5992](https://github.com/getsentry/sentry-java/pull/5992))
5758
- Prevent a deadlock between the app start extension and the Android performance event processor ([#6007](https://github.com/getsentry/sentry-java/pull/6007))

‎sentry/src/main/java/io/sentry/featureflags/FeatureFlagBuffer.java‎

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import io.sentry.util.AutoClosableReentrantLock;
99
import java.util.ArrayList;
1010
import java.util.Collections;
11+
import java.util.LinkedHashMap;
1112
import java.util.List;
12-
import java.util.concurrent.CopyOnWriteArrayList;
13+
import java.util.Map;
1314
import org.jetbrains.annotations.ApiStatus;
1415
import org.jetbrains.annotations.NotNull;
1516
import org.jetbrains.annotations.Nullable;
@@ -23,71 +24,71 @@
2324
* <li>Performance of scope cloning is optimized here
2425
* <li>Supports merging across scope types (GLOBAL, ISOLATION, CURRENT)
2526
* </ul>
27+
*
28+
* <p>{@link #flags} always holds an immutable list. Writers hold {@link #lock} and swap in a new
29+
* list; readers take a single volatile read and are then free to iterate or index into a list that
30+
* can no longer change. This is what makes {@link #clone()} free: the copy shares the list rather
31+
* than duplicating it.
2632
*/
2733
@ApiStatus.Internal
2834
public final class FeatureFlagBuffer implements IFeatureFlagBuffer {
2935

30-
private volatile @NotNull CopyOnWriteArrayList<FeatureFlagEntry> flags;
36+
private volatile @NotNull List<FeatureFlagEntry> flags;
3137
private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();
32-
private int maxSize;
38+
private final int maxSize;
3339

34-
private FeatureFlagBuffer(int maxSize) {
35-
this.maxSize = maxSize;
36-
this.flags = new CopyOnWriteArrayList<>();
40+
private FeatureFlagBuffer(final int maxSize) {
41+
this(maxSize, Collections.<FeatureFlagEntry>emptyList());
3742
}
3843

39-
private FeatureFlagBuffer(
40-
int maxSize, final @NotNull CopyOnWriteArrayList<FeatureFlagEntry> flags) {
44+
private FeatureFlagBuffer(final int maxSize, final @NotNull List<FeatureFlagEntry> flags) {
4145
this.maxSize = maxSize;
4246
this.flags = flags;
4347
}
4448

45-
private FeatureFlagBuffer(@NotNull FeatureFlagBuffer other) {
46-
this.maxSize = other.maxSize;
47-
this.flags = new CopyOnWriteArrayList<>(other.flags);
48-
}
49-
5049
@Override
5150
public void add(final @Nullable String flag, final @Nullable Boolean result) {
5251
if (flag == null || result == null) {
5352
return;
5453
}
5554
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
56-
final int size = flags.size();
57-
for (int i = 0; i < size; i++) {
58-
final @NotNull FeatureFlagEntry entry = flags.get(i);
59-
if (entry.flag.equals(flag)) {
60-
flags.remove(i);
61-
break;
55+
final @NotNull List<FeatureFlagEntry> current = flags;
56+
final @NotNull List<FeatureFlagEntry> updated = new ArrayList<>(current.size() + 1);
57+
for (final @NotNull FeatureFlagEntry entry : current) {
58+
if (!entry.flag.equals(flag)) {
59+
updated.add(entry);
6260
}
6361
}
64-
flags.add(new FeatureFlagEntry(flag, result, System.nanoTime()));
62+
updated.add(new FeatureFlagEntry(flag, result, System.nanoTime()));
6563

66-
if (flags.size() > maxSize) {
67-
flags.remove(0);
64+
if (updated.size() > maxSize) {
65+
updated.remove(0);
6866
}
67+
68+
flags = Collections.unmodifiableList(updated);
6969
}
7070
}
7171

7272
@Override
7373
public void clear() {
7474
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
75-
flags.clear();
75+
flags = Collections.emptyList();
7676
}
7777
}
7878

7979
@Override
8080
public @Nullable FeatureFlags getFeatureFlags() {
81-
List<FeatureFlag> featureFlags = new ArrayList<>();
82-
for (FeatureFlagEntry entry : flags) {
81+
final @NotNull List<FeatureFlagEntry> snapshot = flags;
82+
final @NotNull List<FeatureFlag> featureFlags = new ArrayList<>(snapshot.size());
83+
for (final @NotNull FeatureFlagEntry entry : snapshot) {
8384
featureFlags.add(entry.toFeatureFlag());
8485
}
8586
return new FeatureFlags(featureFlags);
8687
}
8788

8889
@Override
8990
public @NotNull IFeatureFlagBuffer clone() {
90-
return new FeatureFlagBuffer(this);
91+
return new FeatureFlagBuffer(maxSize, flags);
9192
}
9293

9394
public static @NotNull IFeatureFlagBuffer create(final @NotNull SentryOptions options) {
@@ -123,6 +124,9 @@ public void clear() {
123124
* <p>If a duplicate is found we skip it since we're iterating in reverse order and we already
124125
* have the latest entry.
125126
*
127+
* <p>Entries carrying the same timestamp are resolved in favour of the most specific scope, so
128+
* CURRENT wins over ISOLATION, which in turn wins over GLOBAL.
129+
*
126130
* @param maxSize max number of feature flags
127131
* @param globalBuffer buffer from global scope
128132
* @param isolationBuffer buffer from isolation scope
@@ -135,12 +139,13 @@ public void clear() {
135139
final @Nullable FeatureFlagBuffer isolationBuffer,
136140
final @Nullable FeatureFlagBuffer currentBuffer) {
137141

138-
// Capture references to avoid inconsistencies from concurrent modifications
139-
final @Nullable CopyOnWriteArrayList<FeatureFlagEntry> globalFlags =
142+
// One volatile read each pins an immutable list, so concurrent writers cannot shift these
143+
// out from under the index arithmetic below
144+
final @Nullable List<FeatureFlagEntry> globalFlags =
140145
globalBuffer == null ? null : globalBuffer.flags;
141-
final @Nullable CopyOnWriteArrayList<FeatureFlagEntry> isolationFlags =
146+
final @Nullable List<FeatureFlagEntry> isolationFlags =
142147
isolationBuffer == null ? null : isolationBuffer.flags;
143-
final @Nullable CopyOnWriteArrayList<FeatureFlagEntry> currentFlags =
148+
final @Nullable List<FeatureFlagEntry> currentFlags =
144149
currentBuffer == null ? null : currentBuffer.flags;
145150

146151
final int globalSize = globalFlags == null ? 0 : globalFlags.size();
@@ -166,8 +171,7 @@ public void clear() {
166171
FeatureFlagEntry currentEntry =
167172
currentFlags == null || currentIndex < 0 ? null : currentFlags.get(currentIndex);
168173

169-
final @NotNull java.util.Map<String, FeatureFlagEntry> uniqueFlags =
170-
new java.util.LinkedHashMap<>(maxSize);
174+
final @NotNull Map<String, FeatureFlagEntry> uniqueFlags = new LinkedHashMap<>(maxSize);
171175

172176
// check if there is still room and remaining items to check
173177
while (uniqueFlags.size() < maxSize
@@ -177,66 +181,59 @@ public void clear() {
177181
@Nullable ScopeType selectedBuffer = null;
178182

179183
// choose newest entry across all buffers
180-
if (globalEntry != null && (entryToAdd == null || globalEntry.nanos > entryToAdd.nanos)) {
184+
if (globalEntry != null) {
181185
entryToAdd = globalEntry;
182186
selectedBuffer = ScopeType.GLOBAL;
183187
}
184188
if (isolationEntry != null
185-
&& (entryToAdd == null || isolationEntry.nanos > entryToAdd.nanos)) {
189+
&& (entryToAdd == null || isolationEntry.nanos >= entryToAdd.nanos)) {
186190
entryToAdd = isolationEntry;
187191
selectedBuffer = ScopeType.ISOLATION;
188192
}
189-
if (currentEntry != null && (entryToAdd == null || currentEntry.nanos > entryToAdd.nanos)) {
193+
if (currentEntry != null && (entryToAdd == null || currentEntry.nanos >= entryToAdd.nanos)) {
190194
entryToAdd = currentEntry;
191195
selectedBuffer = ScopeType.CURRENT;
192196
}
193197

194-
if (entryToAdd != null) {
195-
// no need to update existing entries since we already have the latest
196-
if (!uniqueFlags.containsKey(entryToAdd.flag)) {
197-
uniqueFlags.put(entryToAdd.flag, entryToAdd);
198-
}
199-
200-
// decrement only index of buffer that was selected
201-
if (ScopeType.CURRENT.equals(selectedBuffer)) {
202-
currentIndex--;
203-
currentEntry =
204-
currentFlags != null && currentIndex >= 0 ? currentFlags.get(currentIndex) : null;
205-
} else if (ScopeType.ISOLATION.equals(selectedBuffer)) {
206-
isolationIndex--;
207-
isolationEntry =
208-
isolationFlags != null && isolationIndex >= 0
209-
? isolationFlags.get(isolationIndex)
210-
: null;
211-
} else if (ScopeType.GLOBAL.equals(selectedBuffer)) {
212-
globalIndex--;
213-
globalEntry =
214-
globalFlags != null && globalIndex >= 0 ? globalFlags.get(globalIndex) : null;
215-
}
216-
} else {
217-
// no need to look any further since lists are sorted and we could not find any newer
218-
// entries anymore
198+
if (entryToAdd == null) {
219199
break;
220200
}
201+
202+
// no need to update existing entries since we already have the latest
203+
if (!uniqueFlags.containsKey(entryToAdd.flag)) {
204+
uniqueFlags.put(entryToAdd.flag, entryToAdd);
205+
}
206+
207+
// decrement only index of buffer that was selected
208+
if (selectedBuffer == ScopeType.CURRENT) {
209+
currentIndex--;
210+
currentEntry =
211+
currentFlags != null && currentIndex >= 0 ? currentFlags.get(currentIndex) : null;
212+
} else if (selectedBuffer == ScopeType.ISOLATION) {
213+
isolationIndex--;
214+
isolationEntry =
215+
isolationFlags != null && isolationIndex >= 0
216+
? isolationFlags.get(isolationIndex)
217+
: null;
218+
} else if (selectedBuffer == ScopeType.GLOBAL) {
219+
globalIndex--;
220+
globalEntry = globalFlags != null && globalIndex >= 0 ? globalFlags.get(globalIndex) : null;
221+
}
221222
}
222223

223224
// Convert to list in reverse order (oldest first, newest last)
224225
final @NotNull List<FeatureFlagEntry> resultList = new ArrayList<>(uniqueFlags.values());
225226
Collections.reverse(resultList);
226-
return new FeatureFlagBuffer(maxSize, new CopyOnWriteArrayList<>(resultList));
227+
return new FeatureFlagBuffer(maxSize, Collections.unmodifiableList(resultList));
227228
}
228229

229230
private static class FeatureFlagEntry {
230231

231232
private final @NotNull String flag;
232233
private final boolean result;
234+
private final long nanos;
233235

234-
@SuppressWarnings("UnusedVariable")
235-
@NotNull
236-
private final Long nanos;
237-
238-
public FeatureFlagEntry(
239-
final @NotNull String flag, final boolean result, final @NotNull Long nanos) {
236+
public FeatureFlagEntry(final @NotNull String flag, final boolean result, final long nanos) {
240237
this.flag = flag;
241238
this.result = result;
242239
this.nanos = nanos;

‎sentry/src/test/java/io/sentry/featureflags/FeatureFlagBufferTest.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.sentry.featureflags
22

3+
import com.google.common.truth.Truth.assertThat
34
import io.sentry.SentryOptions
5+
import java.util.concurrent.atomic.AtomicBoolean
6+
import java.util.concurrent.atomic.AtomicReference
47
import kotlin.test.Test
58
import kotlin.test.assertEquals
69
import kotlin.test.assertFalse
@@ -347,4 +350,51 @@ class FeatureFlagBufferTest {
347350
val featureFlags = buffer.featureFlags
348351
assertNotNull(featureFlags)
349352
}
353+
354+
@Test
355+
fun `clone does not share later writes with the original`() {
356+
val buffer = FeatureFlagBuffer.create(SentryOptions().also { it.maxFeatureFlags = 5 })
357+
buffer.add("a", true)
358+
359+
val clone = buffer.clone()
360+
clone.add("b", true)
361+
buffer.add("c", true)
362+
363+
assertThat(buffer.featureFlags!!.values.map { it!!.flag }).containsExactly("a", "c").inOrder()
364+
assertThat(clone.featureFlags!!.values.map { it!!.flag }).containsExactly("a", "b").inOrder()
365+
}
366+
367+
@Test
368+
fun `merging is safe while another thread adds flags`() {
369+
val options = SentryOptions().also { it.maxFeatureFlags = 100 }
370+
val globalBuffer = FeatureFlagBuffer.create(options)
371+
val isolationBuffer = FeatureFlagBuffer.create(options)
372+
val currentBuffer = FeatureFlagBuffer.create(options)
373+
374+
val stop = AtomicBoolean(false)
375+
val writerFailure = AtomicReference<Throwable?>(null)
376+
// more distinct names than maxFeatureFlags, so adds mix evictions in with refreshes
377+
val writer = Thread {
378+
try {
379+
var i = 0
380+
while (!stop.get()) {
381+
globalBuffer.add("flag${i++ % 150}", true)
382+
}
383+
} catch (e: Exception) {
384+
writerFailure.set(e)
385+
}
386+
}
387+
388+
writer.start()
389+
try {
390+
repeat(10_000) {
391+
FeatureFlagBuffer.merged(options, globalBuffer, isolationBuffer, currentBuffer).featureFlags
392+
}
393+
} finally {
394+
stop.set(true)
395+
writer.join()
396+
}
397+
398+
assertThat(writerFailure.get()).isNull()
399+
}
350400
}

0 commit comments

Comments
 (0)