Skip to content

Commit 4b6068d

Browse files
fix(profiling): track existing asyncio loop if it exists
1 parent ce216ac commit 4b6068d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

ddtrace/profiling/_asyncio.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ def _(asyncio):
5757
if THREAD_LINK is None:
5858
THREAD_LINK = _threading._ThreadLink()
5959

60-
init_stack_v2 = config.stack.v2_enabled and stack_v2.is_available
60+
init_stack_v2: bool = config.stack.v2_enabled and stack_v2.is_available
61+
62+
if init_stack_v2:
63+
try:
64+
running_loop = asyncio.get_running_loop()
65+
stack_v2.track_asyncio_loop(typing.cast(int, ddtrace_threading.current_thread().ident), running_loop)
66+
except RuntimeError:
67+
# No existing loop to track, continue
68+
pass
6169

6270
@partial(wrap, sys.modules["asyncio.events"].BaseDefaultEventLoopPolicy.set_event_loop)
6371
def _(f, args, kwargs):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fixes:
2+
- |
3+
profiling: this fix resolves an issue where importing the profiler module after an asyncio Event Loop had been
4+
started would make the Profiler blind to the existing Event Loop and its Tasks.

0 commit comments

Comments
 (0)