Skip to content

Commit aa52c00

Browse files
authored
fix(tracer): wrap asyncio only when active trace context (#13326)
See linked issue #8907 and release note for details. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 2d662ac commit aa52c00

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

ddtrace/contrib/internal/asyncio/patch.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def patch():
2020
return
2121
asyncio._datadog_patch = True
2222
Pin().onto(asyncio)
23-
wrap(asyncio.BaseEventLoop.create_task, _wrapped_create_task_py37)
23+
wrap(asyncio.BaseEventLoop.create_task, _wrapped_create_task)
2424

2525

2626
def unpatch():
@@ -29,23 +29,29 @@ def unpatch():
2929
if getattr(asyncio, "_datadog_patch", False):
3030
return
3131
asyncio._datadog_patch = False
32-
unwrap(asyncio.BaseEventLoop.create_task, _wrapped_create_task_py37)
32+
unwrap(asyncio.BaseEventLoop.create_task, _wrapped_create_task)
3333

3434

35-
def _wrapped_create_task_py37(wrapped, args, kwargs):
35+
def _wrapped_create_task(wrapped, args, kwargs):
3636
"""This function ensures the current active trace context is propagated to scheduled tasks.
3737
By default the trace context is propagated when a task is executed and NOT when it is created.
3838
"""
3939
pin = Pin.get_from(asyncio)
4040
if not pin or not pin.enabled():
4141
return wrapped(*args, **kwargs)
4242

43-
# override existing co-rountine to ensure the current trace context is propagated
44-
coro = get_argument_value(args, kwargs, 1, "coro")
43+
# Get current trace context
4544
dd_active = pin.tracer.current_trace_context()
45+
# Only wrap the coroutine if we have an active trace context
46+
if not dd_active:
47+
return wrapped(*args, **kwargs)
48+
49+
# Get the coroutine
50+
coro = get_argument_value(args, kwargs, 1, "coro")
4651

52+
# Wrap the coroutine and ensure the current trace context is propagated
4753
async def traced_coro(*args_c, **kwargs_c):
48-
if dd_active and dd_active != pin.tracer.current_trace_context():
54+
if dd_active != pin.tracer.current_trace_context():
4955
pin.tracer.context_provider.activate(dd_active)
5056
return await coro
5157

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
tracer: fixes ``RuntimeWarning`` from an unwaited coroutine during tab
5+
completion in IPython REPL when ``asyncio`` integration is active. Tracer
6+
now wraps an ``asyncio`` coroutine only when there is an active trace
7+
context.

tests/contrib/suitespec.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ components:
1616
- ddtrace/contrib/internal/asgi/*
1717
asyncio:
1818
- ddtrace/contrib/_asyncio.py
19+
- ddtrace/contrib/internal/asyncio/*
1920
avro:
2021
- ddtrace/contrib/_avro.py
2122
- ddtrace/contrib/internal/avro/*

0 commit comments

Comments
 (0)