Skip to content

Commit 7485a64

Browse files
committed
fix async tools
1 parent 24de14f commit 7485a64

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

packages/common-library/src/common_library/async_tools.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,26 @@ async def cancel_wait_task(
8989
TimeoutError: raised if cannot cancel the task.
9090
CancelledError: raised ONLY if owner is being cancelled.
9191
"""
92-
93-
cancelling = task.cancel()
94-
if not cancelling:
95-
return # task was alredy cancelled
96-
97-
assert task.cancelling() # nosec
98-
assert not task.cancelled() # nosec
99-
92+
task.cancel("cancel_wait_task was called to cancel this task")
10093
try:
101-
94+
_logger.debug("%s", f"Cancelling task {task.get_name()!r}")
10295
await asyncio.shield(
10396
# NOTE shield ensures that cancellation of the caller function won't stop you
10497
# from observing the cancellation/finalization of task.
10598
asyncio.wait_for(task, timeout=max_delay)
10699
)
107100

108101
except asyncio.CancelledError:
109-
if not task.cancelled():
110-
# task owner function is being cancelled -> propagate cancellation
102+
current_task = asyncio.current_task()
103+
assert current_task is not None # nosec
104+
if current_task.cancelling() > 0:
105+
# owner function is being cancelled -> propagate cancellation
111106
raise
112-
113-
# else: task cancellation is complete, we can safely ignore it
114-
_logger.debug(
115-
"Task %s cancellation is complete",
116-
task.get_name(),
117-
)
107+
finally:
108+
if not task.done():
109+
_logger.error("Failed to cancel %s", task.get_name())
110+
else:
111+
_logger.debug("%s", f"Task {task.get_name()!r} cancelled")
118112

119113

120114
def delayed_start(

0 commit comments

Comments
 (0)