fix(activity): do not mutate caller-provided extra dict in LoggerAdapter#1513
Open
1fanwang wants to merge 2 commits into
Open
fix(activity): do not mutate caller-provided extra dict in LoggerAdapter#15131fanwang wants to merge 2 commits into
1fanwang wants to merge 2 commits into
Conversation
The activity LoggerAdapter mutated the caller-provided ``extra`` mapping in place when injecting ``temporal_activity`` and ``activity_info``. If the caller reused the dict across log calls (or held a reference to it elsewhere), Temporal context would leak back into their state. Copy the caller's extra into a fresh dict before adding our keys. This matches what the workflow LoggerAdapter already does via ``_build_log_context``. Fixes temporalio#503 Signed-off-by: 1fanwang <1fannnw@gmail.com>
|
|
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 was changed
temporalio.activity._ContextLoggerAdapter.processretrieves the caller'sextradict fromkwargsand writestemporal_activity/activity_infokeys directly into it before forwarding to the wrapped logger. When the caller reuses that same dict elsewhere, Temporal's injected keys leak into their state.The fix is
extra = dict(kwargs.get("extra") or {})— copy once before mutating. The siblingworkflow.pyLoggerAdapter already does this correctly via_build_log_context.Why
Caller-side reproducer:
A user found this in production where their
extradict was a long-lived per-request bag of context that downstream code re-emitted to a separate log sink — Temporal's keys ended up in every other log line for that request.Tested
New parametrized regression
test_activity_logger_does_not_mutate_caller_extraintests/testing/test_activity.py. Asserts the caller dict is byte-equal to its pre-call state and that the logged record still has the Temporal keys. Fails on main withAssertionError(the dict hastemporal_activityandactivity_infoinjected); passes with this PR.Fixes #503