Skip to content

Commit 88fade9

Browse files
authored
Fix memory leak caused by throwableToSpan (#2227)
1 parent d425298 commit 88fade9

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fix memory leak caused by throwableToSpan ([#2227](https://github.com/getsentry/sentry-java/pull/2227))
8+
39
## 6.4.0
410

511
### Fixes

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.sentry.util.Objects;
1313
import io.sentry.util.Pair;
1414
import java.io.Closeable;
15+
import java.lang.ref.WeakReference;
1516
import java.util.Collections;
1617
import java.util.Date;
1718
import java.util.List;
@@ -27,7 +28,7 @@ public final class Hub implements IHub {
2728
private volatile boolean isEnabled;
2829
private final @NotNull Stack stack;
2930
private final @NotNull TracesSampler tracesSampler;
30-
private final @NotNull Map<Throwable, Pair<ISpan, String>> throwableToSpan =
31+
private final @NotNull Map<Throwable, Pair<WeakReference<ISpan>, String>> throwableToSpan =
3132
Collections.synchronizedMap(new WeakHashMap<>());
3233

3334
public Hub(final @NotNull SentryOptions options) {
@@ -237,12 +238,15 @@ public boolean isEnabled() {
237238

238239
private void assignTraceContext(final @NotNull SentryEvent event) {
239240
if (options.isTracingEnabled() && event.getThrowable() != null) {
240-
final Pair<ISpan, String> pair =
241+
final Pair<WeakReference<ISpan>, String> pair =
241242
throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable()));
242243
if (pair != null) {
243-
final ISpan span = pair.getFirst();
244-
if (event.getContexts().getTrace() == null && span != null) {
245-
event.getContexts().setTrace(span.getSpanContext());
244+
final WeakReference<ISpan> spanWeakRef = pair.getFirst();
245+
if (event.getContexts().getTrace() == null && spanWeakRef != null) {
246+
final ISpan span = spanWeakRef.get();
247+
if (span != null) {
248+
event.getContexts().setTrace(span.getSpanContext());
249+
}
246250
}
247251
final String transactionName = pair.getSecond();
248252
if (event.getTransaction() == null && transactionName != null) {
@@ -775,19 +779,22 @@ public void setSpanContext(
775779
final Throwable rootCause = ExceptionUtils.findRootCause(throwable);
776780
// the most inner span should be assigned to a throwable
777781
if (!throwableToSpan.containsKey(rootCause)) {
778-
throwableToSpan.put(rootCause, new Pair<>(span, transactionName));
782+
throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName));
779783
}
780784
}
781785

782786
@Nullable
783787
SpanContext getSpanContext(final @NotNull Throwable throwable) {
784788
Objects.requireNonNull(throwable, "throwable is required");
785789
final Throwable rootCause = ExceptionUtils.findRootCause(throwable);
786-
final Pair<ISpan, String> pair = this.throwableToSpan.get(rootCause);
790+
final Pair<WeakReference<ISpan>, String> pair = this.throwableToSpan.get(rootCause);
787791
if (pair != null) {
788-
final ISpan span = pair.getFirst();
789-
if (span != null) {
790-
return span.getSpanContext();
792+
final WeakReference<ISpan> spanWeakRef = pair.getFirst();
793+
if (spanWeakRef != null) {
794+
final ISpan span = spanWeakRef.get();
795+
if (span != null) {
796+
return span.getSpanContext();
797+
}
791798
}
792799
}
793800
return null;

0 commit comments

Comments
 (0)