Skip to content

Conversation

@ashtarkb
Copy link

What does this PR do?

The spans list in TraceContext was defined as a class-level attribute, causing all TraceContext instances to share the same list. This led to trace corruption when handling concurrent requests, as spans from different requests would be mixed together.

The bug:
class TraceContext:
spans: list[Span] = [] # Shared by ALL instances!

The fix:
Moving spans initialization to __init__ ensures each TraceContext instance has its own isolated spans list:

class TraceContext:
    def __init__(self, logger: BackgroundLogger, trace_id: str):
        self.logger = logger
        self.trace_id = trace_id
        self.spans: list[Span] = []  # Instance-levelCloses #4431

Test Plan

  1. Deploy LlamaStack with telemetry enabled (OpenTelemetry Collector)
  2. Send 3 concurrent requests using the reproduction script from TraceContext uses shared mutable class attribute causing trace corruption #4431
  3. Verify each request creates its own isolated trace (3 traces instead of 1 merged trace with 34 spans)

Before fix: 3 concurrent requests → 1 merged trace with 34 spans
After fix: 3 concurrent requests → 3 separate traces with ~10 spans each

The spans list was defined as a class-level attribute, causing all
TraceContext instances to share the same list. This led to trace
corruption when handling concurrent requests, as spans from different
requests would be mixed together.

Moving spans initialization to __init__ ensures each TraceContext
instance has its own isolated spans list.

Fixes trace mixing bug in concurrent request handling.
@meta-cla
Copy link

meta-cla bot commented Dec 28, 2025

Hi @ashtarkb!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 29, 2025
@meta-cla
Copy link

meta-cla bot commented Dec 29, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@nathan-weinberg nathan-weinberg left a comment

Choose a reason for hiding this comment

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

This is a good catch - thanks for the fix!

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

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants