|
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 | + |
11 | 13 | _logger = logging.getLogger(__name__) |
12 | 14 |
|
13 | 15 | R = TypeVar("R") |
@@ -90,33 +92,26 @@ async def cancel_wait_task( |
90 | 92 | CancelledError: raised ONLY if owner is being cancelled. |
91 | 93 | """ |
92 | 94 |
|
93 | | - cancelling = task.cancel() |
94 | | - if not cancelling: |
95 | | - return # task could not be cancelled (either already done or something else) |
96 | | - |
97 | | - assert task.cancelling() # nosec |
98 | | - assert not task.cancelled() # nosec |
99 | | - assert not task.done() # nosec |
100 | | - |
| 95 | + task.cancel() |
101 | 96 | try: |
102 | | - |
103 | | - await asyncio.shield( |
104 | | - # NOTE shield ensures that cancellation of the caller function won't stop you |
105 | | - # from observing the cancellation/finalization of task. |
106 | | - asyncio.wait_for(task, timeout=max_delay) |
107 | | - ) |
| 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 | + ) |
108 | 105 |
|
109 | 106 | except asyncio.CancelledError: |
110 | 107 | assert task.done() # nosec |
111 | | - if asyncio.current_task().cancelling() > 0: |
| 108 | + current_task = asyncio.current_task() |
| 109 | + assert current_task is not None # nosec |
| 110 | + if current_task.cancelling() > 0: |
112 | 111 | # owner function is being cancelled -> propagate cancellation |
113 | 112 | raise |
114 | 113 |
|
115 | 114 | # else: task cancellation is complete, we can safely ignore it |
116 | | - _logger.debug( |
117 | | - "Task %s cancellation is complete", |
118 | | - task.get_name(), |
119 | | - ) |
120 | 115 |
|
121 | 116 |
|
122 | 117 | def delayed_start( |
|
0 commit comments