Skip to content

Commit 39bc92a

Browse files
committed
Test that TaskStatus.started() does not allow teleporting Cancelled(s) around the tree
1 parent d00e115 commit 39bc92a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/trio/_core/_tests/test_run.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Callable,
4141
Generator,
4242
)
43+
from exceptiongroup import catch
4344

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

1848-
# trying to start in a closed nursery raises an error immediately
1849+
# calling started() while handling a Cancelled raises an error immediately.
1850+
async def started_while_handling_cancelled(
1851+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
1852+
) -> None:
1853+
try:
1854+
await _core.checkpoint()
1855+
except _core.Cancelled:
1856+
task_status.started()
1857+
raise AssertionError() # pragma: no cover
1858+
1859+
async with _core.open_nursery() as nursery:
1860+
with _core.CancelScope() as cs:
1861+
cs.cancel()
1862+
with pytest.raises(RuntimeError):
1863+
await nursery.start(started_while_handling_cancelled)
1864+
1865+
# calling started() while handling multiple Cancelleds raises an error
1866+
# immediately.
1867+
async def started_while_handling_multiple_cancelled(
1868+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
1869+
) -> None:
1870+
with catch({_core.Cancelled: lambda _: task_status.started()}):
1871+
async with _core.open_nursery() as nursery:
1872+
nursery.start_soon(_core.checkpoint)
1873+
nursery.start_soon(_core.checkpoint)
1874+
raise AssertionError() # pragma: no cover
1875+
1876+
async with _core.open_nursery() as nursery:
1877+
with _core.CancelScope() as cs:
1878+
cs.cancel()
1879+
with pytest.raises(RuntimeError):
1880+
await nursery.start(started_while_handling_multiple_cancelled)
1881+
1882+
# calling started() while handling an exception while handling Cancelled(s) raises an error immediately.
1883+
async def started_while_handling_exc_while_handling_cancelled(
1884+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
1885+
) -> None:
1886+
try:
1887+
await _core.checkpoint()
1888+
except _core.Cancelled:
1889+
try:
1890+
raise ValueError
1891+
except ValueError:
1892+
task_status.started()
1893+
raise AssertionError() # pragma: no cover
1894+
1895+
async with _core.open_nursery() as nursery:
1896+
with _core.CancelScope() as cs:
1897+
cs.cancel()
1898+
with pytest.raises(RuntimeError):
1899+
await nursery.start(started_while_handling_exc_while_handling_cancelled)
1900+
1901+
# trying to start in a closed nursery raises an error immediately.
18491902
async with _core.open_nursery() as closed_nursery:
18501903
pass
18511904
t0 = _core.current_time()

test-requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ sortedcontainers
3030
idna
3131
outcome
3232
sniffio
33-
exceptiongroup >= 1.0.0rc9; python_version < "3.11"
33+
exceptiongroup >= 1.0.0rc9

test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ cryptography==41.0.5
3636
# types-pyopenssl
3737
dill==0.3.7
3838
# via pylint
39-
exceptiongroup==1.1.3 ; python_version < "3.11"
39+
exceptiongroup==1.1.3
4040
# via
4141
# -r test-requirements.in
4242
# pytest

0 commit comments

Comments
 (0)