fix: re-attach OTel span context in async test functions#1693
Merged
jirikuncar merged 4 commits intomainfrom Feb 9, 2026
Merged
fix: re-attach OTel span context in async test functions#1693jirikuncar merged 4 commits intomainfrom
jirikuncar merged 4 commits intomainfrom
Conversation
…yfunc_call hook On Python 3.11+, when a module/session-scoped async fixture keeps anyio's runner task alive across tests, subsequent async tests inherit the first test's contextvars snapshot. This causes logfire.get_context() to return a stale traceparent and child spans to be mis-parented under the wrong test. Use a pytest_pyfunc_call hookwrapper that wraps async test functions with functools.wraps to call context_api.attach() inside the coroutine body. This is safer than an async autouse fixture, which breaks sync-only test suites with PytestRemovedIn9Warning (will become a hard error in pytest 9).
Deploying logfire-docs with
|
| Latest commit: |
cc5731e
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://cd9e95e0.logfire-docs.pages.dev |
| Branch Preview URL: | https://fix-pytest-async.logfire-docs.pages.dev |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes OpenTelemetry context propagation for async pytest tests by re-attaching the per-test span context inside the coroutine body, preventing stale/empty contextvars snapshots (notably on Python 3.11+ with reused anyio runner tasks) from causing mis-parented spans and missing traceparent.
Changes:
- Add a
pytest_pyfunc_callhookwrapper that wraps async test functions and callsopentelemetry.context.attach()inside the coroutine. - Add regression tests ensuring async tests see the correct current span /
traceparentand that child spans are parented under the correct per-test span when runner tasks are reused.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
logfire/_internal/integrations/pytest.py |
Adds a pytest_pyfunc_call hookwrapper to re-attach the per-test OTel span context within async test coroutines. |
tests/otel_integrations/test_pytest_plugin.py |
Adds regression coverage for async context propagation, logfire.get_context() traceparent presence, and correct span parenting across multiple async tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
dmontagu
pushed a commit
that referenced
this pull request
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Python 3.11+, when a module/session-scoped async fixture keeps anyio's runner task alive across tests, subsequent async tests inherit the first test's contextvars snapshot. This causes
logfire.get_context()to return a stale traceparent and child spans to be mis-parented under the wrong test.Use a pytest_pyfunc_call hookwrapper that wraps async test functions with
functools.wrapsto callcontext_api.attach()inside the coroutine body.