One more thing. I've figured out that I can fix the cancellation around the asyncio.wait_for() with asyncio.shield() like:
try:
await asyncio.shield(wf := asyncio.ensure_future(asyncio.wait_for(self.event.wait(), timeout=60.0)))
except asyncio.CancelledError:
wf.cancel()
result = await asyncio.gather(wf, return_exceptions=True)
# here I know there is a cancellation AND I might have a result as well!
raise
However I don't like the idea of writing all that boilerplate for every wait_for usage. I still might be overlooking something, but at least I have adequate workarounds.
I'm curious what the consensus will be on this issue. I'm certain it should be documented though. Right now there is no mention of ignoring/eating a cancellation. |