fix(langfuse): close root context during shutdown - #43677
Open
bkutasi wants to merge 1 commit into
Open
Conversation
Contributor
|
Verification comment from automated review Reviewed the diff — this correctly addresses a real resource leak. The plugin calls The fix is well-structured:
No issues found. LGTM. |
Contributor
|
Thanks for targeting a real lifecycle leak: current main still manually enters the Langfuse root context in Problems
Suggested changes
This is an automated hermes-sweeper review. |
bkutasi
force-pushed
the
fix/langfuse-shutdown-cleanup
branch
from
July 15, 2026 15:31
c7728c9 to
ef0213e
Compare
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.
What does this PR do?
Closes the Langfuse root trace context during plugin shutdown and adds an
atexithandler to clean up traces still open when the Python interpreter exits. Previously the plugin manually entered the root context viaroot_ctx.__enter__()in_start_root_tracebut never calledroot_ctx.__exit__(), leaving OpenTelemetry's active span context attached until interpreter teardown — at which point module globals may already be torn down. This caused noisyException ignored in: <generator object Langfuse._create_span_with_parent_context ...>andTypeError: isinstance() arg 2 must be a type...errors on exit.Related Issue
No related issue; this is a bug fix identified from noisy shutdown logs.
Type of Change
Changes Made
plugins/observability/langfuse/__init__.pyimport atexitat module level._close_root_context(state)helper — readsstate.root_ctx, sets it toNonebefore calling__exit__to guarantee single-close semantics, then callsroot_ctx.__exit__(None, None, None)to detach the OpenTelemetry active span context._shutdown_active_traces()— iterates all openTraceStateentries under the lock, ends any active observations and root spans, closes root contexts, and flushes the Langfuse client._finish_tracenow calls_close_root_context(state)in itsfinallyblock so the context is always cleaned up on normal trace completion.atexit.register(_shutdown_active_traces)is at module level (not insideregister()) so the handler is registered as soon as the module is imported.tests/plugins/test_langfuse_plugin.pyfrom types import SimpleNamespace.TestRootContextCleanuptest class with two test methods.How to Test
pytest tests/plugins/test_langfuse_plugin.py::TestRootContextCleanup -vpytest tests/plugins/test_langfuse_plugin.py -q— all 43 tests should pass.ruff check plugins/observability/langfuse/__init__.py tests/plugins/test_langfuse_plugin.pyandpython -m py_compile plugins/observability/langfuse/__init__.py— both should pass.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qsuite (not run for this PR; see verification above)Documentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs
Before fix (noisy exceptions on interpreter shutdown):
After fix: clean shutdown, no exceptions.