There are several instances where gen.with_timeout is used in the codebase. As part of our switch from existing tornado code to asyncio, we should move to using asyncio.wait_for.
However, one important distinction between these functions is that gen.with_timeout does not cancel a task when it's timeout expires, while asyncio.wait_fordoes cancel the task. So we need to be careful and determine on a case by case basis when task cancellation on timeout should happen. From @jcrist's comment (#3365 (comment)):
replacing it isn't always straightforward. Each case needs to be examined to determine what the intent was and if the lack of cancellation was accidental or necessary.
To avoid task cancellation when using asyncio.wait_for, the task should first be wrapped in asyncio.shield
There are several instances where
gen.with_timeoutis used in the codebase. As part of our switch from existing tornado code to asyncio, we should move to usingasyncio.wait_for.However, one important distinction between these functions is that
gen.with_timeoutdoes not cancel a task when it's timeout expires, whileasyncio.wait_fordoes cancel the task. So we need to be careful and determine on a case by case basis when task cancellation on timeout should happen. From @jcrist's comment (#3365 (comment)):To avoid task cancellation when using
asyncio.wait_for, the task should first be wrapped inasyncio.shield