From 9324451693c864a3bb8eebb6deed898d0d16a299 Mon Sep 17 00:00:00 2001 From: nnNyx <64274427+nnNyx@users.noreply.github.com> Date: Sat, 4 Jul 2026 01:55:09 +0200 Subject: [PATCH 1/2] [fix]: core/schemas - make trace/span helpers nil-safe to prevent streaming error panic A Bedrock streaming error finalizer could hit a nil span entry (or nil receiver) in the tracing path (completeDeferredSpan -> Trace.GetSpan), panicking the process instead of forwarding the stream error. Affected packages: - core/schemas/trace.go Fixes #3455 Co-Authored-By: Claude Fable 5 --- core/schemas/trace.go | 17 ++++++++++++++++- core/schemas/trace_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 core/schemas/trace_test.go diff --git a/core/schemas/trace.go b/core/schemas/trace.go index d3b6cb0e439..2e2c32f5041 100644 --- a/core/schemas/trace.go +++ b/core/schemas/trace.go @@ -36,6 +36,9 @@ const ( // AddSpan adds a span to the trace in a thread-safe manner func (t *Trace) AddSpan(span *Span) { + if t == nil || span == nil { + return + } t.mu.Lock() defer t.mu.Unlock() t.Spans = append(t.Spans, span) @@ -43,9 +46,15 @@ func (t *Trace) AddSpan(span *Span) { // GetSpan retrieves a span by ID func (t *Trace) GetSpan(spanID string) *Span { + if t == nil || spanID == "" { + return nil + } t.mu.Lock() defer t.mu.Unlock() for _, span := range t.Spans { + if span == nil { + continue + } if span.SpanID == spanID { return span } @@ -146,7 +155,7 @@ type Span struct { // SetAttribute sets an attribute on the span in a thread-safe manner func (s *Span) SetAttribute(key string, value any) { - if value == nil { + if s == nil || value == nil { return } s.mu.Lock() @@ -159,6 +168,9 @@ func (s *Span) SetAttribute(key string, value any) { // AddEvent adds an event to the span in a thread-safe manner func (s *Span) AddEvent(event SpanEvent) { + if s == nil { + return + } s.mu.Lock() defer s.mu.Unlock() s.Events = append(s.Events, event) @@ -166,6 +178,9 @@ func (s *Span) AddEvent(event SpanEvent) { // End marks the span as complete with the given status func (s *Span) End(status SpanStatus, statusMsg string) { + if s == nil { + return + } s.mu.Lock() defer s.mu.Unlock() s.EndTime = time.Now() diff --git a/core/schemas/trace_test.go b/core/schemas/trace_test.go new file mode 100644 index 00000000000..fe565e9b0c3 --- /dev/null +++ b/core/schemas/trace_test.go @@ -0,0 +1,34 @@ +package schemas + +import "testing" + +func TestTraceGetSpanNilSafe(t *testing.T) { + var nilTrace *Trace + if span := nilTrace.GetSpan("span"); span != nil { + t.Fatalf("nil trace GetSpan returned %v, want nil", span) + } + + trace := &Trace{Spans: []*Span{nil, &Span{SpanID: "target"}}} + if span := trace.GetSpan("missing"); span != nil { + t.Fatalf("missing span = %v, want nil", span) + } + if span := trace.GetSpan("target"); span == nil || span.SpanID != "target" { + t.Fatalf("target span = %v, want target", span) + } +} + +func TestTraceAndSpanNilMutatorsNoop(t *testing.T) { + trace := &Trace{} + trace.AddSpan(nil) + if len(trace.Spans) != 0 { + t.Fatalf("nil span was appended: %v", trace.Spans) + } + + var nilTrace *Trace + nilTrace.AddSpan(&Span{SpanID: "ignored"}) + + var nilSpan *Span + nilSpan.SetAttribute("key", "value") + nilSpan.AddEvent(SpanEvent{Name: "event"}) + nilSpan.End(SpanStatusOk, "") +} From 38259c55e55e1f5f6fb619a8aae3753ccd3bb4f5 Mon Sep 17 00:00:00 2001 From: nnNyx <64274427+nnNyx@users.noreply.github.com> Date: Sat, 4 Jul 2026 02:07:30 +0200 Subject: [PATCH 2/2] [test]: core/schemas - cover empty span ID guard in GetSpan Addresses greptile review on PR #4896. Co-Authored-By: Claude Fable 5 --- core/schemas/trace_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/schemas/trace_test.go b/core/schemas/trace_test.go index fe565e9b0c3..48f374d4a73 100644 --- a/core/schemas/trace_test.go +++ b/core/schemas/trace_test.go @@ -9,6 +9,9 @@ func TestTraceGetSpanNilSafe(t *testing.T) { } trace := &Trace{Spans: []*Span{nil, &Span{SpanID: "target"}}} + if span := trace.GetSpan(""); span != nil { + t.Fatalf("empty span ID = %v, want nil", span) + } if span := trace.GetSpan("missing"); span != nil { t.Fatalf("missing span = %v, want nil", span) }