Skip to content

Commit 3ad504c

Browse files
adinauerclaude
andcommitted
fix(core): Make trace ignore statuses thread-safe
Store traceIgnoreStatusCodes in a copy-on-write list so callers can mutate the exposed configuration while transactions iterate it. Mark the replaceable reference volatile so setter updates are visible across threads. Refs #4736 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent cf2f6fc commit 3ad504c

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎sentry/src/main/java/io/sentry/SentryOptions.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ public class SentryOptions implements RateLimiterConfig {
599599
*/
600600
private @Nullable List<FilterString> ignoredTransactions = null;
601601

602-
private @Nullable List<HttpStatusCodeRange> traceIgnoreStatusCodes =
603-
new ArrayList<>(
602+
private volatile @Nullable List<HttpStatusCodeRange> traceIgnoreStatusCodes =
603+
new CopyOnWriteArrayList<>(
604604
Arrays.asList(
605605
new HttpStatusCodeRange(301, 303),
606606
new HttpStatusCodeRange(305, 399),
@@ -3179,7 +3179,7 @@ public void setIgnoredTransactions(final @Nullable List<String> ignoredTransacti
31793179
public void setTraceIgnoreStatusCodes(
31803180
final @Nullable List<HttpStatusCodeRange> traceIgnoreStatusCodes) {
31813181
this.traceIgnoreStatusCodes =
3182-
traceIgnoreStatusCodes == null ? null : new ArrayList<>(traceIgnoreStatusCodes);
3182+
traceIgnoreStatusCodes == null ? null : new CopyOnWriteArrayList<>(traceIgnoreStatusCodes);
31833183
}
31843184

31853185
/** Returns the current {@link SentryDateProvider} that is used to retrieve the current date. */

‎sentry/src/test/java/io/sentry/SentryOptionsTest.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.sentry.util.LazyEvaluator
1010
import io.sentry.util.StringUtils
1111
import java.io.File
1212
import java.net.Proxy
13+
import java.util.concurrent.CopyOnWriteArrayList
1314
import kotlin.test.Test
1415
import kotlin.test.assertEquals
1516
import kotlin.test.assertFailsWith
@@ -1091,6 +1092,7 @@ class SentryOptionsTest {
10911092
fun `ignored server HTTP status codes for tracing use the spec defaults`() {
10921093
val ranges = SentryOptions().traceIgnoreStatusCodes!!
10931094

1095+
assertThat(ranges).isInstanceOf(CopyOnWriteArrayList::class.java)
10941096
assertThat(ranges).hasSize(3)
10951097
listOf(301, 303, 305, 399, 401, 404).forEach {
10961098
assertThat(ranges.any { range -> range.isInRange(it) }).isTrue()
@@ -1112,6 +1114,16 @@ class SentryOptionsTest {
11121114
assertThat(second.any { it.isInRange(404) }).isTrue()
11131115
}
11141116

1117+
@Test
1118+
fun `ignored server HTTP status code iteration uses a stable snapshot`() {
1119+
val ranges = SentryOptions().traceIgnoreStatusCodes!!
1120+
val iterator = ranges.iterator()
1121+
1122+
ranges.clear()
1123+
1124+
assertThat(iterator.asSequence().toList()).hasSize(3)
1125+
}
1126+
11151127
@Test
11161128
fun `ignored server HTTP status code setter preserves null, empty and custom ranges`() {
11171129
val options = SentryOptions()
@@ -1123,6 +1135,7 @@ class SentryOptionsTest {
11231135
assertThat(options.traceIgnoreStatusCodes).isEmpty()
11241136

11251137
options.traceIgnoreStatusCodes = listOf(HttpStatusCodeRange(404), HttpStatusCodeRange(500, 599))
1138+
assertThat(options.traceIgnoreStatusCodes).isInstanceOf(CopyOnWriteArrayList::class.java)
11261139
assertThat(options.traceIgnoreStatusCodes).hasSize(2)
11271140
assertThat(options.traceIgnoreStatusCodes!![0].isInRange(404)).isTrue()
11281141
assertThat(options.traceIgnoreStatusCodes!![1].isInRange(599)).isTrue()

0 commit comments

Comments
 (0)