Skip to content

Commit f7855f6

Browse files
Guard Activity.Current in GXSpanContext() to avoid NullReferenceException (#1291)
The parameterless GXSpanContext ctor dereferenced Activity.Current.Context with no null check. When there is no active span — e.g. reading the current span outside any started Activity, or in a CLI/batch procedure run (GXProcedure. MainImpl) where no ambient span exists — Activity.Current is null and the ctor threw a NullReferenceException (seen via SdtSpanContext.get_gxTpr_Spanid -> new GXSpanContext()). Use Activity.Current?.Context ?? default so an empty (all-zero) ActivityContext is produced instead of crashing; TraceId/SpanId then return zeros rather than throwing. Latent since the OpenTelemetry Spans API was added (#975), unrelated to the recent dependency bump. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a192187 commit f7855f6

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

  • dotnet/src/dotnetcore/Providers/OpenTelemetry/Diagnostics/GXOtel.Diagnostics

dotnet/src/dotnetcore/Providers/OpenTelemetry/Diagnostics/GXOtel.Diagnostics/OtelSpan.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ public class GXSpanContext
118118
private ActivityContext _activityContext;
119119
public GXSpanContext()
120120
{
121-
_activityContext = Activity.Current.Context;
121+
// Activity.Current is null when there is no active span (e.g. reading the current
122+
// span outside any started Activity, or in a CLI/batch run with no span). Guard it
123+
// so building a context yields an empty (all-zero) ActivityContext instead of an NRE.
124+
_activityContext = Activity.Current?.Context ?? default;
122125
}
123126

124127
public GXSpanContext(ActivityContext activityContext)

0 commit comments

Comments
 (0)