Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(langchain): detach existing SpanHolder token before overwrite in …
…_create_llm_span

Closes #3957

When _create_llm_span() is called for a run_id that already has an entry
in self.spans, the old holder's token was lost without being detached,
leaving an orphaned context_api.attach() on the OTel context stack. This
is the same class of bug as #3526 / #3807.

Defensively detach the existing holder's token before replacing the entry.
  • Loading branch information
saivedant169 committed Apr 12, 2026
commit c056d6fb0c075f86b9526f12929c27c137113179
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@ def _create_llm_span(
# If context setting fails, continue without suppression token
token = None

# Detach any existing holder's token before overwriting, otherwise
# the previous context_api.attach() is orphaned and corrupts the
# OTel context stack (same class of bug as #3526 / #3807).
existing_holder = self.spans.get(run_id)
if existing_holder is not None and existing_holder.token is not None:
self._safe_detach_context(existing_holder.token)

self.spans[run_id] = SpanHolder(
span, token, None, [], workflow_name, None, entity_path
)
Expand Down