Skip to content

Commit fd86212

Browse files
authored
fix(asyncio): avoid overwriting coroutine names (#12011)
Resolves: #10975 ## 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 - [ ] 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 91eeb4d commit fd86212

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

ddtrace/contrib/internal/asyncio/patch.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,12 @@ async def traced_coro(*args_c, **kwargs_c):
4949
pin.tracer.context_provider.activate(dd_active)
5050
return await coro
5151

52-
args, kwargs = set_argument_value(args, kwargs, 1, "coro", traced_coro())
52+
# DEV: try to persist the original function name (useful for debugging)
53+
tc = traced_coro()
54+
if hasattr(coro, "__name__"):
55+
tc.__name__ = coro.__name__
56+
if hasattr(coro, "__qualname__"):
57+
tc.__qualname__ = coro.__qualname__
58+
args, kwargs = set_argument_value(args, kwargs, 1, "coro", tc)
59+
5360
return wrapped(*args, **kwargs)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
asyncio: Fixes an issue where the name of a coroutine was being overridden by a ddtrace function.

tests/contrib/asyncio/test_tracer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Ensure that the tracer works with asynchronous executions within the same ``IOLoop``."""
22
import asyncio
3+
import os
4+
import re
35

46
import pytest
57

@@ -193,3 +195,31 @@ async def test():
193195
spans = tracer.get_spans()
194196
assert len(spans) == 3
195197
assert spans[0].trace_id == spans[1].trace_id == spans[2].trace_id
198+
199+
200+
def test_asyncio_scheduled_tasks_debug_logs(run_python_code_in_subprocess):
201+
code = """
202+
import ddtrace
203+
204+
ddtrace.patch(asyncio=True)
205+
import asyncio
206+
import time
207+
import pytest
208+
209+
210+
async def my_function():
211+
time.sleep(2)
212+
213+
if __name__ == "__main__":
214+
asyncio.run(my_function())
215+
"""
216+
env = os.environ.copy()
217+
env["PYTHONASYNCIODEBUG"] = "1"
218+
out, err, status, _ = run_python_code_in_subprocess(code, env=env)
219+
assert status == 0, err + out
220+
221+
pattern = rb"Executing <Task finished name=\'Task-1\' coro=<my_function\(\) done, "
222+
rb"defined at .*/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:.* result=None "
223+
rb"created at .*/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:.* took .* seconds"
224+
match = re.match(pattern, err)
225+
assert match, err

0 commit comments

Comments
 (0)