Skip to content

fix(tracing): don't let sibling spans overwrite each other - #737

Open
grempe wants to merge 1 commit into
aws:mainfrom
grempe:tracing-span-lineage-race
Open

grempe wants to merge 1 commit into
aws:mainfrom
grempe:tracing-span-lineage-race

Conversation

@grempe

@grempe grempe commented Oct 28, 2026

Copy link
Copy Markdown

Issue #, if available:

Fixes #736.

Description of changes:

tracing.WithSpan appended the new span to the parent context's span slice. When that slice had spare capacity, two spans started from the same parent wrote the same slot of one array, so the first child's context ended up holding the second child's span, and children started concurrently raced. In aws-sdk-go-v2 those children are DoHTTPRequest and OperationDeserializer, and the HTTP dialer reads the first in a goroutine of its own.

This caps the slice's capacity at its length before appending, so the append always copies. PopSpan only reslices, so no context now writes into an array that another context can see. The cost is that each WithSpan on a context that already has spans allocates a new array, a few interface values long.

Testing

  • New TestWithSpanSiblings starts two children from a parent three spans deep, without goroutines. On main it fails, the first child's context holding the second child's span (span 5 != 4); with the fix it passes.
  • New TestWithSpanSiblingsConcurrent starts eight children from that parent at once. On main, go test -race fails it with DATA RACE; with the fix, go test -race -count=20 ./tracing/ passes.
  • make unit-race, which vets every module and runs its tests under -race, passes.
  • End to end, in a service that traces its S3 calls through smithyoteltracing: our race test, which fails under -race against v1.28.6 without our workaround, passes against this branch. After an S3 GetObject, PutObject, HeadObject or ListObjectsV2, the sent request's context now holds DoHTTPRequest, where on v1.28.7 it holds OperationDeserializer.

The changelog entry is .changelog/dd9b2f7f-8087-4a3c-97bc-d24ff84f79ec.json.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


Reviewed and submitted by me, a human. The investigation, the fix and its testing were developed with the assistance of an AI coding agent.

WithSpan appended the new span to the lineage slice from the parent
context. When that slice had spare capacity, two spans started from
the same parent wrote the same slot of one backing array, so each
sibling's context saw whichever span was written last, and concurrent
callers raced. In aws-sdk-go-v2 this shows up as a data race between
the HTTP dialer's GetSpan and the deserialize middleware's StartSpan
within one operation.

Cap the slice's capacity at its length before appending, so the append
always copies.
@grempe
grempe requested review from a team as code owners October 28, 2026 01:07

@blacksoxx blacksoxx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the diff against main; I did not run the package here, so these are reading notes.

The full slice expression is the right tool: lineage[:len(lineage):len(lineage)] leaves no spare capacity, so append has to copy and the parent's backing array is never written through a child. PopSpan only reslices, so it stays safe, and the price is one small allocation per WithSpan on a context that already carries spans, which is what the description says. TestWithSpanSiblings reproduces the overwrite exactly as the issue describes it (three spans deep, room for a fourth, two children), and the concurrent test is what -race needs to see the write.

Nothing blocking from my side; the changelog entry follows the usual shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tracing: WithSpan writes into the parent context's span slice, so sibling spans overwrite each other and race

2 participants