Skip to content

Commit 650cbfd

Browse files
committed
fix(async-io): instrumented asyncio.wait_for properly raises asyncio.TimeoutError
@see https://docs.python.org/3.13/library/asyncio-task.html#asyncio.wait_for
1 parent ff17c79 commit 650cbfd

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ async def trace_coroutine(self, coro):
280280
# CancelledError is raised when a coroutine is cancelled
281281
# before it has a chance to run. We don't want to record
282282
# this as an error.
283+
# Still it needs to be raised in order for `asyncio.wait_for`
284+
# to properly work with timeout and raise accordingly `asyncio.TimeoutError`
283285
except asyncio.CancelledError:
284286
attr["state"] = "cancelled"
287+
raise
285288
except Exception as exc:
286289
exception = exc
287290
state = determine_state(exception)

instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_wait.py

+13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ async def main():
6868
spans = self.memory_exporter.get_finished_spans()
6969
self.assertEqual(len(spans), 2)
7070

71+
def test_asyncio_wait_for_with_timeout(self):
72+
ex = None
73+
74+
async def main():
75+
nonlocal ex
76+
try:
77+
await asyncio.wait_for(async_func(), 0.01)
78+
except asyncio.TimeoutError as e:
79+
ex = e
80+
81+
asyncio.run(main())
82+
self.assertNotEqual(ex, None)
83+
7184
def test_asyncio_as_completed(self):
7285
async def main():
7386
if sys.version_info >= (3, 11):

0 commit comments

Comments
 (0)