Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `SentryTraced` now checks for its owning transaction dynamically rather than once per app process. The latter caused `SentryTraced` spans to be dropped process-wide once the original transaction finished ([#6057](https://github.com/getsentry/sentry-java/pull/6057))
- Fix typos in Spring GraphQL integration names (`GrahQL` to `GraphQL`) ([#6061](https://github.com/getsentry/sentry-java/pull/6061))
- Prevent infinite loops when capturing exceptions with cyclic cause chains ([#6073](https://github.com/getsentry/sentry-java/pull/6073))

## 8.55.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -54,10 +56,12 @@ private static <T> boolean containsAnyKey(

private static @NotNull List<Throwable> allCauses(final @NotNull Throwable throwable) {
final List<Throwable> causes = new ArrayList<>();
Throwable ex = throwable;
while (ex.getCause() != null) {
causes.add(ex.getCause());
ex = ex.getCause();
final Set<Throwable> visited = Collections.newSetFromMap(new IdentityHashMap<>());
visited.add(throwable);
Throwable cause = throwable.getCause();
while (cause != null && visited.add(cause)) {
causes.add(cause);
cause = cause.getCause();
}
return causes;
}
Expand Down
10 changes: 8 additions & 2 deletions sentry/src/main/java/io/sentry/util/ExceptionUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sentry.util;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -16,8 +18,12 @@ public final class ExceptionUtils {
public static @NotNull Throwable findRootCause(final @NotNull Throwable throwable) {
Objects.requireNonNull(throwable, "throwable cannot be null");
Throwable rootCause = throwable;
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
final Set<Throwable> visited = Collections.newSetFromMap(new IdentityHashMap<>());
visited.add(rootCause);
Throwable cause = rootCause.getCause();
while (cause != null && visited.add(cause)) {
rootCause = cause;
cause = rootCause.getCause();
}
return rootCause;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import kotlin.test.assertNotNull
import kotlin.test.assertNull

class DuplicateEventDetectionEventProcessorTest {
private class CircularCauseThrowable : RuntimeException() {
private var nextCause: Throwable? = null
private var causeReads = 0

fun linkTo(cause: Throwable) {
nextCause = cause
}

override val cause: Throwable?
get() {
check(causeReads++ < 10) { "Throwable cause cycle was not detected" }
return nextCause
}
}

class Fixture {
fun getSut(enableDeduplication: Boolean? = null): DuplicateEventDetectionEventProcessor {
val options =
Expand Down Expand Up @@ -99,6 +114,19 @@ class DuplicateEventDetectionEventProcessorTest {
assertNull(result)
}

@Test
fun `does not loop indefinitely for cyclic cause chain`() {
val processor = fixture.getSut()
val first = CircularCauseThrowable()
val second = CircularCauseThrowable()
first.linkTo(second)
second.linkTo(first)

val result = processor.process(SentryEvent(first), Hint())

assertNotNull(result)
}

@Test
fun `does not deduplicate is deduplication is disabled`() {
val processor = fixture.getSut(enableDeduplication = false)
Expand Down
25 changes: 25 additions & 0 deletions sentry/src/test/java/io/sentry/util/ExceptionUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,29 @@ class ExceptionUtilsTest {
ExceptionUtils.rethrowIfFatal(RuntimeException())
assertFalse(Thread.currentThread().isInterrupted)
}

@Test
fun `does not loop indefinitely for cyclic cause chain`() {
val first = CircularCauseThrowable()
val second = CircularCauseThrowable()
first.linkTo(second)
second.linkTo(first)

assertThat(ExceptionUtils.findRootCause(first)).isSameInstanceAs(second)
}

private class CircularCauseThrowable : RuntimeException() {
private var nextCause: Throwable? = null
private var causeReads = 0

fun linkTo(cause: Throwable) {
nextCause = cause
}

override val cause: Throwable?
get() {
check(causeReads++ < 10) { "Throwable cause cycle was not detected" }
return nextCause
}
}
}
Loading