1111import io .sentry .util .CollectionUtils ;
1212import io .sentry .util .EventProcessorUtils ;
1313import io .sentry .util .ExceptionUtils ;
14+ import io .sentry .util .LazyEvaluator ;
1415import io .sentry .util .Objects ;
1516import io .sentry .util .Pair ;
1617import java .lang .ref .WeakReference ;
@@ -93,7 +94,7 @@ public final class Scope implements IScope {
9394 /** Scope's attachments */
9495 private @ NotNull List <Attachment > attachments = new CopyOnWriteArrayList <>();
9596
96- private @ NotNull PropagationContext propagationContext ;
97+ private @ NotNull LazyEvaluator < PropagationContext > propagationContext ;
9798
9899 /** Scope's session replay id */
99100 private @ NotNull SentryId replayId = SentryId .EMPTY_ID ;
@@ -111,7 +112,14 @@ public final class Scope implements IScope {
111112 public Scope (final @ NotNull SentryOptions options ) {
112113 this .options = Objects .requireNonNull (options , "SentryOptions is required." );
113114 this .breadcrumbs = createBreadcrumbsList (this .options .getMaxBreadcrumbs ());
114- this .propagationContext = new PropagationContext ();
115+ this .propagationContext =
116+ new LazyEvaluator <>(
117+ new LazyEvaluator .Evaluator <PropagationContext >() {
118+ @ Override
119+ public @ NotNull PropagationContext evaluate () {
120+ return new PropagationContext ();
121+ }
122+ });
115123 this .lastEventId = SentryId .EMPTY_ID ;
116124 }
117125
@@ -124,6 +132,15 @@ private Scope(final @NotNull Scope scope) {
124132 this .client = scope .client ;
125133 this .lastEventId = scope .getLastEventId ();
126134
135+ this .propagationContext =
136+ new LazyEvaluator <>(
137+ new LazyEvaluator .Evaluator <PropagationContext >() {
138+ @ Override
139+ public @ NotNull PropagationContext evaluate () {
140+ return new PropagationContext ();
141+ }
142+ });
143+
127144 final User userRef = scope .user ;
128145 this .user = userRef != null ? new User (userRef ) : null ;
129146 this .screen = scope .screen ;
@@ -173,7 +190,7 @@ private Scope(final @NotNull Scope scope) {
173190
174191 this .attachments = new CopyOnWriteArrayList <>(scope .attachments );
175192
176- this .propagationContext = new PropagationContext (scope .propagationContext );
193+ this .propagationContext . setValue ( new PropagationContext (scope .propagationContext . getValue ()) );
177194 }
178195
179196 /**
@@ -1065,7 +1082,7 @@ public void clearSession() {
10651082 @ ApiStatus .Internal
10661083 @ Override
10671084 public void setPropagationContext (final @ NotNull PropagationContext propagationContext ) {
1068- this .propagationContext = propagationContext ;
1085+ this .propagationContext . setValue ( propagationContext ) ;
10691086
10701087 final @ NotNull SpanContext spanContext = propagationContext .toSpanContext ();
10711088 for (final IScopeObserver observer : options .getScopeObservers ()) {
@@ -1076,16 +1093,17 @@ public void setPropagationContext(final @NotNull PropagationContext propagationC
10761093 @ ApiStatus .Internal
10771094 @ Override
10781095 public @ NotNull PropagationContext getPropagationContext () {
1079- return propagationContext ;
1096+ return propagationContext . getValue () ;
10801097 }
10811098
10821099 @ ApiStatus .Internal
10831100 @ Override
10841101 public @ NotNull PropagationContext withPropagationContext (
10851102 final @ NotNull IWithPropagationContext callback ) {
10861103 try (final @ NotNull ISentryLifecycleToken ignored = propagationContextLock .acquire ()) {
1087- callback .accept (propagationContext );
1088- return new PropagationContext (propagationContext );
1104+ final @ NotNull PropagationContext context = getPropagationContext ();
1105+ callback .accept (context );
1106+ return new PropagationContext (context );
10891107 }
10901108 }
10911109
0 commit comments