Skip to content

coroutine: catch asyncio.CancelledError alongside Exception so cancellations surface on the returned future#3664

Open
HrachShah wants to merge 1 commit into
tornadoweb:masterfrom
HrachShah:fix/gen-catch-cancelled-error
Open

coroutine: catch asyncio.CancelledError alongside Exception so cancellations surface on the returned future#3664
HrachShah wants to merge 1 commit into
tornadoweb:masterfrom
HrachShah:fix/gen-catch-cancelled-error

Conversation

@HrachShah

Copy link
Copy Markdown

Fixes #3537.

The @gen.coroutine wrapper and the Runner both caught only Exception. asyncio.CancelledError inherits from BaseException in Python 3.7- and from Exception in 3.8+, so the wrapper's outer try/except Exception around ctx_run(func, ...) and the Runner's except Exception around future.result() silently dropped the cancellation. The pre-fix behaviour either let the CancelledError leak out (3.7-) or vanished into the result_future without ever surfacing on the consumer's await (3.8+).

Catch (Exception, asyncio.CancelledError) explicitly at both call sites so an asyncio cancellation of a yielded future or a sync cancellation in the coroutine body is propagated to the returned future the same way any other exception is. Three new tests cover the sync-raise, post-yield-raise, and yield-a-cancelled-future shapes.

…lations surface on the returned future

The @gen.coroutine wrapper and the Runner both caught only Exception. asyncio.CancelledError inherits from BaseException in Python 3.8- and from Exception in 3.8+, so the wrapper's outer try/except Exception around ctx_run(func, ...) and the Runner's except Exception around future.result() silently dropped the cancellation. The pre-fix behaviour either let the CancelledError leak out (3.8-) or vanished into the result_future without ever surfacing on the consumer's await (3.8+). Catch (Exception, asyncio.CancelledError) explicitly at both call sites so an asyncio cancellation of a yielded future or a sync cancellation in the coroutine body is propagated to the returned future the same way any other exception is. Regression test for tornadoweb#3537.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IOLoop behaves in an unexpected manner when asycnio.CancelledError is raised.

1 participant