Skip to content

fix(activity): do not mutate caller-provided extra dict in LoggerAdapter#1513

Open
1fanwang wants to merge 2 commits into
temporalio:mainfrom
1fanwang:fix/activity-logger-no-mutate-extra
Open

fix(activity): do not mutate caller-provided extra dict in LoggerAdapter#1513
1fanwang wants to merge 2 commits into
temporalio:mainfrom
1fanwang:fix/activity-logger-no-mutate-extra

Conversation

@1fanwang
Copy link
Copy Markdown

What was changed

temporalio.activity._ContextLoggerAdapter.process retrieves the caller's extra dict from kwargs and writes temporal_activity / activity_info keys 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 sibling workflow.py LoggerAdapter already does this correctly via _build_log_context.

Why

Caller-side reproducer:

shared = {"caller": "value"}
activity.logger.info("step", extra=shared)
assert shared == {"caller": "value"}  # fails on main, passes with this PR

A user found this in production where their extra dict 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_extra in tests/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 with AssertionError (the dict has temporal_activity and activity_info injected); passes with this PR.

pytest tests/testing/test_activity.py -xvs
... 7 passed ...
ruff check / format — clean
pyright temporalio/activity.py — clean

Fixes #503

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>
@1fanwang 1fanwang requested a review from a team as a code owner May 12, 2026 08:19
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 12, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

[Bug] Do not mutate extra in activity/workflow loggers

2 participants