22using System . Linq ;
33using System . Runtime . ExceptionServices ;
44using System . Text ;
5+ using System . Threading ;
56using System . Threading . Tasks ;
67using AspectInjector . Broker ;
78using AWS . Lambda . Powertools . Common ;
@@ -28,14 +29,15 @@ public class TracingAspect
2829 private readonly IXRayRecorder _xRayRecorder ;
2930
3031 /// <summary>
31- /// If true, capture annotations
32+ /// Thread-safe flag for capturing annotations. Uses int for Interlocked operations.
33+ /// 1 = should capture, 0 = already captured
3234 /// </summary>
33- private static bool _captureAnnotations = true ;
35+ private static int _captureAnnotations = 1 ;
3436
3537 /// <summary>
36- /// If true, annotations have been captured
38+ /// If true, annotations have been captured by this invocation's execution context
3739 /// </summary>
38- private bool _isAnnotationsCaptured ;
40+ private static readonly AsyncLocal < bool > _isAnnotationsCaptured = new ( ) ;
3941
4042 /// <summary>
4143 /// Aspect constructor
@@ -117,8 +119,12 @@ public object Around(
117119 }
118120 finally
119121 {
120- if ( _isAnnotationsCaptured )
121- _captureAnnotations = true ;
122+ // Reset the capture flag if this execution context captured annotations
123+ if ( _isAnnotationsCaptured . Value )
124+ {
125+ Interlocked . Exchange ( ref _captureAnnotations , 1 ) ;
126+ _isAnnotationsCaptured . Value = false ;
127+ }
122128 }
123129 }
124130
@@ -127,12 +133,12 @@ private void BeginSegment(string segmentName, string @namespace)
127133 _xRayRecorder . BeginSubsegment ( segmentName ) ;
128134 _xRayRecorder . SetNamespace ( @namespace ) ;
129135
130- if ( _captureAnnotations )
136+ // Use Interlocked.CompareExchange for thread-safe check-and-set
137+ // Only one thread will successfully change from 1 to 0
138+ if ( Interlocked . CompareExchange ( ref _captureAnnotations , 0 , 1 ) == 1 )
131139 {
132140 _xRayRecorder . AddAnnotation ( "ColdStart" , LambdaLifecycleTracker . IsColdStart ) ;
133-
134- _captureAnnotations = false ;
135- _isAnnotationsCaptured = true ;
141+ _isAnnotationsCaptured . Value = true ;
136142
137143 if ( _powertoolsConfigurations . IsServiceDefined )
138144 _xRayRecorder . AddAnnotation ( "Service" , _powertoolsConfigurations . Service ) ;
@@ -231,6 +237,7 @@ private bool CaptureError(TracingCaptureMode captureMode)
231237 internal static void ResetForTest ( )
232238 {
233239 LambdaLifecycleTracker . Reset ( ) ;
234- _captureAnnotations = true ;
240+ Interlocked . Exchange ( ref _captureAnnotations , 1 ) ;
241+ _isAnnotationsCaptured . Value = false ;
235242 }
236243}
0 commit comments