Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(v1-branch): make LoggingContext safe for concurrent invocations [DEVC-2693] - #129

Merged
kossman merged 1 commit into
version-onefrom
fix/logging-context-thread-safety-v1
Oct 16, 2026
Merged

kossman merged 1 commit into
version-onefrom
fix/logging-context-thread-safety-v1

Conversation

@kossman

@kossman kossman commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Rationale

Backport of #128 to SDK 1.x.

With several invocations processed in threads of one process (OpenFaaS adapter in threads mode, the default), LoggingContext mixed 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-wide logger.handlers on enter and restoring it on exit. The bug is present in all SDK versions.

Changes

  • One permanent dispatch handler on the logger picks the handlers of the active LoggingContext per execution context (contextvars) instead of replacing logger.handlers.
  • Threads started by app code without a copied context fall back to the most recently entered active context, so their logs keep working as before.
  • Outside any context records go to logging.lastResort, same as for a logger without handlers.
  • Tests: two threads in both exit orders, nested contexts, threads with and without a copied context.
  • Tested on Python 3.10, 3.11, 3.12 and 3.13.
  • Version is not bumped here, release is a separate step.

JIRA ticket

TODO

  • Update CHANGELOG.md

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 threads mode). Previously LoggingContext replaced the shared logger.handlers on 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.

LoggingContext now registers its handler chain per execution context via a permanent _DispatchHandler on the logger and contextvars, instead of mutating logger.handlers. OTel handler attachment and dynamic asset_id updates behave the same inside a context. Worker threads that do not copy contextvars still fall back to the most recently active context; with no active context, records use logging.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.

…-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 kossman self-assigned this Oct 13, 2026
@kossman
kossman marked this pull request as ready for review October 14, 2026 10:55
@kossman
kossman merged commit 655387c into version-one Oct 16, 2026
14 checks passed
@kossman
kossman deleted the fix/logging-context-thread-safety-v1 branch October 16, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants