Repository navigation
fix(v1-branch): make LoggingContext safe for concurrent invocations [DEVC-2693] - #129
Merged
Merged
Conversation
…-BRANCH] Backport of the SDK 2.x fix. LoggingContext replaced the process-wide `logger.handlers` on enter and restored it on exit. With several invocations processed in threads of one process (OpenFaaS adapter in `threads` mode, the default), logs got the ASSET, AC and request id of another invocation, and handlers leaked or got lost depending on the order the contexts exited. Install one permanent dispatch handler on the logger that picks the handlers of the active LoggingContext per execution context (contextvars). Threads started by app code without a copied context fall back to the most recently entered active context, which keeps their logs working as before. Outside any context records go to `logging.lastResort`, same as for a logger without handlers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
kossman
marked this pull request as ready for review
October 14, 2026 10:55
smoke
approved these changes
Oct 14, 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.
Rationale
Backport of #128 to SDK 1.x.
With several invocations processed in threads of one process (OpenFaaS adapter in
threadsmode, the default),LoggingContextmixed their logs: records got the ASSET, AC and request id of another invocation, and handlers leaked or got lost depending on the order the contexts exited. The cause was swapping the process-widelogger.handlerson enter and restoring it on exit. The bug is present in all SDK versions.Changes
LoggingContextper execution context (contextvars) instead of replacinglogger.handlers.logging.lastResort, same as for a logger without handlers.JIRA ticket
TODO
Note
Medium Risk
Changes core logging routing for every invocation and concurrent-thread edge cases; wrong dispatch could mislabel logs or drop handlers, though behavior is heavily covered by new tests.
Overview
Fixes cross-invocation log contamination when multiple requests run in threads in one process (e.g. OpenFaaS
threadsmode). PreviouslyLoggingContextreplaced the sharedlogger.handlerson enter and restored on exit, so concurrent contexts could pick up another invocation’s ASSET, AC, and request id, and handlers could leak or disappear depending on exit order.LoggingContextnow registers its handler chain per execution context via a permanent_DispatchHandleron the logger andcontextvars, instead of mutatinglogger.handlers. OTel handler attachment and dynamicasset_idupdates behave the same inside a context. Worker threads that do not copycontextvarsstill fall back to the most recently active context; with no active context, records uselogging.lastResort.Adds unit tests for concurrent contexts (both exit orders), nesting, worker threads with/without copied context, and updates the OTel assertion to check
context.handlers. CHANGELOG documents the fix under Unreleased.Reviewed by Cursor Bugbot for commit 9280cda. Configure here.