Recently an interesting paper about task cancellation has been published:
https://www.usenix.org/conference/osdi22/presentation/sethi
Although the paper did not mention Python at all, I think that Python 3.11’s asyncio already covers most issues.
In addition to the paper’s summary about cancellation patterns, I think we need to address:
- Proper task (or task group) abstractions/extensions for long-running (open-ended) tasks, similar to my suggestion for
PersistentTaskGroup
(though the naming may need update) - Tracking the source of cancellation (probably via the path name of module who called
.cancel()
as an intrinsic argument toasyncio.CancelledError
, as a replacement of cancel messages) - Prevention of cancelling shielded tasks or an improved abstraction of
asyncio.shield()
to guarantee completion of such tasks upon event loop shutdown - Tracking and potentially force-terminating hanging shielded tasks with explicit signals or timeouts
- Prevention of in-mid or double cancelling the tasks who are now performing cleanup in reaction to a prior cancellation, or more clear examples and description in the docs to demonstrate how to avoid such situations – or we may need some better abstraction for cancellation