Skip to content

Commit 2bdf631

Browse files
authored
fix(logger): copy thread-local keys in thread_safe_remove_keys (#8772)
remove_context_keys popped keys out of the dict held by the THREAD_LOCAL_KEYS ContextVar in place. asyncio tasks and asyncio.to_thread run in a copy of the caller's context, and that copy points at the same dict, so removing a key in one of them removed it for the caller and every other task that copied it. thread_safe_append_keys and thread_safe_clear_keys already set a new dict. thread_safe_remove_keys now does the same.
1 parent 7a54af5 commit 2bdf631

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎aws_lambda_powertools/logging/formatter.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ def set_context_keys(**kwargs: dict[str, Any]) -> None:
490490

491491
def remove_context_keys(keys: Iterable[str]) -> None:
492492
context = _get_context()
493-
context_values = context.get()
493+
# Copy instead of mutating in place: asyncio tasks and copied contexts share this dict object
494+
context_values = dict(context.get())
494495

495496
for k in keys:
496497
context_values.pop(k, None)

‎tests/functional/logger/required_dependencies/test_logger_powertools_formatter.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import asyncio
56
import io
67
import json
78
import os
@@ -486,6 +487,32 @@ def send_message_with_key_and_without(message, keys):
486487
assert logs[1].get("exampleThread1Key") is None
487488

488489

490+
def test_thread_safe_remove_keys_does_not_affect_tasks_sharing_the_context(service_name, stdout):
491+
logger = Logger(
492+
service=service_name,
493+
stream=stdout,
494+
)
495+
496+
async def process_order():
497+
await asyncio.sleep(0)
498+
logger.info("processing order")
499+
500+
async def main():
501+
logger.thread_safe_append_keys(order_id="order-1")
502+
task = asyncio.create_task(process_order()) # the task runs in a copy of the current context
503+
logger.thread_safe_remove_keys(["order_id"])
504+
await task
505+
logger.info("caller after removing the key")
506+
507+
asyncio.run(main())
508+
509+
logs = capture_logging_output(stdout)
510+
511+
assert logs[0].get("message") == "processing order"
512+
assert logs[0].get("order_id") == "order-1"
513+
assert logs[1].get("order_id") is None
514+
515+
489516
def test_thread_safe_clear_key(service_name, stdout):
490517
logger = Logger(
491518
service=service_name,

0 commit comments

Comments
 (0)