1212import io .sentry .util .Objects ;
1313import io .sentry .util .Pair ;
1414import java .io .Closeable ;
15+ import java .lang .ref .WeakReference ;
1516import java .util .Collections ;
1617import java .util .Date ;
1718import 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