Skip to content

Conversation

vblagoje
Copy link
Member

Why:

Purpose: Fixes a race condition where spans from concurrent pipeline runs become intertwined, corrupting tracing data and making debugging unreliable in production web server environments.

What:

  • Replaced instance-level self._context list with ContextVar for thread-safe span stack management
  • Updated LangfuseTracer.trace() method to use context-local span storage
  • Modified current_span() method to read from context-local state
  • Added module-level span_context_var ContextVar for isolated span contexts per execution thread
  • Fixed support for session IDs as well

How can it be used:

Nothing changes in client code except that we don't corrupt traces in concurrent async pipelines. All existing usage patterns continue to work exactly as before, but now with proper thread safety.

How did you test it:

  • Added unit tests verifying context isolation between concurrent tracer instances
  • Verified that spans are properly isolated per execution context
  • Tested examples in examples directory and itinerary agent
  • Ran existing test suite to ensure no regressions in single-threaded scenarios

@github-actions github-actions bot added integration:langfuse type:documentation Improvements or additions to documentation labels Aug 21, 2025
@vblagoje
Copy link
Member Author

Replacing #2188

@vblagoje vblagoje marked this pull request as ready for review August 21, 2025 12:30
@vblagoje vblagoje requested a review from a team as a code owner August 21, 2025 12:30
@vblagoje vblagoje requested review from anakin87 and removed request for a team August 21, 2025 12:30
@anakin87 anakin87 requested a review from mpangrazzi August 21, 2025 12:36
@anakin87
Copy link
Member

anakin87 commented Aug 21, 2025

Asking @mpangrazzi a review since he was originally involved.

Let's also adjust the title.

@vblagoje vblagoje changed the title LangfuseTracer is now thread-safe, avoids mixed traces in async envs fix: Avoid mixed traces in async envs Aug 21, 2025
@vblagoje vblagoje changed the title fix: Avoid mixed traces in async envs fix: Avoid mixed Langfuse traces in async envs Aug 21, 2025
@LastRemote
Copy link
Contributor

LastRemote commented Aug 22, 2025

Screenshot 2025-08-22 at 14 23 31 Looks good! Tested on both simple pipelines and agent (nested) pipelines.

@vblagoje
Copy link
Member Author

I checked complex agents and examples pipelines. All good. Thanks a lot for the confirmation @LastRemote 💪

Copy link
Contributor

@mpangrazzi mpangrazzi left a comment

Choose a reason for hiding this comment

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

Looks good! 👍 I've left only a minor comment (note that I am not a Langfuse expert 😉)

if self._context and self._context[-1] == span:
self._context.pop()
# Restore previous span stack using saved token - ensures proper cleanup
span_stack_var.reset(token)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we safely assume this line will never throw an error?

Copy link
Member Author

Choose a reason for hiding this comment

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

No mention of it in pydocs anywhere. Seems like it can't happen

@vblagoje vblagoje merged commit d8cc57b into main Aug 25, 2025
10 checks passed
@vblagoje vblagoje deleted the concurrent_langfuse_nohier branch August 25, 2025 13:09
@vblagoje
Copy link
Member Author

Fixed and released in https://pypi.org/project/langfuse-haystack/2.3.0/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:langfuse type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Concurrency: LangfuseTracer is not thread-safe, causing mixed traces in async environments
4 participants