Skip to content

Commit 4f9b78d

Browse files
committed
Bug Fix: Defensively copy context entities
Before this change, concurrent async tasks would all share the same instance of the entities list. This change makes it so they each get their own copy of the list. This matters because the recorder modifies the list in place, which makes it so concurrent subtasks have the wrong parent subsegment.
1 parent 816ab26 commit 4f9b78d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

aws_xray_sdk/core/async_context.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import sys
3+
import copy
34

45
from .context import Context as _Context
56

@@ -108,6 +109,13 @@ def task_factory(loop, coro):
108109
else:
109110
current_task = asyncio.Task.current_task(loop=loop)
110111
if current_task is not None and hasattr(current_task, 'context'):
111-
setattr(task, 'context', current_task.context)
112+
if current_task.context['entities']:
113+
# Defensively copying because recorder modifies the list in place.
114+
new_context = copy.copy(current_task.context)
115+
new_context['entities'] = [item for item in current_task.context['entities']]
116+
else:
117+
# no reason to copy if there's no entities list.
118+
new_context = current_task.context
119+
setattr(task, 'context', new_context)
112120

113121
return task

0 commit comments

Comments
 (0)