Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix memory leak caused by throwableToSpan ([#2227](https://github.com/getsentry/sentry-java/pull/2227))

## 6.4.0

### Fixes
Expand Down
27 changes: 17 additions & 10 deletions sentry/src/main/java/io/sentry/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.sentry.util.Objects;
import io.sentry.util.Pair;
import java.io.Closeable;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand All @@ -27,7 +28,7 @@ public final class Hub implements IHub {
private volatile boolean isEnabled;
private final @NotNull Stack stack;
private final @NotNull TracesSampler tracesSampler;
private final @NotNull Map<Throwable, Pair<ISpan, String>> throwableToSpan =
private final @NotNull Map<Throwable, Pair<WeakReference<ISpan>, String>> throwableToSpan =
Collections.synchronizedMap(new WeakHashMap<>());

public Hub(final @NotNull SentryOptions options) {
Expand Down Expand Up @@ -237,12 +238,15 @@ public boolean isEnabled() {

private void assignTraceContext(final @NotNull SentryEvent event) {
if (options.isTracingEnabled() && event.getThrowable() != null) {
final Pair<ISpan, String> pair =
final Pair<WeakReference<ISpan>, String> pair =
throwableToSpan.get(ExceptionUtils.findRootCause(event.getThrowable()));
if (pair != null) {
final ISpan span = pair.getFirst();
if (event.getContexts().getTrace() == null && span != null) {
event.getContexts().setTrace(span.getSpanContext());
final WeakReference<ISpan> spanWeakRef = pair.getFirst();
if (event.getContexts().getTrace() == null && spanWeakRef != null) {
final ISpan span = spanWeakRef.get();
if (span != null) {
event.getContexts().setTrace(span.getSpanContext());
}
}
final String transactionName = pair.getSecond();
if (event.getTransaction() == null && transactionName != null) {
Expand Down Expand Up @@ -775,19 +779,22 @@ public void setSpanContext(
final Throwable rootCause = ExceptionUtils.findRootCause(throwable);
// the most inner span should be assigned to a throwable
if (!throwableToSpan.containsKey(rootCause)) {
throwableToSpan.put(rootCause, new Pair<>(span, transactionName));
throwableToSpan.put(rootCause, new Pair<>(new WeakReference<>(span), transactionName));
}
}

@Nullable
SpanContext getSpanContext(final @NotNull Throwable throwable) {
Objects.requireNonNull(throwable, "throwable is required");
final Throwable rootCause = ExceptionUtils.findRootCause(throwable);
final Pair<ISpan, String> pair = this.throwableToSpan.get(rootCause);
final Pair<WeakReference<ISpan>, String> pair = this.throwableToSpan.get(rootCause);
if (pair != null) {
final ISpan span = pair.getFirst();
if (span != null) {
return span.getSpanContext();
final WeakReference<ISpan> spanWeakRef = pair.getFirst();
if (spanWeakRef != null) {
final ISpan span = spanWeakRef.get();
if (span != null) {
return span.getSpanContext();
}
}
}
return null;
Expand Down