Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions tornado/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Future[_T]:
result = ctx_run(func, *args, **kwargs)
except (Return, StopIteration) as e:
result = _value_from_stopiteration(e)
except Exception:
except (Exception, asyncio.CancelledError):
# asyncio.CancelledError is a BaseException in Python <3.8 and
# changed to inherit from Exception in 3.8+. We catch it
# explicitly here so that an asyncio cancellation of the wrapped
# function surfaces on the returned future (matching the
# behavior of coroutine-created tasks) rather than escaping
# the wrapper and silently breaking the IOLoop callback chain.
# See https://github.com/tornadoweb/tornado/issues/3537
future_set_exc_info(future, sys.exc_info())
try:
return future
Expand All @@ -225,7 +232,8 @@ def wrapper(*args: Any, **kwargs: Any) -> Future[_T]:
future_set_result_unless_cancelled(
future, _value_from_stopiteration(e)
)
except Exception:
except (Exception, asyncio.CancelledError):
# Same as above: see https://github.com/tornadoweb/tornado/issues/3537
future_set_exc_info(future, sys.exc_info())
else:
# Provide strong references to Runner objects as long
Expand Down Expand Up @@ -765,10 +773,14 @@ def run(self) -> None:
try:
try:
value = future.result()
except Exception as e:
except (Exception, asyncio.CancelledError) as e:
# Save the exception for later. It's important that
# gen.throw() not be called inside this try/except block
# because that makes sys.exc_info behave unexpectedly.
# asyncio.CancelledError is caught here so that
# cancellation of a yielded future surfaces on
# result_future rather than being silently dropped
# when the generator is resumed. See tornado #3537.
exc: Exception | None = e
else:
exc = None
Expand All @@ -793,7 +805,14 @@ def run(self) -> None:
)
self.result_future = None # type: ignore
return
except Exception:
except (Exception, asyncio.CancelledError):
# Catching asyncio.CancelledError alongside Exception
# mirrors the inline-first-iteration handling above:
# an asyncio cancellation that escapes the generator
# body (or is raised by the generator itself) is
# propagated to result_future instead of leaking past
# the Runner and breaking the IOLoop's bookkeeping.
# See tornado #3537.
self.finished = True
self.future = _null_future
future_set_exc_info(self.result_future, sys.exc_info())
Expand Down
53 changes: 53 additions & 0 deletions tornado/test/gen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,59 @@ def f():
yield future
self.finished = True

@gen_test
def test_sync_raise_cancelled_error(self):
# A coroutine that raises asyncio.CancelledError synchronously
# (before its first yield) should still surface the cancellation
# on the returned future, the same way any other Exception is
# captured. Regression test for #3537.
@gen.coroutine
def f():
raise asyncio.CancelledError()

future = f()
with self.assertRaises(asyncio.CancelledError):
yield future
self.finished = True

@gen_test
def test_async_raise_cancelled_error(self):
# A coroutine that raises asyncio.CancelledError after a yield
# point should propagate the cancellation to the future through
# the Runner, matching the behaviour for plain Exception
# subclasses. Regression test for #3537.
@gen.coroutine
def f():
yield gen.moment
raise asyncio.CancelledError()

future = f()
with self.assertRaises(asyncio.CancelledError):
yield future
self.finished = True

@gen_test
def test_yield_cancelled_asyncio_future(self):
# Yielding an asyncio future that was cancelled before being
# awaited should propagate CancelledError to the parent
# coroutine. Without the fix the parent coroutine never
# completes and the test times out. Regression test for #3537.
async def cancellable():
await asyncio.sleep(10000.0)

cancellable_future = asyncio.ensure_future(cancellable())
cancellable_future.cancel()

@gen.coroutine
def child():
yield gen.moment
yield cancellable_future

future = child()
with self.assertRaises(asyncio.CancelledError):
yield future
self.finished = True

@gen_test
def test_replace_yieldpoint_exception(self):
# Test exception handling: a coroutine can catch one exception
Expand Down