|
1 | 1 | import asyncio |
| 2 | +import sys |
2 | 3 | from collections.abc import Awaitable |
3 | 4 | from contextlib import suppress |
4 | 5 | from typing import Callable, NoReturn |
@@ -239,6 +240,100 @@ async def coro2() -> None: |
239 | 240 | await job.close() |
240 | 241 |
|
241 | 242 |
|
| 243 | +async def test_job_close_cancelled_from_outside(scheduler: Scheduler) -> None: |
| 244 | + """An external cancellation of the task running close() must propagate. |
| 245 | +
|
| 246 | + The CancelledError raised by awaiting the job's cancelled task is |
| 247 | + expected and swallowed, but a cancellation aimed at the caller of |
| 248 | + close() itself is not ours to swallow. |
| 249 | + """ |
| 250 | + cancel_seen = asyncio.Event() |
| 251 | + unblock = asyncio.Event() |
| 252 | + |
| 253 | + async def coro() -> None: |
| 254 | + try: |
| 255 | + await asyncio.sleep(60) |
| 256 | + except asyncio.CancelledError: |
| 257 | + cancel_seen.set() |
| 258 | + await unblock.wait() |
| 259 | + raise |
| 260 | + |
| 261 | + job = await scheduler.spawn(coro()) |
| 262 | + closer = asyncio.ensure_future(job.close()) |
| 263 | + # Once the job saw the cancellation, close() is suspended awaiting |
| 264 | + # the job task, which is blocked until unblock is set. |
| 265 | + await cancel_seen.wait() |
| 266 | + closer.cancel() |
| 267 | + await asyncio.wait({closer}) |
| 268 | + assert closer.cancelled() |
| 269 | + |
| 270 | + unblock.set() |
| 271 | + with suppress(asyncio.CancelledError): |
| 272 | + await job.wait() |
| 273 | + |
| 274 | + |
| 275 | +@pytest.mark.skipif( |
| 276 | + sys.version_info < (3, 11), |
| 277 | + reason="distinguishing this race needs Task.cancelling()", |
| 278 | +) |
| 279 | +async def test_job_close_cancelled_from_outside_completion_race( |
| 280 | + scheduler: Scheduler, |
| 281 | +) -> None: |
| 282 | + """The job task may finish in the same loop iteration the caller of |
| 283 | + close() is cancelled in; the external cancellation must still |
| 284 | + propagate.""" |
| 285 | + cancel_seen = asyncio.Event() |
| 286 | + unblock = asyncio.Event() |
| 287 | + |
| 288 | + async def coro() -> None: |
| 289 | + try: |
| 290 | + await asyncio.sleep(60) |
| 291 | + except asyncio.CancelledError: |
| 292 | + cancel_seen.set() |
| 293 | + await unblock.wait() |
| 294 | + raise |
| 295 | + |
| 296 | + job = await scheduler.spawn(coro()) |
| 297 | + closer = asyncio.ensure_future(job.close()) |
| 298 | + await cancel_seen.wait() |
| 299 | + # Unblock the job before cancelling the closer: the job task then |
| 300 | + # completes and schedules its done callbacks before the closer wakes |
| 301 | + # up and detaches them. |
| 302 | + unblock.set() |
| 303 | + closer.cancel() |
| 304 | + await asyncio.wait({closer}) |
| 305 | + assert closer.cancelled() |
| 306 | + with suppress(asyncio.CancelledError): |
| 307 | + await job.wait() |
| 308 | + |
| 309 | + |
| 310 | +async def test_job_close_timeout_source_traceback( |
| 311 | + make_scheduler: _MakeScheduler, |
| 312 | +) -> None: |
| 313 | + """In debug mode the close timeout report carries the source traceback.""" |
| 314 | + loop = asyncio.get_running_loop() |
| 315 | + loop.set_debug(True) |
| 316 | + try: |
| 317 | + handler = mock.Mock() |
| 318 | + scheduler = await make_scheduler(close_timeout=0.01, exception_handler=handler) |
| 319 | + |
| 320 | + async def coro() -> None: |
| 321 | + try: |
| 322 | + await asyncio.sleep(60) |
| 323 | + except asyncio.CancelledError: |
| 324 | + await asyncio.sleep(60) |
| 325 | + |
| 326 | + job = await scheduler.spawn(coro()) |
| 327 | + await scheduler.close() |
| 328 | + assert job.closed |
| 329 | + assert handler.called |
| 330 | + context = handler.call_args[0][1] |
| 331 | + assert context["message"] == "Job closing timed out" |
| 332 | + assert "source_traceback" in context |
| 333 | + finally: |
| 334 | + loop.set_debug(False) |
| 335 | + |
| 336 | + |
242 | 337 | async def test_job_await_closed(scheduler: Scheduler) -> None: |
243 | 338 | async def coro() -> int: |
244 | 339 | return 5 |
|
0 commit comments