-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Description
When running in a mixed codebased of asyncio and tornado with python 3.10 and tornado 6.4.1, I've observed very peculiar behavior when a future holding an asyncio.CancelledError exception is yielded on in a function marked with tornado.gen.coroutine. Instead of the tornado.gen.coroutine forwarding the exception up to the calling logic through the returned future, the coroutine will instead never complete.
Here are some demonstrative unit-tests that display the behavior. In both of the below unit-tests, the test will fail with a timeout from the pytest.mark.gen_test decorator.
import pytest
import asyncio
from tornado import gen
@pytest.mark.gen_test(timeout=5.0)
def test_simple_bad_behavior():
@gen.coroutine
def raise_cancelled_error():
yield gen.sleep(0.1)
raise asyncio.CancelledError()
with pytest.raises(asyncio.CancelledError):
yield raise_cancelled_error()
@pytest.mark.gen_test(timeout=5.0)
def test_demonstrate_bad_behavior():
async def cancellable():
await asyncio.sleep(10000.0)
cancellable_future = asyncio.ensure_future(cancellable())
cancellable_future.cancel()
# Sleep so that the above future can complete.
yield gen.sleep(1e-6)
with pytest.raises(asyncio.CancelledError):
yield cancellable_futureNot passing up asyncio.CancelledErrors breaks compatibility between asyncio and tornado in challenging to debug manner. The coroutines are stopped without logging and will never be started again by the IOLoop. It's not possible to use with_timeout to avoid this issue, since the IOLoop does not reschedule the corresponding coroutine.
The issue appears to originate from this line: https://github.com/tornadoweb/tornado/blob/master/tornado/gen.py#L224. Exceptions are caught and stored in the corresponding future. However, BaseExceptions are not caught, and asyncio.CancelledError is a BaseException. A solution to the issue would be to include asyncio.CancelledError alongside Exception as the errors that get caught.