| 1 |
import asyncio |
| 2 |
import io |
| 3 |
import unittest |
| 4 |
|
| 5 |
|
| 6 |
# To prevent a warning "test altered the execution environment" |
| 7 |
def tearDownModule(): |
| 8 |
asyncio.events._set_event_loop_policy(None) |
| 9 |
|
| 10 |
|
| 11 |
def capture_test_stack(*, fut=None, depth=1): |
| 12 |
|
| 13 |
def walk(s): |
| 14 |
ret = [ |
| 15 |
(f"T<{n}>" if '-' not in (n := s.future.get_name()) else 'T<anon>') |
| 16 |
if isinstance(s.future, asyncio.Task) else 'F' |
| 17 |
] |
| 18 |
|
| 19 |
ret.append( |
| 20 |
[ |
| 21 |
( |
| 22 |
f"s {entry.frame.f_code.co_name}" |
| 23 |
if entry.frame.f_generator is None else |
| 24 |
( |
| 25 |
f"a {entry.frame.f_generator.cr_code.co_name}" |
| 26 |
if hasattr(entry.frame.f_generator, 'cr_code') else |
| 27 |
f"ag {entry.frame.f_generator.ag_code.co_name}" |
| 28 |
) |
| 29 |
) for entry in s.call_stack |
| 30 |
] |
| 31 |
) |
| 32 |
|
| 33 |
ret.append( |
| 34 |
sorted([ |
| 35 |
walk(ab) for ab in s.awaited_by |
| 36 |
], key=lambda entry: entry[0]) |
| 37 |
) |
| 38 |
|
| 39 |
return ret |
| 40 |
|
| 41 |
buf = io.StringIO() |
| 42 |
asyncio.print_call_graph(fut, file=buf, depth=depth+1) |
| 43 |
|
| 44 |
stack = asyncio.capture_call_graph(fut, depth=depth) |
| 45 |
return walk(stack), buf.getvalue() |
| 46 |
|
| 47 |
|
| 48 |
class CallStackTestBase: |
| 49 |
|
| 50 |
async def test_stack_tgroup(self): |
| 51 |
|
| 52 |
stack_for_c5 = None |
| 53 |
|
| 54 |
def c5(): |
| 55 |
nonlocal stack_for_c5 |
| 56 |
stack_for_c5 = capture_test_stack(depth=2) |
| 57 |
|
| 58 |
async def c4(): |
| 59 |
await asyncio.sleep(0) |
| 60 |
c5() |
| 61 |
|
| 62 |
async def c3(): |
| 63 |
await c4() |
| 64 |
|
| 65 |
async def c2(): |
| 66 |
await c3() |
| 67 |
|
| 68 |
async def c1(task): |
| 69 |
await task |
| 70 |
|
| 71 |
async def main(): |
| 72 |
async with asyncio.TaskGroup() as tg: |
| 73 |
task = tg.create_task(c2(), name="c2_root") |
| 74 |
tg.create_task(c1(task), name="sub_main_1") |
| 75 |
tg.create_task(c1(task), name="sub_main_2") |
| 76 |
|
| 77 |
await main() |
| 78 |
|
| 79 |
self.assertEqual(stack_for_c5[0], [ |
| 80 |
# task name |
| 81 |
'T<c2_root>', |
| 82 |
# call stack |
| 83 |
['s c5', 'a c4', 'a c3', 'a c2'], |
| 84 |
# awaited by |
| 85 |
[ |
| 86 |
['T<anon>', |
| 87 |
['a _aexit', 'a __aexit__', 'a main', 'a test_stack_tgroup'], [] |
| 88 |
], |
| 89 |
['T<sub_main_1>', |
| 90 |
['a c1'], |
| 91 |
[ |
| 92 |
['T<anon>', |
| 93 |
['a _aexit', 'a __aexit__', 'a main', 'a test_stack_tgroup'], [] |
| 94 |
] |
| 95 |
] |
| 96 |
], |
| 97 |
['T<sub_main_2>', |
| 98 |
['a c1'], |
| 99 |
[ |
| 100 |
['T<anon>', |
| 101 |
['a _aexit', 'a __aexit__', 'a main', 'a test_stack_tgroup'], [] |
| 102 |
] |
| 103 |
] |
| 104 |
] |
| 105 |
] |
| 106 |
]) |
| 107 |
|
| 108 |
self.assertIn( |
| 109 |
' async CallStackTestBase.test_stack_tgroup()', |
| 110 |
stack_for_c5[1]) |
| 111 |
|
| 112 |
|
| 113 |
async def test_stack_async_gen(self): |
| 114 |
|
| 115 |
stack_for_gen_nested_call = None |
| 116 |
|
| 117 |
async def gen_nested_call(): |
| 118 |
nonlocal stack_for_gen_nested_call |
| 119 |
stack_for_gen_nested_call = capture_test_stack() |
| 120 |
|
| 121 |
async def gen(): |
| 122 |
for num in range(2): |
| 123 |
yield num |
| 124 |
if num == 1: |
| 125 |
await gen_nested_call() |
| 126 |
|
| 127 |
async def main(): |
| 128 |
async for el in gen(): |
| 129 |
pass |
| 130 |
|
| 131 |
await main() |
| 132 |
|
| 133 |
self.assertEqual(stack_for_gen_nested_call[0], [ |
| 134 |
'T<anon>', |
| 135 |
[ |
| 136 |
's capture_test_stack', |
| 137 |
'a gen_nested_call', |
| 138 |
'ag gen', |
| 139 |
'a main', |
| 140 |
'a test_stack_async_gen' |
| 141 |
], |
| 142 |
[] |
| 143 |
]) |
| 144 |
|
| 145 |
self.assertIn( |
| 146 |
'async generator CallStackTestBase.test_stack_async_gen.<locals>.gen()', |
| 147 |
stack_for_gen_nested_call[1]) |
| 148 |
|
| 149 |
async def test_stack_gather(self): |
| 150 |
|
| 151 |
stack_for_deep = None |
| 152 |
|
| 153 |
async def deep(): |
| 154 |
await asyncio.sleep(0) |
| 155 |
nonlocal stack_for_deep |
| 156 |
stack_for_deep = capture_test_stack() |
| 157 |
|
| 158 |
async def c1(): |
| 159 |
await asyncio.sleep(0) |
| 160 |
await deep() |
| 161 |
|
| 162 |
async def c2(): |
| 163 |
await asyncio.sleep(0) |
| 164 |
|
| 165 |
async def main(): |
| 166 |
await asyncio.gather(c1(), c2()) |
| 167 |
|
| 168 |
await main() |
| 169 |
|
| 170 |
self.assertEqual(stack_for_deep[0], [ |
| 171 |
'T<anon>', |
| 172 |
['s capture_test_stack', 'a deep', 'a c1'], |
| 173 |
[ |
| 174 |
['T<anon>', ['a main', 'a test_stack_gather'], []] |
| 175 |
] |
| 176 |
]) |
| 177 |
|
| 178 |
async def test_stack_shield(self): |
| 179 |
|
| 180 |
stack_for_shield = None |
| 181 |
|
| 182 |
async def deep(): |
| 183 |
await asyncio.sleep(0) |
| 184 |
nonlocal stack_for_shield |
| 185 |
stack_for_shield = capture_test_stack() |
| 186 |
|
| 187 |
async def c1(): |
| 188 |
await asyncio.sleep(0) |
| 189 |
await deep() |
| 190 |
|
| 191 |
async def main(): |
| 192 |
await asyncio.shield(c1()) |
| 193 |
|
| 194 |
await main() |
| 195 |
|
| 196 |
self.assertEqual(stack_for_shield[0], [ |
| 197 |
'T<anon>', |
| 198 |
['s capture_test_stack', 'a deep', 'a c1'], |
| 199 |
[ |
| 200 |
['T<anon>', ['a main', 'a test_stack_shield'], []] |
| 201 |
] |
| 202 |
]) |
| 203 |
|
| 204 |
async def test_stack_timeout(self): |
| 205 |
|
| 206 |
stack_for_inner = None |
| 207 |
|
| 208 |
async def inner(): |
| 209 |
await asyncio.sleep(0) |
| 210 |
nonlocal stack_for_inner |
| 211 |
stack_for_inner = capture_test_stack() |
| 212 |
|
| 213 |
async def c1(): |
| 214 |
async with asyncio.timeout(1): |
| 215 |
await asyncio.sleep(0) |
| 216 |
await inner() |
| 217 |
|
| 218 |
async def main(): |
| 219 |
await asyncio.shield(c1()) |
| 220 |
|
| 221 |
await main() |
| 222 |
|
| 223 |
self.assertEqual(stack_for_inner[0], [ |
| 224 |
'T<anon>', |
| 225 |
['s capture_test_stack', 'a inner', 'a c1'], |
| 226 |
[ |
| 227 |
['T<anon>', ['a main', 'a test_stack_timeout'], []] |
| 228 |
] |
| 229 |
]) |
| 230 |
|
| 231 |
async def test_stack_wait(self): |
| 232 |
|
| 233 |
stack_for_inner = None |
| 234 |
|
| 235 |
async def inner(): |
| 236 |
await asyncio.sleep(0) |
| 237 |
nonlocal stack_for_inner |
| 238 |
stack_for_inner = capture_test_stack() |
| 239 |
|
| 240 |
async def c1(): |
| 241 |
async with asyncio.timeout(1): |
| 242 |
await asyncio.sleep(0) |
| 243 |
await inner() |
| 244 |
|
| 245 |
async def c2(): |
| 246 |
for i in range(3): |
| 247 |
await asyncio.sleep(0) |
| 248 |
|
| 249 |
async def main(t1, t2): |
| 250 |
while True: |
| 251 |
_, pending = await asyncio.wait([t1, t2]) |
| 252 |
if not pending: |
| 253 |
break |
| 254 |
|
| 255 |
t1 = asyncio.create_task(c1()) |
| 256 |
t2 = asyncio.create_task(c2()) |
| 257 |
try: |
| 258 |
await main(t1, t2) |
| 259 |
finally: |
| 260 |
await t1 |
| 261 |
await t2 |
| 262 |
|
| 263 |
self.assertEqual(stack_for_inner[0], [ |
| 264 |
'T<anon>', |
| 265 |
['s capture_test_stack', 'a inner', 'a c1'], |
| 266 |
[ |
| 267 |
['T<anon>', |
| 268 |
['a _wait', 'a wait', 'a main', 'a test_stack_wait'], |
| 269 |
[] |
| 270 |
] |
| 271 |
] |
| 272 |
]) |
| 273 |
|
| 274 |
async def test_stack_task(self): |
| 275 |
|
| 276 |
stack_for_inner = None |
| 277 |
|
| 278 |
async def inner(): |
| 279 |
await asyncio.sleep(0) |
| 280 |
nonlocal stack_for_inner |
| 281 |
stack_for_inner = capture_test_stack() |
| 282 |
|
| 283 |
async def c1(): |
| 284 |
await inner() |
| 285 |
|
| 286 |
async def c2(): |
| 287 |
await asyncio.create_task(c1(), name='there there') |
| 288 |
|
| 289 |
async def main(): |
| 290 |
await c2() |
| 291 |
|
| 292 |
await main() |
| 293 |
|
| 294 |
self.assertEqual(stack_for_inner[0], [ |
| 295 |
'T<there there>', |
| 296 |
['s capture_test_stack', 'a inner', 'a c1'], |
| 297 |
[['T<anon>', ['a c2', 'a main', 'a test_stack_task'], []]] |
| 298 |
]) |
| 299 |
|
| 300 |
async def test_stack_future(self): |
| 301 |
|
| 302 |
stack_for_fut = None |
| 303 |
|
| 304 |
async def a2(fut): |
| 305 |
await fut |
| 306 |
|
| 307 |
async def a1(fut): |
| 308 |
await a2(fut) |
| 309 |
|
| 310 |
async def b1(fut): |
| 311 |
await fut |
| 312 |
|
| 313 |
async def main(): |
| 314 |
nonlocal stack_for_fut |
| 315 |
|
| 316 |
fut = asyncio.Future() |
| 317 |
async with asyncio.TaskGroup() as g: |
| 318 |
g.create_task(a1(fut), name="task A") |
| 319 |
g.create_task(b1(fut), name='task B') |
| 320 |
|
| 321 |
for _ in range(5): |
| 322 |
# Do a few iterations to ensure that both a1 and b1 |
| 323 |
# await on the future |
| 324 |
await asyncio.sleep(0) |
| 325 |
|
| 326 |
stack_for_fut = capture_test_stack(fut=fut) |
| 327 |
fut.set_result(None) |
| 328 |
|
| 329 |
await main() |
| 330 |
|
| 331 |
self.assertEqual(stack_for_fut[0], |
| 332 |
['F', |
| 333 |
[], |
| 334 |
[ |
| 335 |
['T<task A>', |
| 336 |
['a a2', 'a a1'], |
| 337 |
[['T<anon>', ['a test_stack_future'], []]] |
| 338 |
], |
| 339 |
['T<task B>', |
| 340 |
['a b1'], |
| 341 |
[['T<anon>', ['a test_stack_future'], []]] |
| 342 |
], |
| 343 |
]] |
| 344 |
) |
| 345 |
|
| 346 |
self.assertTrue(stack_for_fut[1].startswith('* Future(id=')) |
| 347 |
|
| 348 |
|
| 349 |
@unittest.skipIf( |
| 350 |
not hasattr(asyncio.futures, "_c_future_add_to_awaited_by"), |
| 351 |
"C-accelerated asyncio call graph backend missing", |
| 352 |
) |
| 353 |
class TestCallStackC(CallStackTestBase, unittest.IsolatedAsyncioTestCase): |
| 354 |
def setUp(self): |
| 355 |
futures = asyncio.futures |
| 356 |
tasks = asyncio.tasks |
| 357 |
|
| 358 |
self._Future = asyncio.Future |
| 359 |
asyncio.Future = futures.Future = futures._CFuture |
| 360 |
|
| 361 |
self._Task = asyncio.Task |
| 362 |
asyncio.Task = tasks.Task = tasks._CTask |
| 363 |
|
| 364 |
self._future_add_to_awaited_by = asyncio.future_add_to_awaited_by |
| 365 |
futures.future_add_to_awaited_by = futures._c_future_add_to_awaited_by |
| 366 |
asyncio.future_add_to_awaited_by = futures.future_add_to_awaited_by |
| 367 |
|
| 368 |
self._future_discard_from_awaited_by = asyncio.future_discard_from_awaited_by |
| 369 |
futures.future_discard_from_awaited_by = futures._c_future_discard_from_awaited_by |
| 370 |
asyncio.future_discard_from_awaited_by = futures.future_discard_from_awaited_by |
| 371 |
|
| 372 |
self._current_task = asyncio.current_task |
| 373 |
asyncio.current_task = asyncio.tasks.current_task = tasks._c_current_task |
| 374 |
|
| 375 |
def tearDown(self): |
| 376 |
futures = asyncio.futures |
| 377 |
tasks = asyncio.tasks |
| 378 |
|
| 379 |
futures.future_discard_from_awaited_by = self._future_discard_from_awaited_by |
| 380 |
asyncio.future_discard_from_awaited_by = self._future_discard_from_awaited_by |
| 381 |
del self._future_discard_from_awaited_by |
| 382 |
|
| 383 |
futures.future_add_to_awaited_by = self._future_add_to_awaited_by |
| 384 |
asyncio.future_add_to_awaited_by = self._future_add_to_awaited_by |
| 385 |
del self._future_add_to_awaited_by |
| 386 |
|
| 387 |
asyncio.Task = self._Task |
| 388 |
tasks.Task = self._Task |
| 389 |
del self._Task |
| 390 |
|
| 391 |
asyncio.Future = self._Future |
| 392 |
futures.Future = self._Future |
| 393 |
del self._Future |
| 394 |
|
| 395 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 396 |
|
| 397 |
|
| 398 |
@unittest.skipIf( |
| 399 |
not hasattr(asyncio.futures, "_py_future_add_to_awaited_by"), |
| 400 |
"Pure Python asyncio call graph backend missing", |
| 401 |
) |
| 402 |
class TestCallStackPy(CallStackTestBase, unittest.IsolatedAsyncioTestCase): |
| 403 |
def setUp(self): |
| 404 |
futures = asyncio.futures |
| 405 |
tasks = asyncio.tasks |
| 406 |
|
| 407 |
self._Future = asyncio.Future |
| 408 |
asyncio.Future = futures.Future = futures._PyFuture |
| 409 |
|
| 410 |
self._Task = asyncio.Task |
| 411 |
asyncio.Task = tasks.Task = tasks._PyTask |
| 412 |
|
| 413 |
self._future_add_to_awaited_by = asyncio.future_add_to_awaited_by |
| 414 |
futures.future_add_to_awaited_by = futures._py_future_add_to_awaited_by |
| 415 |
asyncio.future_add_to_awaited_by = futures.future_add_to_awaited_by |
| 416 |
|
| 417 |
self._future_discard_from_awaited_by = asyncio.future_discard_from_awaited_by |
| 418 |
futures.future_discard_from_awaited_by = futures._py_future_discard_from_awaited_by |
| 419 |
asyncio.future_discard_from_awaited_by = futures.future_discard_from_awaited_by |
| 420 |
|
| 421 |
self._current_task = asyncio.current_task |
| 422 |
asyncio.current_task = asyncio.tasks.current_task = tasks._py_current_task |
| 423 |
|
| 424 |
|
| 425 |
def tearDown(self): |
| 426 |
futures = asyncio.futures |
| 427 |
tasks = asyncio.tasks |
| 428 |
|
| 429 |
futures.future_discard_from_awaited_by = self._future_discard_from_awaited_by |
| 430 |
asyncio.future_discard_from_awaited_by = self._future_discard_from_awaited_by |
| 431 |
del self._future_discard_from_awaited_by |
| 432 |
|
| 433 |
futures.future_add_to_awaited_by = self._future_add_to_awaited_by |
| 434 |
asyncio.future_add_to_awaited_by = self._future_add_to_awaited_by |
| 435 |
del self._future_add_to_awaited_by |
| 436 |
|
| 437 |
asyncio.Task = self._Task |
| 438 |
tasks.Task = self._Task |
| 439 |
del self._Task |
| 440 |
|
| 441 |
asyncio.Future = self._Future |
| 442 |
futures.Future = self._Future |
| 443 |
del self._Future |
| 444 |
|
| 445 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |