tornadoweb / tornadoweb/tornado
IOLoop behaves in an unexpected manner when `asycnio.CancelledError` is raised.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 22.2k
- Forks
- 5.6k
- Avg merge
- 3h 42m
- Merged PRs (30d)
- 16
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_future
Not 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tornado/gen.py around line 224, where exceptions from yielded futures are caught, and run the two reproducing tests in the issue under Python 3.10. Trace how asyncio.CancelledError is handled when yielded from a gen.coroutine. Done means the coroutine forwards the cancellation to its caller and both timeout tests pass without hanging.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100