Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TaskStatus.started() being a no-op when in an effectively cancelled scope #2896

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test that TaskStatus.started() does not allow teleporting Cancelled(s…
…) around the tree
gschaffner committed Apr 24, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
binghe Chun Tian
commit 39bc92a79b11d785ec60314324d3bebb215a7cd1
55 changes: 54 additions & 1 deletion src/trio/_core/_tests/test_run.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
Callable,
Generator,
)
from exceptiongroup import catch

if sys.version_info < (3, 11):
from exceptiongroup import BaseExceptionGroup, ExceptionGroup
@@ -1845,7 +1846,59 @@ async def no_checkpoint_before_started(
raise AssertionError() from exc # pragma: no cover
assert not cs.cancelled_caught

# trying to start in a closed nursery raises an error immediately
# calling started() while handling a Cancelled raises an error immediately.
async def started_while_handling_cancelled(
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
try:
await _core.checkpoint()
except _core.Cancelled:
task_status.started()
raise AssertionError() # pragma: no cover

async with _core.open_nursery() as nursery:
with _core.CancelScope() as cs:
cs.cancel()
with pytest.raises(RuntimeError):
await nursery.start(started_while_handling_cancelled)

# calling started() while handling multiple Cancelleds raises an error
# immediately.
async def started_while_handling_multiple_cancelled(
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
with catch({_core.Cancelled: lambda _: task_status.started()}):
async with _core.open_nursery() as nursery:
nursery.start_soon(_core.checkpoint)
nursery.start_soon(_core.checkpoint)
raise AssertionError() # pragma: no cover

async with _core.open_nursery() as nursery:
with _core.CancelScope() as cs:
cs.cancel()
with pytest.raises(RuntimeError):
await nursery.start(started_while_handling_multiple_cancelled)

# calling started() while handling an exception while handling Cancelled(s) raises an error immediately.
async def started_while_handling_exc_while_handling_cancelled(
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
) -> None:
try:
await _core.checkpoint()
except _core.Cancelled:
try:
raise ValueError
except ValueError:
task_status.started()
raise AssertionError() # pragma: no cover

async with _core.open_nursery() as nursery:
with _core.CancelScope() as cs:
cs.cancel()
with pytest.raises(RuntimeError):
await nursery.start(started_while_handling_exc_while_handling_cancelled)

# trying to start in a closed nursery raises an error immediately.
async with _core.open_nursery() as closed_nursery:
pass
t0 = _core.current_time()
2 changes: 1 addition & 1 deletion test-requirements.in
Original file line number Diff line number Diff line change
@@ -30,4 +30,4 @@ sortedcontainers
idna
outcome
sniffio
exceptiongroup >= 1.0.0rc9; python_version < "3.11"
exceptiongroup >= 1.0.0rc9
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ cryptography==41.0.5
# types-pyopenssl
dill==0.3.7
# via pylint
exceptiongroup==1.1.3 ; python_version < "3.11"
exceptiongroup==1.1.3
# via
# -r test-requirements.in
# pytest