|
8 | 8 | from inspect import isawaitable |
9 | 9 | from typing import Any, ParamSpec, TypeVar, overload |
10 | 10 |
|
11 | | -from servicelib.logging_utils import log_context |
12 | | - |
13 | 11 | _logger = logging.getLogger(__name__) |
14 | 12 |
|
15 | 13 | R = TypeVar("R") |
@@ -94,22 +92,24 @@ async def cancel_wait_task( |
94 | 92 |
|
95 | 93 | task.cancel() |
96 | 94 | try: |
97 | | - with log_context( |
98 | | - _logger, logging.DEBUG, f"Cancelling task {task.get_name()!r}" |
99 | | - ): |
100 | | - await asyncio.shield( |
101 | | - # NOTE shield ensures that cancellation of the caller function won't stop you |
102 | | - # from observing the cancellation/finalization of task. |
103 | | - asyncio.wait_for(task, timeout=max_delay) |
104 | | - ) |
| 95 | + _logger.debug("%s", f"Cancelling task {task.get_name()!r}") |
| 96 | + await asyncio.shield( |
| 97 | + # NOTE shield ensures that cancellation of the caller function won't stop you |
| 98 | + # from observing the cancellation/finalization of task. |
| 99 | + asyncio.wait_for(task, timeout=max_delay) |
| 100 | + ) |
105 | 101 |
|
106 | 102 | except asyncio.CancelledError: |
107 | | - assert task.done() # nosec |
108 | 103 | current_task = asyncio.current_task() |
109 | 104 | assert current_task is not None # nosec |
110 | 105 | if current_task.cancelling() > 0: |
111 | 106 | # owner function is being cancelled -> propagate cancellation |
112 | 107 | raise |
| 108 | + finally: |
| 109 | + if not task.done(): |
| 110 | + _logger.error("Failed to cancel %s", task.get_name()) |
| 111 | + else: |
| 112 | + _logger.debug("%s", f"Task {task.get_name()!r} cancelled") |
113 | 113 |
|
114 | 114 |
|
115 | 115 | def delayed_start( |
|
0 commit comments