@ajksunkang-aios/kgraph-linux-x64 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/kgraph-launcher +15 -3
- package/lib/kgraph/scripts/build-bundle.sh +17 -4
- package/lib/site-packages/outcome/__init__.py +20 -0
- package/lib/site-packages/outcome/_impl.py +239 -0
- package/lib/site-packages/outcome/_util.py +33 -0
- package/lib/site-packages/outcome/_version.py +7 -0
- package/lib/site-packages/outcome/py.typed +0 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/INSTALLER +1 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE +3 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE.APACHE2 +202 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/LICENSE.MIT +20 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/METADATA +63 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/RECORD +13 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/WHEEL +6 -0
- package/lib/site-packages/outcome-1.3.0.post0.dist-info/top_level.txt +1 -0
- package/lib/site-packages/sniffio/__init__.py +17 -0
- package/lib/site-packages/sniffio/_impl.py +95 -0
- package/lib/site-packages/sniffio/_tests/__init__.py +0 -0
- package/lib/site-packages/sniffio/_tests/test_sniffio.py +84 -0
- package/lib/site-packages/sniffio/_version.py +3 -0
- package/lib/site-packages/sniffio/py.typed +0 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/INSTALLER +1 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE +3 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE.APACHE2 +202 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/LICENSE.MIT +20 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/METADATA +104 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/RECORD +14 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/WHEEL +5 -0
- package/lib/site-packages/sniffio-1.3.1.dist-info/top_level.txt +1 -0
- package/lib/site-packages/sortedcontainers/__init__.py +74 -0
- package/lib/site-packages/sortedcontainers/sorteddict.py +812 -0
- package/lib/site-packages/sortedcontainers/sortedlist.py +2646 -0
- package/lib/site-packages/sortedcontainers/sortedset.py +733 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/INSTALLER +1 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/LICENSE +13 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/METADATA +264 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/RECORD +10 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/WHEEL +6 -0
- package/lib/site-packages/sortedcontainers-2.4.0.dist-info/top_level.txt +1 -0
- package/lib/site-packages/trio/__init__.py +133 -0
- package/lib/site-packages/trio/__main__.py +3 -0
- package/lib/site-packages/trio/_abc.py +714 -0
- package/lib/site-packages/trio/_channel.py +610 -0
- package/lib/site-packages/trio/_core/__init__.py +94 -0
- package/lib/site-packages/trio/_core/_asyncgens.py +243 -0
- package/lib/site-packages/trio/_core/_concat_tb.py +26 -0
- package/lib/site-packages/trio/_core/_entry_queue.py +223 -0
- package/lib/site-packages/trio/_core/_exceptions.py +169 -0
- package/lib/site-packages/trio/_core/_generated_instrumentation.py +50 -0
- package/lib/site-packages/trio/_core/_generated_io_epoll.py +98 -0
- package/lib/site-packages/trio/_core/_generated_io_kqueue.py +153 -0
- package/lib/site-packages/trio/_core/_generated_io_windows.py +204 -0
- package/lib/site-packages/trio/_core/_generated_run.py +269 -0
- package/lib/site-packages/trio/_core/_generated_windows_ffi.py +10 -0
- package/lib/site-packages/trio/_core/_instrumentation.py +117 -0
- package/lib/site-packages/trio/_core/_io_common.py +31 -0
- package/lib/site-packages/trio/_core/_io_epoll.py +385 -0
- package/lib/site-packages/trio/_core/_io_kqueue.py +292 -0
- package/lib/site-packages/trio/_core/_io_windows.py +1036 -0
- package/lib/site-packages/trio/_core/_ki.py +271 -0
- package/lib/site-packages/trio/_core/_local.py +104 -0
- package/lib/site-packages/trio/_core/_mock_clock.py +165 -0
- package/lib/site-packages/trio/_core/_parking_lot.py +317 -0
- package/lib/site-packages/trio/_core/_run.py +3148 -0
- package/lib/site-packages/trio/_core/_run_context.py +15 -0
- package/lib/site-packages/trio/_core/_tests/__init__.py +0 -0
- package/lib/site-packages/trio/_core/_tests/test_asyncgen.py +339 -0
- package/lib/site-packages/trio/_core/_tests/test_cancelled.py +222 -0
- package/lib/site-packages/trio/_core/_tests/test_exceptiongroup_gc.py +103 -0
- package/lib/site-packages/trio/_core/_tests/test_guest_mode.py +755 -0
- package/lib/site-packages/trio/_core/_tests/test_instrumentation.py +315 -0
- package/lib/site-packages/trio/_core/_tests/test_io.py +522 -0
- package/lib/site-packages/trio/_core/_tests/test_ki.py +703 -0
- package/lib/site-packages/trio/_core/_tests/test_local.py +118 -0
- package/lib/site-packages/trio/_core/_tests/test_mock_clock.py +193 -0
- package/lib/site-packages/trio/_core/_tests/test_parking_lot.py +389 -0
- package/lib/site-packages/trio/_core/_tests/test_run.py +3024 -0
- package/lib/site-packages/trio/_core/_tests/test_thread_cache.py +227 -0
- package/lib/site-packages/trio/_core/_tests/test_tutil.py +13 -0
- package/lib/site-packages/trio/_core/_tests/test_unbounded_queue.py +154 -0
- package/lib/site-packages/trio/_core/_tests/test_windows.py +305 -0
- package/lib/site-packages/trio/_core/_tests/tutil.py +117 -0
- package/lib/site-packages/trio/_core/_tests/type_tests/nursery_start.py +79 -0
- package/lib/site-packages/trio/_core/_tests/type_tests/run.py +51 -0
- package/lib/site-packages/trio/_core/_thread_cache.py +317 -0
- package/lib/site-packages/trio/_core/_traps.py +318 -0
- package/lib/site-packages/trio/_core/_unbounded_queue.py +163 -0
- package/lib/site-packages/trio/_core/_wakeup_socketpair.py +75 -0
- package/lib/site-packages/trio/_core/_windows_cffi.py +313 -0
- package/lib/site-packages/trio/_deprecate.py +171 -0
- package/lib/site-packages/trio/_dtls.py +1380 -0
- package/lib/site-packages/trio/_file_io.py +513 -0
- package/lib/site-packages/trio/_highlevel_generic.py +125 -0
- package/lib/site-packages/trio/_highlevel_open_tcp_listeners.py +251 -0
- package/lib/site-packages/trio/_highlevel_open_tcp_stream.py +397 -0
- package/lib/site-packages/trio/_highlevel_open_unix_stream.py +65 -0
- package/lib/site-packages/trio/_highlevel_serve_listeners.py +148 -0
- package/lib/site-packages/trio/_highlevel_socket.py +423 -0
- package/lib/site-packages/trio/_highlevel_ssl_helpers.py +180 -0
- package/lib/site-packages/trio/_path.py +289 -0
- package/lib/site-packages/trio/_repl.py +159 -0
- package/lib/site-packages/trio/_signals.py +185 -0
- package/lib/site-packages/trio/_socket.py +1326 -0
- package/lib/site-packages/trio/_ssl.py +964 -0
- package/lib/site-packages/trio/_subprocess.py +1178 -0
- package/lib/site-packages/trio/_subprocess_platform/__init__.py +123 -0
- package/lib/site-packages/trio/_subprocess_platform/kqueue.py +48 -0
- package/lib/site-packages/trio/_subprocess_platform/waitid.py +113 -0
- package/lib/site-packages/trio/_subprocess_platform/windows.py +11 -0
- package/lib/site-packages/trio/_sync.py +908 -0
- package/lib/site-packages/trio/_tests/__init__.py +0 -0
- package/lib/site-packages/trio/_tests/astrill-codesigning-cert.cer +0 -0
- package/lib/site-packages/trio/_tests/check_type_completeness.py +247 -0
- package/lib/site-packages/trio/_tests/module_with_deprecations.py +22 -0
- package/lib/site-packages/trio/_tests/pytest_plugin.py +54 -0
- package/lib/site-packages/trio/_tests/test_abc.py +72 -0
- package/lib/site-packages/trio/_tests/test_channel.py +750 -0
- package/lib/site-packages/trio/_tests/test_contextvars.py +56 -0
- package/lib/site-packages/trio/_tests/test_deprecate.py +277 -0
- package/lib/site-packages/trio/_tests/test_deprecate_strict_exception_groups_false.py +64 -0
- package/lib/site-packages/trio/_tests/test_dtls.py +950 -0
- package/lib/site-packages/trio/_tests/test_exports.py +626 -0
- package/lib/site-packages/trio/_tests/test_fakenet.py +317 -0
- package/lib/site-packages/trio/_tests/test_file_io.py +269 -0
- package/lib/site-packages/trio/_tests/test_highlevel_generic.py +98 -0
- package/lib/site-packages/trio/_tests/test_highlevel_open_tcp_listeners.py +419 -0
- package/lib/site-packages/trio/_tests/test_highlevel_open_tcp_stream.py +693 -0
- package/lib/site-packages/trio/_tests/test_highlevel_open_unix_stream.py +86 -0
- package/lib/site-packages/trio/_tests/test_highlevel_serve_listeners.py +186 -0
- package/lib/site-packages/trio/_tests/test_highlevel_socket.py +336 -0
- package/lib/site-packages/trio/_tests/test_highlevel_ssl_helpers.py +169 -0
- package/lib/site-packages/trio/_tests/test_path.py +279 -0
- package/lib/site-packages/trio/_tests/test_repl.py +428 -0
- package/lib/site-packages/trio/_tests/test_scheduler_determinism.py +47 -0
- package/lib/site-packages/trio/_tests/test_signals.py +186 -0
- package/lib/site-packages/trio/_tests/test_socket.py +1253 -0
- package/lib/site-packages/trio/_tests/test_ssl.py +1371 -0
- package/lib/site-packages/trio/_tests/test_subprocess.py +767 -0
- package/lib/site-packages/trio/_tests/test_sync.py +735 -0
- package/lib/site-packages/trio/_tests/test_testing.py +682 -0
- package/lib/site-packages/trio/_tests/test_testing_raisesgroup.py +1128 -0
- package/lib/site-packages/trio/_tests/test_threads.py +1173 -0
- package/lib/site-packages/trio/_tests/test_timeouts.py +281 -0
- package/lib/site-packages/trio/_tests/test_tracing.py +88 -0
- package/lib/site-packages/trio/_tests/test_trio.py +8 -0
- package/lib/site-packages/trio/_tests/test_unix_pipes.py +288 -0
- package/lib/site-packages/trio/_tests/test_util.py +349 -0
- package/lib/site-packages/trio/_tests/test_wait_for_object.py +225 -0
- package/lib/site-packages/trio/_tests/test_windows_pipes.py +112 -0
- package/lib/site-packages/trio/_tests/tools/__init__.py +0 -0
- package/lib/site-packages/trio/_tests/tools/test_gen_exports.py +179 -0
- package/lib/site-packages/trio/_tests/tools/test_mypy_annotate.py +140 -0
- package/lib/site-packages/trio/_tests/tools/test_sync_requirements.py +80 -0
- package/lib/site-packages/trio/_tests/type_tests/check_wraps.py +9 -0
- package/lib/site-packages/trio/_tests/type_tests/open_memory_channel.py +4 -0
- package/lib/site-packages/trio/_tests/type_tests/path.py +140 -0
- package/lib/site-packages/trio/_tests/type_tests/subprocesses.py +23 -0
- package/lib/site-packages/trio/_tests/type_tests/task_status.py +29 -0
- package/lib/site-packages/trio/_threads.py +610 -0
- package/lib/site-packages/trio/_timeouts.py +197 -0
- package/lib/site-packages/trio/_tools/__init__.py +0 -0
- package/lib/site-packages/trio/_tools/gen_exports.py +401 -0
- package/lib/site-packages/trio/_tools/mypy_annotate.py +126 -0
- package/lib/site-packages/trio/_tools/sync_requirements.py +98 -0
- package/lib/site-packages/trio/_tools/windows_ffi_build.py +220 -0
- package/lib/site-packages/trio/_unix_pipes.py +197 -0
- package/lib/site-packages/trio/_util.py +385 -0
- package/lib/site-packages/trio/_version.py +3 -0
- package/lib/site-packages/trio/_wait_for_object.py +67 -0
- package/lib/site-packages/trio/_windows_pipes.py +144 -0
- package/lib/site-packages/trio/abc.py +23 -0
- package/lib/site-packages/trio/from_thread.py +13 -0
- package/lib/site-packages/trio/lowlevel.py +95 -0
- package/lib/site-packages/trio/py.typed +0 -0
- package/lib/site-packages/trio/socket.py +602 -0
- package/lib/site-packages/trio/testing/__init__.py +58 -0
- package/lib/site-packages/trio/testing/_check_streams.py +570 -0
- package/lib/site-packages/trio/testing/_checkpoints.py +69 -0
- package/lib/site-packages/trio/testing/_fake_net.py +584 -0
- package/lib/site-packages/trio/testing/_memory_streams.py +633 -0
- package/lib/site-packages/trio/testing/_network.py +36 -0
- package/lib/site-packages/trio/testing/_raises_group.py +1015 -0
- package/lib/site-packages/trio/testing/_sequencer.py +87 -0
- package/lib/site-packages/trio/testing/_trio_test.py +50 -0
- package/lib/site-packages/trio/to_thread.py +4 -0
- package/lib/site-packages/trio-0.33.0.dist-info/INSTALLER +1 -0
- package/lib/site-packages/trio-0.33.0.dist-info/METADATA +186 -0
- package/lib/site-packages/trio-0.33.0.dist-info/RECORD +156 -0
- package/lib/site-packages/trio-0.33.0.dist-info/REQUESTED +0 -0
- package/lib/site-packages/trio-0.33.0.dist-info/WHEEL +5 -0
- package/lib/site-packages/trio-0.33.0.dist-info/entry_points.txt +2 -0
- package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE +3 -0
- package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE.APACHE2 +202 -0
- package/lib/site-packages/trio-0.33.0.dist-info/licenses/LICENSE.MIT +22 -0
- package/lib/site-packages/trio-0.33.0.dist-info/top_level.txt +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,3024 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import contextvars
|
|
4
|
+
import functools
|
|
5
|
+
import gc
|
|
6
|
+
import sys
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
import types
|
|
10
|
+
import weakref
|
|
11
|
+
from contextlib import (
|
|
12
|
+
AsyncExitStack,
|
|
13
|
+
ExitStack,
|
|
14
|
+
asynccontextmanager,
|
|
15
|
+
contextmanager,
|
|
16
|
+
suppress,
|
|
17
|
+
)
|
|
18
|
+
from math import inf, nan
|
|
19
|
+
from typing import TYPE_CHECKING, NoReturn, TypeVar
|
|
20
|
+
from unittest import mock
|
|
21
|
+
|
|
22
|
+
import outcome
|
|
23
|
+
import pytest
|
|
24
|
+
import sniffio
|
|
25
|
+
|
|
26
|
+
from ... import _core
|
|
27
|
+
from ..._threads import to_thread_run_sync
|
|
28
|
+
from ..._timeouts import fail_after, sleep
|
|
29
|
+
from ...testing import (
|
|
30
|
+
Sequencer,
|
|
31
|
+
assert_checkpoints,
|
|
32
|
+
wait_all_tasks_blocked,
|
|
33
|
+
)
|
|
34
|
+
from .._run import DEADLINE_HEAP_MIN_PRUNE_THRESHOLD, _count_context_run_tb_frames
|
|
35
|
+
from .tutil import (
|
|
36
|
+
check_sequence_matches,
|
|
37
|
+
create_asyncio_future_in_new_loop,
|
|
38
|
+
gc_collect_harder,
|
|
39
|
+
ignore_coroutine_never_awaited_warnings,
|
|
40
|
+
restore_unraisablehook,
|
|
41
|
+
slow,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if TYPE_CHECKING:
|
|
45
|
+
from collections.abc import (
|
|
46
|
+
AsyncGenerator,
|
|
47
|
+
AsyncIterator,
|
|
48
|
+
Awaitable,
|
|
49
|
+
Callable,
|
|
50
|
+
Generator,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if sys.version_info < (3, 11):
|
|
54
|
+
from exceptiongroup import BaseExceptionGroup, ExceptionGroup
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
T = TypeVar("T")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# slightly different from _timeouts.sleep_forever because it returns the value
|
|
61
|
+
# its rescheduled with, which is really only useful for tests of
|
|
62
|
+
# rescheduling...
|
|
63
|
+
async def sleep_forever() -> object:
|
|
64
|
+
return await _core.wait_task_rescheduled(lambda _: _core.Abort.SUCCEEDED)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def not_none(x: T | None) -> T:
|
|
68
|
+
"""Assert that this object is not None.
|
|
69
|
+
|
|
70
|
+
This is just to satisfy type checkers, if this ever fails the test is broken.
|
|
71
|
+
"""
|
|
72
|
+
assert x is not None
|
|
73
|
+
return x
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_basic() -> None:
|
|
77
|
+
async def trivial(x: T) -> T:
|
|
78
|
+
return x
|
|
79
|
+
|
|
80
|
+
assert _core.run(trivial, 8) == 8
|
|
81
|
+
|
|
82
|
+
with pytest.raises(TypeError):
|
|
83
|
+
# Missing an argument
|
|
84
|
+
_core.run(trivial) # type: ignore[arg-type]
|
|
85
|
+
|
|
86
|
+
with pytest.raises(TypeError):
|
|
87
|
+
# Not an async function
|
|
88
|
+
_core.run(lambda: None) # type: ignore
|
|
89
|
+
|
|
90
|
+
async def trivial2(x: T) -> T:
|
|
91
|
+
await _core.checkpoint()
|
|
92
|
+
return x
|
|
93
|
+
|
|
94
|
+
assert _core.run(trivial2, 1) == 1
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_initial_task_error() -> None:
|
|
98
|
+
async def main(x: object) -> NoReturn:
|
|
99
|
+
raise ValueError(x)
|
|
100
|
+
|
|
101
|
+
with pytest.raises(ValueError, match=r"^17$") as excinfo:
|
|
102
|
+
_core.run(main, 17)
|
|
103
|
+
assert excinfo.value.args == (17,)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_run_nesting() -> None:
|
|
107
|
+
async def inception() -> None:
|
|
108
|
+
async def main() -> None: # pragma: no cover
|
|
109
|
+
pass
|
|
110
|
+
|
|
111
|
+
return _core.run(main)
|
|
112
|
+
|
|
113
|
+
with pytest.raises(RuntimeError) as excinfo:
|
|
114
|
+
_core.run(inception)
|
|
115
|
+
assert "from inside" in str(excinfo.value)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def test_nursery_warn_use_async_with() -> None:
|
|
119
|
+
on = _core.open_nursery()
|
|
120
|
+
with pytest.raises(RuntimeError) as excinfo:
|
|
121
|
+
with on: # type: ignore
|
|
122
|
+
pass # pragma: no cover
|
|
123
|
+
excinfo.match(
|
|
124
|
+
r"use 'async with open_nursery\(...\)', not 'with open_nursery\(...\)'",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# avoid unawaited coro.
|
|
128
|
+
async with on:
|
|
129
|
+
pass
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
async def test_nursery_main_block_error_basic() -> None:
|
|
133
|
+
exc = ValueError("whoops")
|
|
134
|
+
|
|
135
|
+
with pytest.RaisesGroup(pytest.RaisesExc(check=lambda e: e is exc)):
|
|
136
|
+
async with _core.open_nursery():
|
|
137
|
+
raise exc
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
async def test_child_crash_basic() -> None:
|
|
141
|
+
my_exc = ValueError("uh oh")
|
|
142
|
+
|
|
143
|
+
async def erroring() -> NoReturn:
|
|
144
|
+
raise my_exc
|
|
145
|
+
|
|
146
|
+
with pytest.RaisesGroup(pytest.RaisesExc(check=lambda e: e is my_exc)):
|
|
147
|
+
# nursery.__aexit__ propagates exception from child back to parent
|
|
148
|
+
async with _core.open_nursery() as nursery:
|
|
149
|
+
nursery.start_soon(erroring)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
async def test_basic_interleave() -> None:
|
|
153
|
+
async def looper(whoami: str, record: list[tuple[str, int]]) -> None:
|
|
154
|
+
for i in range(3):
|
|
155
|
+
record.append((whoami, i))
|
|
156
|
+
await _core.checkpoint()
|
|
157
|
+
|
|
158
|
+
record: list[tuple[str, int]] = []
|
|
159
|
+
async with _core.open_nursery() as nursery:
|
|
160
|
+
nursery.start_soon(looper, "a", record)
|
|
161
|
+
nursery.start_soon(looper, "b", record)
|
|
162
|
+
|
|
163
|
+
check_sequence_matches(
|
|
164
|
+
record,
|
|
165
|
+
[{("a", 0), ("b", 0)}, {("a", 1), ("b", 1)}, {("a", 2), ("b", 2)}],
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_task_crash_propagation() -> None:
|
|
170
|
+
looper_record: list[str] = []
|
|
171
|
+
|
|
172
|
+
async def looper() -> None:
|
|
173
|
+
try:
|
|
174
|
+
while True:
|
|
175
|
+
await _core.checkpoint()
|
|
176
|
+
except _core.Cancelled:
|
|
177
|
+
print("looper cancelled")
|
|
178
|
+
looper_record.append("cancelled")
|
|
179
|
+
|
|
180
|
+
async def crasher() -> NoReturn:
|
|
181
|
+
raise ValueError("argh")
|
|
182
|
+
|
|
183
|
+
async def main() -> None:
|
|
184
|
+
async with _core.open_nursery() as nursery:
|
|
185
|
+
nursery.start_soon(looper)
|
|
186
|
+
nursery.start_soon(crasher)
|
|
187
|
+
|
|
188
|
+
with pytest.RaisesGroup(pytest.RaisesExc(ValueError, match="^argh$")):
|
|
189
|
+
_core.run(main)
|
|
190
|
+
|
|
191
|
+
assert looper_record == ["cancelled"]
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_main_and_task_both_crash() -> None:
|
|
195
|
+
# If main crashes and there's also a task crash, then we get both in an
|
|
196
|
+
# ExceptionGroup
|
|
197
|
+
async def crasher() -> NoReturn:
|
|
198
|
+
raise ValueError
|
|
199
|
+
|
|
200
|
+
async def main() -> NoReturn:
|
|
201
|
+
async with _core.open_nursery() as nursery:
|
|
202
|
+
nursery.start_soon(crasher)
|
|
203
|
+
raise KeyError
|
|
204
|
+
|
|
205
|
+
with pytest.RaisesGroup(ValueError, KeyError):
|
|
206
|
+
_core.run(main)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def test_two_child_crashes() -> None:
|
|
210
|
+
async def crasher(etype: type[Exception]) -> NoReturn:
|
|
211
|
+
raise etype
|
|
212
|
+
|
|
213
|
+
async def main() -> None:
|
|
214
|
+
async with _core.open_nursery() as nursery:
|
|
215
|
+
nursery.start_soon(crasher, KeyError)
|
|
216
|
+
nursery.start_soon(crasher, ValueError)
|
|
217
|
+
|
|
218
|
+
with pytest.RaisesGroup(ValueError, KeyError):
|
|
219
|
+
_core.run(main)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
async def test_child_crash_wakes_parent() -> None:
|
|
223
|
+
async def crasher() -> NoReturn:
|
|
224
|
+
raise ValueError("this is a crash")
|
|
225
|
+
|
|
226
|
+
with pytest.RaisesGroup(pytest.RaisesExc(ValueError, match="^this is a crash$")):
|
|
227
|
+
async with _core.open_nursery() as nursery:
|
|
228
|
+
nursery.start_soon(crasher)
|
|
229
|
+
await sleep_forever()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
async def test_reschedule() -> None:
|
|
233
|
+
t1: _core.Task | None = None
|
|
234
|
+
t2: _core.Task | None = None
|
|
235
|
+
|
|
236
|
+
async def child1() -> None:
|
|
237
|
+
nonlocal t1, t2
|
|
238
|
+
t1 = _core.current_task()
|
|
239
|
+
print("child1 start")
|
|
240
|
+
x = await sleep_forever()
|
|
241
|
+
print("child1 woke")
|
|
242
|
+
assert x == 0
|
|
243
|
+
print("child1 rescheduling t2")
|
|
244
|
+
_core.reschedule(not_none(t2), outcome.Error(ValueError("error message")))
|
|
245
|
+
print("child1 exit")
|
|
246
|
+
|
|
247
|
+
async def child2() -> None:
|
|
248
|
+
nonlocal t1, t2
|
|
249
|
+
print("child2 start")
|
|
250
|
+
t2 = _core.current_task()
|
|
251
|
+
_core.reschedule(not_none(t1), outcome.Value(0))
|
|
252
|
+
print("child2 sleep")
|
|
253
|
+
with pytest.raises(ValueError, match=r"^error message$"):
|
|
254
|
+
await sleep_forever()
|
|
255
|
+
print("child2 successful exit")
|
|
256
|
+
|
|
257
|
+
async with _core.open_nursery() as nursery:
|
|
258
|
+
nursery.start_soon(child1)
|
|
259
|
+
# let t1 run and fall asleep
|
|
260
|
+
await _core.checkpoint()
|
|
261
|
+
nursery.start_soon(child2)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
async def test_current_time() -> None:
|
|
265
|
+
t1 = _core.current_time()
|
|
266
|
+
# Windows clock is pretty low-resolution -- appveyor tests fail unless we
|
|
267
|
+
# sleep for a bit here.
|
|
268
|
+
time.sleep(time.get_clock_info("perf_counter").resolution) # noqa: ASYNC251
|
|
269
|
+
t2 = _core.current_time()
|
|
270
|
+
assert t1 < t2
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
async def test_current_time_with_mock_clock(mock_clock: _core.MockClock) -> None:
|
|
274
|
+
start = mock_clock.current_time()
|
|
275
|
+
assert mock_clock.current_time() == _core.current_time()
|
|
276
|
+
assert mock_clock.current_time() == _core.current_time()
|
|
277
|
+
mock_clock.jump(3.15)
|
|
278
|
+
assert start + 3.15 == mock_clock.current_time() == _core.current_time()
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
async def test_current_clock(mock_clock: _core.MockClock) -> None:
|
|
282
|
+
assert mock_clock is _core.current_clock()
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
async def test_current_task() -> None:
|
|
286
|
+
parent_task = _core.current_task()
|
|
287
|
+
|
|
288
|
+
async def child() -> None:
|
|
289
|
+
assert not_none(_core.current_task().parent_nursery).parent_task is parent_task
|
|
290
|
+
|
|
291
|
+
async with _core.open_nursery() as nursery:
|
|
292
|
+
nursery.start_soon(child)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
async def test_root_task() -> None:
|
|
296
|
+
root = not_none(_core.current_root_task())
|
|
297
|
+
assert root.parent_nursery is root.eventual_parent_nursery is None
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def test_out_of_context() -> None:
|
|
301
|
+
with pytest.raises(RuntimeError):
|
|
302
|
+
_core.current_task()
|
|
303
|
+
with pytest.raises(RuntimeError):
|
|
304
|
+
_core.current_time()
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
async def test_current_statistics(mock_clock: _core.MockClock) -> None:
|
|
308
|
+
# Make sure all the early startup stuff has settled down
|
|
309
|
+
await wait_all_tasks_blocked()
|
|
310
|
+
|
|
311
|
+
# A child that sticks around to make some interesting stats:
|
|
312
|
+
async def child() -> None:
|
|
313
|
+
with suppress(_core.Cancelled):
|
|
314
|
+
await sleep_forever()
|
|
315
|
+
|
|
316
|
+
stats = _core.current_statistics()
|
|
317
|
+
print(stats)
|
|
318
|
+
# 2 system tasks + us
|
|
319
|
+
assert stats.tasks_living == 3
|
|
320
|
+
assert stats.run_sync_soon_queue_size == 0
|
|
321
|
+
|
|
322
|
+
async with _core.open_nursery() as nursery:
|
|
323
|
+
nursery.start_soon(child)
|
|
324
|
+
await wait_all_tasks_blocked()
|
|
325
|
+
token = _core.current_trio_token()
|
|
326
|
+
token.run_sync_soon(lambda: None)
|
|
327
|
+
token.run_sync_soon(lambda: None, idempotent=True)
|
|
328
|
+
stats = _core.current_statistics()
|
|
329
|
+
print(stats)
|
|
330
|
+
# 2 system tasks + us + child
|
|
331
|
+
assert stats.tasks_living == 4
|
|
332
|
+
# the exact value here might shift if we change how we do accounting
|
|
333
|
+
# (currently it only counts tasks that we already know will be
|
|
334
|
+
# runnable on the next pass), but still useful to at least test the
|
|
335
|
+
# difference between now and after we wake up the child:
|
|
336
|
+
assert stats.tasks_runnable == 0
|
|
337
|
+
assert stats.run_sync_soon_queue_size == 2
|
|
338
|
+
|
|
339
|
+
nursery.cancel_scope.cancel()
|
|
340
|
+
stats = _core.current_statistics()
|
|
341
|
+
print(stats)
|
|
342
|
+
assert stats.tasks_runnable == 1
|
|
343
|
+
|
|
344
|
+
# Give the child a chance to die and the run_sync_soon a chance to clear
|
|
345
|
+
await _core.checkpoint()
|
|
346
|
+
await _core.checkpoint()
|
|
347
|
+
|
|
348
|
+
with _core.CancelScope(deadline=_core.current_time() + 5):
|
|
349
|
+
stats = _core.current_statistics()
|
|
350
|
+
print(stats)
|
|
351
|
+
assert stats.seconds_to_next_deadline == 5
|
|
352
|
+
stats = _core.current_statistics()
|
|
353
|
+
print(stats)
|
|
354
|
+
assert stats.seconds_to_next_deadline == inf
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
async def test_cancel_scope_repr(mock_clock: _core.MockClock) -> None:
|
|
358
|
+
scope = _core.CancelScope()
|
|
359
|
+
assert "unbound" in repr(scope)
|
|
360
|
+
with scope:
|
|
361
|
+
assert "active" in repr(scope)
|
|
362
|
+
scope.deadline = _core.current_time() - 1
|
|
363
|
+
assert "deadline is 1.00 seconds ago" in repr(scope)
|
|
364
|
+
scope.deadline = _core.current_time() + 10
|
|
365
|
+
assert "deadline is 10.00 seconds from now" in repr(scope)
|
|
366
|
+
# when not in async context, can't get the current time
|
|
367
|
+
assert "deadline" not in await to_thread_run_sync(repr, scope)
|
|
368
|
+
scope.cancel()
|
|
369
|
+
assert "cancelled" in repr(scope)
|
|
370
|
+
assert "exited" in repr(scope)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
async def test_cancel_scope_validation() -> None:
|
|
374
|
+
with pytest.raises(
|
|
375
|
+
ValueError,
|
|
376
|
+
match=r"^Cannot specify both a deadline and a relative deadline$",
|
|
377
|
+
):
|
|
378
|
+
_core.CancelScope(deadline=7, relative_deadline=3)
|
|
379
|
+
|
|
380
|
+
with pytest.raises(ValueError, match=r"^deadline must not be NaN$"):
|
|
381
|
+
_core.CancelScope(deadline=nan)
|
|
382
|
+
with pytest.raises(ValueError, match=r"^relative deadline must not be NaN$"):
|
|
383
|
+
_core.CancelScope(relative_deadline=nan)
|
|
384
|
+
|
|
385
|
+
with pytest.raises(ValueError, match=r"^timeout must be non-negative$"):
|
|
386
|
+
_core.CancelScope(relative_deadline=-3)
|
|
387
|
+
|
|
388
|
+
scope = _core.CancelScope()
|
|
389
|
+
|
|
390
|
+
with pytest.raises(ValueError, match=r"^deadline must not be NaN$"):
|
|
391
|
+
scope.deadline = nan
|
|
392
|
+
with pytest.raises(ValueError, match=r"^relative deadline must not be NaN$"):
|
|
393
|
+
scope.relative_deadline = nan
|
|
394
|
+
|
|
395
|
+
with pytest.raises(ValueError, match=r"^relative deadline must be non-negative$"):
|
|
396
|
+
scope.relative_deadline = -3
|
|
397
|
+
scope.relative_deadline = 5
|
|
398
|
+
assert scope.relative_deadline == 5
|
|
399
|
+
|
|
400
|
+
# several related tests of CancelScope are implicitly handled by test_timeouts.py
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def test_cancel_points() -> None:
|
|
404
|
+
async def main1() -> None:
|
|
405
|
+
with _core.CancelScope() as scope:
|
|
406
|
+
await _core.checkpoint_if_cancelled()
|
|
407
|
+
scope.cancel()
|
|
408
|
+
with pytest.raises(_core.Cancelled):
|
|
409
|
+
await _core.checkpoint_if_cancelled()
|
|
410
|
+
|
|
411
|
+
_core.run(main1)
|
|
412
|
+
|
|
413
|
+
async def main2() -> None:
|
|
414
|
+
with _core.CancelScope() as scope:
|
|
415
|
+
await _core.checkpoint()
|
|
416
|
+
scope.cancel()
|
|
417
|
+
with pytest.raises(_core.Cancelled):
|
|
418
|
+
await _core.checkpoint()
|
|
419
|
+
|
|
420
|
+
_core.run(main2)
|
|
421
|
+
|
|
422
|
+
async def main3() -> None:
|
|
423
|
+
with _core.CancelScope() as scope:
|
|
424
|
+
scope.cancel()
|
|
425
|
+
with pytest.raises(_core.Cancelled):
|
|
426
|
+
await sleep_forever()
|
|
427
|
+
|
|
428
|
+
_core.run(main3)
|
|
429
|
+
|
|
430
|
+
async def main4() -> None:
|
|
431
|
+
with _core.CancelScope() as scope:
|
|
432
|
+
scope.cancel()
|
|
433
|
+
await _core.cancel_shielded_checkpoint()
|
|
434
|
+
await _core.cancel_shielded_checkpoint()
|
|
435
|
+
with pytest.raises(_core.Cancelled):
|
|
436
|
+
await _core.checkpoint()
|
|
437
|
+
|
|
438
|
+
_core.run(main4)
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
async def test_cancel_edge_cases() -> None:
|
|
442
|
+
with _core.CancelScope() as scope:
|
|
443
|
+
# Two cancels in a row -- idempotent
|
|
444
|
+
scope.cancel()
|
|
445
|
+
scope.cancel()
|
|
446
|
+
await _core.checkpoint()
|
|
447
|
+
assert scope.cancel_called
|
|
448
|
+
assert scope.cancelled_caught
|
|
449
|
+
|
|
450
|
+
with _core.CancelScope() as scope:
|
|
451
|
+
# Check level-triggering
|
|
452
|
+
scope.cancel()
|
|
453
|
+
with pytest.raises(_core.Cancelled):
|
|
454
|
+
await sleep_forever()
|
|
455
|
+
with pytest.raises(_core.Cancelled):
|
|
456
|
+
await sleep_forever()
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
async def test_cancel_scope_exceptiongroup_filtering() -> None:
|
|
460
|
+
async def crasher() -> NoReturn:
|
|
461
|
+
raise KeyError
|
|
462
|
+
|
|
463
|
+
# This is outside the outer scope, so all the Cancelled
|
|
464
|
+
# exceptions should have been absorbed, leaving just a regular
|
|
465
|
+
# KeyError from crasher(), wrapped in an ExceptionGroup
|
|
466
|
+
with pytest.RaisesGroup(KeyError):
|
|
467
|
+
with _core.CancelScope() as outer:
|
|
468
|
+
# Since the outer scope became cancelled before the
|
|
469
|
+
# nursery block exited, all cancellations inside the
|
|
470
|
+
# nursery block continue propagating to reach the
|
|
471
|
+
# outer scope.
|
|
472
|
+
with pytest.RaisesGroup(
|
|
473
|
+
_core.Cancelled,
|
|
474
|
+
_core.Cancelled,
|
|
475
|
+
_core.Cancelled,
|
|
476
|
+
KeyError,
|
|
477
|
+
) as excinfo:
|
|
478
|
+
async with _core.open_nursery() as nursery:
|
|
479
|
+
# Two children that get cancelled by the nursery scope
|
|
480
|
+
nursery.start_soon(sleep_forever) # t1
|
|
481
|
+
nursery.start_soon(sleep_forever) # t2
|
|
482
|
+
nursery.cancel_scope.cancel()
|
|
483
|
+
with _core.CancelScope(shield=True):
|
|
484
|
+
await wait_all_tasks_blocked()
|
|
485
|
+
# One child that gets cancelled by the outer scope
|
|
486
|
+
nursery.start_soon(sleep_forever) # t3
|
|
487
|
+
outer.cancel()
|
|
488
|
+
# And one that raises a different error
|
|
489
|
+
nursery.start_soon(crasher) # t4
|
|
490
|
+
# and then our __aexit__ also receives an outer Cancelled
|
|
491
|
+
# reraise the exception caught by pytest.RaisesGroup for the
|
|
492
|
+
# CancelScope to handle
|
|
493
|
+
raise excinfo.value
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
async def test_precancelled_task() -> None:
|
|
497
|
+
# a task that gets spawned into an already-cancelled nursery should begin
|
|
498
|
+
# execution (https://github.com/python-trio/trio/issues/41), but get a
|
|
499
|
+
# cancelled error at its first blocking call.
|
|
500
|
+
record: list[str] = []
|
|
501
|
+
|
|
502
|
+
async def blocker() -> None:
|
|
503
|
+
record.append("started")
|
|
504
|
+
await sleep_forever()
|
|
505
|
+
|
|
506
|
+
async with _core.open_nursery() as nursery:
|
|
507
|
+
nursery.cancel_scope.cancel()
|
|
508
|
+
nursery.start_soon(blocker)
|
|
509
|
+
assert record == ["started"]
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
async def test_cancel_shielding() -> None:
|
|
513
|
+
with _core.CancelScope() as outer:
|
|
514
|
+
with _core.CancelScope() as inner:
|
|
515
|
+
await _core.checkpoint()
|
|
516
|
+
outer.cancel()
|
|
517
|
+
with pytest.raises(_core.Cancelled):
|
|
518
|
+
await _core.checkpoint()
|
|
519
|
+
|
|
520
|
+
assert inner.shield is False
|
|
521
|
+
with pytest.raises(TypeError):
|
|
522
|
+
inner.shield = "hello" # type: ignore
|
|
523
|
+
assert inner.shield is False
|
|
524
|
+
|
|
525
|
+
inner.shield = True
|
|
526
|
+
assert inner.shield is True
|
|
527
|
+
# shield protects us from 'outer'
|
|
528
|
+
await _core.checkpoint()
|
|
529
|
+
|
|
530
|
+
with _core.CancelScope() as innerest:
|
|
531
|
+
innerest.cancel()
|
|
532
|
+
# but it doesn't protect us from scope inside inner
|
|
533
|
+
with pytest.raises(_core.Cancelled):
|
|
534
|
+
await _core.checkpoint()
|
|
535
|
+
await _core.checkpoint()
|
|
536
|
+
|
|
537
|
+
inner.shield = False
|
|
538
|
+
# can disable shield again
|
|
539
|
+
with pytest.raises(_core.Cancelled):
|
|
540
|
+
await _core.checkpoint()
|
|
541
|
+
|
|
542
|
+
# re-enable shield
|
|
543
|
+
inner.shield = True
|
|
544
|
+
await _core.checkpoint()
|
|
545
|
+
# shield doesn't protect us from inner itself
|
|
546
|
+
inner.cancel()
|
|
547
|
+
# This should now raise, but be absorbed by the inner scope
|
|
548
|
+
await _core.checkpoint()
|
|
549
|
+
assert inner.cancelled_caught
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
# make sure that cancellation propagates immediately to all children
|
|
553
|
+
async def test_cancel_inheritance() -> None:
|
|
554
|
+
record: set[str] = set()
|
|
555
|
+
|
|
556
|
+
async def leaf(ident: str) -> None:
|
|
557
|
+
try:
|
|
558
|
+
await sleep_forever()
|
|
559
|
+
except _core.Cancelled:
|
|
560
|
+
record.add(ident)
|
|
561
|
+
|
|
562
|
+
async def worker(ident: str) -> None:
|
|
563
|
+
async with _core.open_nursery() as nursery:
|
|
564
|
+
nursery.start_soon(leaf, ident + "-l1")
|
|
565
|
+
nursery.start_soon(leaf, ident + "-l2")
|
|
566
|
+
|
|
567
|
+
async with _core.open_nursery() as nursery:
|
|
568
|
+
nursery.start_soon(worker, "w1")
|
|
569
|
+
nursery.start_soon(worker, "w2")
|
|
570
|
+
nursery.cancel_scope.cancel()
|
|
571
|
+
|
|
572
|
+
assert record == {"w1-l1", "w1-l2", "w2-l1", "w2-l2"}
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
async def test_cancel_shield_abort() -> None:
|
|
576
|
+
with _core.CancelScope() as outer:
|
|
577
|
+
async with _core.open_nursery() as nursery:
|
|
578
|
+
outer.cancel()
|
|
579
|
+
nursery.cancel_scope.shield = True
|
|
580
|
+
# The outer scope is cancelled, but this task is protected by the
|
|
581
|
+
# shield, so it manages to get to sleep
|
|
582
|
+
record = []
|
|
583
|
+
|
|
584
|
+
async def sleeper() -> None:
|
|
585
|
+
record.append("sleeping")
|
|
586
|
+
try:
|
|
587
|
+
await sleep_forever()
|
|
588
|
+
except _core.Cancelled:
|
|
589
|
+
record.append("cancelled")
|
|
590
|
+
|
|
591
|
+
nursery.start_soon(sleeper)
|
|
592
|
+
await wait_all_tasks_blocked()
|
|
593
|
+
assert record == ["sleeping"]
|
|
594
|
+
# now when we unshield, it should abort the sleep.
|
|
595
|
+
nursery.cancel_scope.shield = False
|
|
596
|
+
# wait for the task to finish before entering the nursery
|
|
597
|
+
# __aexit__, because __aexit__ could make it spuriously look like
|
|
598
|
+
# this worked by cancelling the nursery scope. (When originally
|
|
599
|
+
# written, without these last few lines, the test spuriously
|
|
600
|
+
# passed, even though shield assignment was buggy.)
|
|
601
|
+
with _core.CancelScope(shield=True):
|
|
602
|
+
await wait_all_tasks_blocked()
|
|
603
|
+
assert record == ["sleeping", "cancelled"]
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
async def test_basic_timeout(mock_clock: _core.MockClock) -> None:
|
|
607
|
+
start = _core.current_time()
|
|
608
|
+
with _core.CancelScope() as scope:
|
|
609
|
+
assert scope.deadline == inf
|
|
610
|
+
scope.deadline = start + 1
|
|
611
|
+
assert scope.deadline == start + 1
|
|
612
|
+
assert not scope.cancel_called
|
|
613
|
+
mock_clock.jump(2)
|
|
614
|
+
await _core.checkpoint()
|
|
615
|
+
await _core.checkpoint()
|
|
616
|
+
await _core.checkpoint()
|
|
617
|
+
assert not scope.cancel_called
|
|
618
|
+
|
|
619
|
+
start = _core.current_time()
|
|
620
|
+
with _core.CancelScope(deadline=start + 1) as scope:
|
|
621
|
+
mock_clock.jump(2)
|
|
622
|
+
await sleep_forever()
|
|
623
|
+
# But then the scope swallowed the exception... but we can still see it
|
|
624
|
+
# here:
|
|
625
|
+
assert scope.cancel_called
|
|
626
|
+
assert scope.cancelled_caught
|
|
627
|
+
|
|
628
|
+
# changing deadline
|
|
629
|
+
start = _core.current_time()
|
|
630
|
+
with _core.CancelScope() as scope:
|
|
631
|
+
await _core.checkpoint()
|
|
632
|
+
scope.deadline = start + 10
|
|
633
|
+
await _core.checkpoint()
|
|
634
|
+
mock_clock.jump(5)
|
|
635
|
+
await _core.checkpoint()
|
|
636
|
+
scope.deadline = start + 1
|
|
637
|
+
with pytest.raises(_core.Cancelled):
|
|
638
|
+
await _core.checkpoint()
|
|
639
|
+
with pytest.raises(_core.Cancelled):
|
|
640
|
+
await _core.checkpoint()
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
async def test_cancel_scope_nesting() -> None:
|
|
644
|
+
# Nested scopes: if two triggering at once, the outer one wins
|
|
645
|
+
with _core.CancelScope() as scope1:
|
|
646
|
+
with _core.CancelScope() as scope2:
|
|
647
|
+
with _core.CancelScope() as scope3:
|
|
648
|
+
scope3.cancel()
|
|
649
|
+
scope2.cancel()
|
|
650
|
+
await sleep_forever()
|
|
651
|
+
assert scope3.cancel_called
|
|
652
|
+
assert not scope3.cancelled_caught
|
|
653
|
+
assert scope2.cancel_called
|
|
654
|
+
assert scope2.cancelled_caught
|
|
655
|
+
assert not scope1.cancel_called
|
|
656
|
+
assert not scope1.cancelled_caught
|
|
657
|
+
|
|
658
|
+
# shielding
|
|
659
|
+
with _core.CancelScope() as scope1:
|
|
660
|
+
with _core.CancelScope() as scope2:
|
|
661
|
+
scope1.cancel()
|
|
662
|
+
with pytest.raises(_core.Cancelled):
|
|
663
|
+
await _core.checkpoint()
|
|
664
|
+
with pytest.raises(_core.Cancelled):
|
|
665
|
+
await _core.checkpoint()
|
|
666
|
+
scope2.shield = True
|
|
667
|
+
await _core.checkpoint()
|
|
668
|
+
scope2.cancel()
|
|
669
|
+
with pytest.raises(_core.Cancelled):
|
|
670
|
+
await _core.checkpoint()
|
|
671
|
+
|
|
672
|
+
# if a scope is pending, but then gets popped off the stack, then it
|
|
673
|
+
# isn't delivered
|
|
674
|
+
with _core.CancelScope() as scope:
|
|
675
|
+
scope.cancel()
|
|
676
|
+
await _core.cancel_shielded_checkpoint()
|
|
677
|
+
await _core.checkpoint()
|
|
678
|
+
assert not scope.cancelled_caught
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
# Regression test for https://github.com/python-trio/trio/issues/1175
|
|
682
|
+
async def test_unshield_while_cancel_propagating() -> None:
|
|
683
|
+
with _core.CancelScope() as outer:
|
|
684
|
+
with _core.CancelScope() as inner:
|
|
685
|
+
outer.cancel()
|
|
686
|
+
try:
|
|
687
|
+
await _core.checkpoint()
|
|
688
|
+
finally:
|
|
689
|
+
inner.shield = True
|
|
690
|
+
assert outer.cancelled_caught
|
|
691
|
+
assert not inner.cancelled_caught
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
async def test_cancel_unbound() -> None:
|
|
695
|
+
async def sleep_until_cancelled(scope: _core.CancelScope) -> None:
|
|
696
|
+
with scope, fail_after(1):
|
|
697
|
+
await sleep_forever()
|
|
698
|
+
|
|
699
|
+
# Cancel before entry
|
|
700
|
+
scope = _core.CancelScope()
|
|
701
|
+
scope.cancel()
|
|
702
|
+
async with _core.open_nursery() as nursery:
|
|
703
|
+
nursery.start_soon(sleep_until_cancelled, scope)
|
|
704
|
+
|
|
705
|
+
# Cancel after entry
|
|
706
|
+
scope = _core.CancelScope()
|
|
707
|
+
async with _core.open_nursery() as nursery:
|
|
708
|
+
nursery.start_soon(sleep_until_cancelled, scope)
|
|
709
|
+
await wait_all_tasks_blocked()
|
|
710
|
+
scope.cancel()
|
|
711
|
+
|
|
712
|
+
# Shield before entry
|
|
713
|
+
scope = _core.CancelScope()
|
|
714
|
+
scope.shield = True
|
|
715
|
+
with _core.CancelScope() as outer, scope:
|
|
716
|
+
outer.cancel()
|
|
717
|
+
await _core.checkpoint()
|
|
718
|
+
scope.shield = False
|
|
719
|
+
with pytest.raises(_core.Cancelled):
|
|
720
|
+
await _core.checkpoint()
|
|
721
|
+
|
|
722
|
+
# Can't reuse
|
|
723
|
+
with _core.CancelScope() as scope:
|
|
724
|
+
await _core.checkpoint()
|
|
725
|
+
scope.cancel()
|
|
726
|
+
await _core.checkpoint()
|
|
727
|
+
assert scope.cancel_called
|
|
728
|
+
assert not scope.cancelled_caught
|
|
729
|
+
with pytest.raises(RuntimeError) as exc_info:
|
|
730
|
+
with scope:
|
|
731
|
+
pass # pragma: no cover
|
|
732
|
+
assert "single 'with' block" in str(exc_info.value)
|
|
733
|
+
|
|
734
|
+
# Can't reenter
|
|
735
|
+
with _core.CancelScope() as scope:
|
|
736
|
+
with pytest.raises(RuntimeError) as exc_info:
|
|
737
|
+
with scope:
|
|
738
|
+
pass # pragma: no cover
|
|
739
|
+
assert "single 'with' block" in str(exc_info.value)
|
|
740
|
+
|
|
741
|
+
# Can't enter from multiple tasks simultaneously
|
|
742
|
+
scope = _core.CancelScope()
|
|
743
|
+
|
|
744
|
+
async def enter_scope() -> None:
|
|
745
|
+
with scope:
|
|
746
|
+
await sleep_forever()
|
|
747
|
+
|
|
748
|
+
async with _core.open_nursery() as nursery:
|
|
749
|
+
nursery.start_soon(enter_scope, name="this one")
|
|
750
|
+
await wait_all_tasks_blocked()
|
|
751
|
+
|
|
752
|
+
with pytest.raises(RuntimeError) as exc_info:
|
|
753
|
+
with scope:
|
|
754
|
+
pass # pragma: no cover
|
|
755
|
+
assert "single 'with' block" in str(exc_info.value)
|
|
756
|
+
nursery.cancel_scope.cancel()
|
|
757
|
+
|
|
758
|
+
# If not yet entered, cancel_called is true when the deadline has passed
|
|
759
|
+
# even if cancel() hasn't been called yet
|
|
760
|
+
scope = _core.CancelScope(deadline=_core.current_time() + 1)
|
|
761
|
+
assert not scope.cancel_called
|
|
762
|
+
scope.deadline -= 1
|
|
763
|
+
assert scope.cancel_called
|
|
764
|
+
scope.deadline += 1
|
|
765
|
+
assert scope.cancel_called # never become un-cancelled
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
async def test_cancel_scope_misnesting_1() -> None:
|
|
769
|
+
outer = _core.CancelScope()
|
|
770
|
+
inner = _core.CancelScope()
|
|
771
|
+
with ExitStack() as stack:
|
|
772
|
+
stack.enter_context(outer)
|
|
773
|
+
with inner:
|
|
774
|
+
with pytest.raises(RuntimeError, match="still within its child"):
|
|
775
|
+
stack.close()
|
|
776
|
+
# No further error is raised when exiting the inner context
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
async def test_cancel_scope_misnesting_2() -> None:
|
|
780
|
+
# If there are other tasks inside the abandoned part of the cancel tree,
|
|
781
|
+
# they get cancelled when the misnesting is detected
|
|
782
|
+
async def task1() -> None:
|
|
783
|
+
with pytest.raises(_core.Cancelled):
|
|
784
|
+
await sleep_forever()
|
|
785
|
+
|
|
786
|
+
# Even if inside another cancel scope
|
|
787
|
+
async def task2() -> None:
|
|
788
|
+
with _core.CancelScope():
|
|
789
|
+
with pytest.raises(
|
|
790
|
+
_core.Cancelled,
|
|
791
|
+
match=r"^cancelled due to unknown with reason 'misnesting'$",
|
|
792
|
+
):
|
|
793
|
+
await sleep_forever()
|
|
794
|
+
|
|
795
|
+
with ExitStack() as stack:
|
|
796
|
+
stack.enter_context(_core.CancelScope())
|
|
797
|
+
async with _core.open_nursery() as nursery:
|
|
798
|
+
nursery.start_soon(task1)
|
|
799
|
+
nursery.start_soon(task2)
|
|
800
|
+
await wait_all_tasks_blocked()
|
|
801
|
+
with pytest.raises(RuntimeError, match="still within its child"):
|
|
802
|
+
stack.close()
|
|
803
|
+
|
|
804
|
+
# Variant that makes the child tasks direct children of the scope
|
|
805
|
+
# that noticed the misnesting:
|
|
806
|
+
nursery_mgr = _core.open_nursery()
|
|
807
|
+
nursery = await nursery_mgr.__aenter__()
|
|
808
|
+
try:
|
|
809
|
+
nursery.start_soon(task1)
|
|
810
|
+
nursery.start_soon(task2)
|
|
811
|
+
nursery.start_soon(sleep_forever)
|
|
812
|
+
await wait_all_tasks_blocked()
|
|
813
|
+
nursery.cancel_scope.__exit__(None, None, None)
|
|
814
|
+
finally:
|
|
815
|
+
with pytest.raises(
|
|
816
|
+
RuntimeError,
|
|
817
|
+
match="which had already been exited",
|
|
818
|
+
) as exc_info:
|
|
819
|
+
await nursery_mgr.__aexit__(*sys.exc_info())
|
|
820
|
+
|
|
821
|
+
def no_context(exc: RuntimeError) -> bool:
|
|
822
|
+
return exc.__context__ is None
|
|
823
|
+
|
|
824
|
+
msg = "closed before the task exited"
|
|
825
|
+
group = pytest.RaisesGroup(
|
|
826
|
+
pytest.RaisesExc(RuntimeError, match=msg, check=no_context),
|
|
827
|
+
pytest.RaisesExc(RuntimeError, match=msg, check=no_context),
|
|
828
|
+
# sleep_forever
|
|
829
|
+
pytest.RaisesExc(
|
|
830
|
+
RuntimeError,
|
|
831
|
+
match=msg,
|
|
832
|
+
check=lambda x: isinstance(x.__context__, _core.Cancelled),
|
|
833
|
+
),
|
|
834
|
+
)
|
|
835
|
+
assert group.matches(exc_info.value.__context__)
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
async def test_cancel_scope_misnesting_3() -> None:
|
|
839
|
+
# Trying to exit a cancel scope from an unrelated task raises an error
|
|
840
|
+
# without affecting any state
|
|
841
|
+
async def task3(task_status: _core.TaskStatus[_core.CancelScope]) -> None:
|
|
842
|
+
with _core.CancelScope() as scope:
|
|
843
|
+
task_status.started(scope)
|
|
844
|
+
await sleep_forever()
|
|
845
|
+
|
|
846
|
+
async with _core.open_nursery() as nursery:
|
|
847
|
+
value = await nursery.start(task3)
|
|
848
|
+
assert isinstance(value, _core.CancelScope)
|
|
849
|
+
scope: _core.CancelScope = value
|
|
850
|
+
with pytest.raises(RuntimeError, match="from unrelated"):
|
|
851
|
+
scope.__exit__(None, None, None)
|
|
852
|
+
scope.cancel()
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
# helper to check we're not outputting overly verbose tracebacks
|
|
856
|
+
def no_cause_or_context(e: BaseException) -> bool:
|
|
857
|
+
return e.__cause__ is None and e.__context__ is None
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
async def test_nursery_misnest() -> None:
|
|
861
|
+
# See https://github.com/python-trio/trio/issues/3298
|
|
862
|
+
async def inner_func() -> None:
|
|
863
|
+
inner_nursery = await inner_cm.__aenter__()
|
|
864
|
+
inner_nursery.start_soon(sleep, 1)
|
|
865
|
+
|
|
866
|
+
with pytest.RaisesGroup(
|
|
867
|
+
pytest.RaisesExc(
|
|
868
|
+
RuntimeError, match="Nursery stack corrupted", check=no_cause_or_context
|
|
869
|
+
),
|
|
870
|
+
check=no_cause_or_context,
|
|
871
|
+
):
|
|
872
|
+
async with _core.open_nursery() as outer_nursery:
|
|
873
|
+
inner_cm = _core.open_nursery()
|
|
874
|
+
outer_nursery.start_soon(inner_func)
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
def test_nursery_nested_child_misnest() -> None:
|
|
878
|
+
# Note that this example does *not* raise an exception group.
|
|
879
|
+
async def main() -> None:
|
|
880
|
+
async with _core.open_nursery():
|
|
881
|
+
inner_cm = _core.open_nursery()
|
|
882
|
+
await inner_cm.__aenter__()
|
|
883
|
+
|
|
884
|
+
with pytest.raises(RuntimeError, match="Nursery stack corrupted") as excinfo:
|
|
885
|
+
_core.run(main)
|
|
886
|
+
assert excinfo.value.__cause__ is None
|
|
887
|
+
# This AssertionError is kind of redundant, but I don't think we want to remove
|
|
888
|
+
# the assertion and don't think we care enough to suppress it in this specific case.
|
|
889
|
+
assert pytest.RaisesExc(
|
|
890
|
+
AssertionError, match="^Nursery misnesting detected!$"
|
|
891
|
+
).matches(excinfo.value.__context__)
|
|
892
|
+
assert excinfo.value.__context__.__cause__ is None
|
|
893
|
+
assert excinfo.value.__context__.__context__ is None
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
async def test_asyncexitstack_nursery_misnest() -> None:
|
|
897
|
+
# This example is trickier than the above ones, and is the one that requires
|
|
898
|
+
# special logic of abandoned nurseries to avoid nasty internal errors that masks
|
|
899
|
+
# the RuntimeError.
|
|
900
|
+
@asynccontextmanager
|
|
901
|
+
async def asynccontextmanager_that_creates_a_nursery_internally() -> (
|
|
902
|
+
AsyncGenerator[None]
|
|
903
|
+
):
|
|
904
|
+
async with _core.open_nursery() as nursery:
|
|
905
|
+
await nursery.start(started_sleeper)
|
|
906
|
+
nursery.start_soon(unstarted_task)
|
|
907
|
+
yield
|
|
908
|
+
|
|
909
|
+
async def started_sleeper(task_status: _core.TaskStatus[None]) -> None:
|
|
910
|
+
task_status.started()
|
|
911
|
+
await sleep_forever()
|
|
912
|
+
|
|
913
|
+
async def unstarted_task() -> None:
|
|
914
|
+
await _core.checkpoint()
|
|
915
|
+
|
|
916
|
+
with pytest.RaisesGroup(
|
|
917
|
+
pytest.RaisesGroup(
|
|
918
|
+
pytest.RaisesExc(
|
|
919
|
+
RuntimeError, match="Nursery stack corrupted", check=no_cause_or_context
|
|
920
|
+
),
|
|
921
|
+
check=no_cause_or_context,
|
|
922
|
+
),
|
|
923
|
+
check=no_cause_or_context,
|
|
924
|
+
):
|
|
925
|
+
async with AsyncExitStack() as stack, _core.open_nursery() as nursery:
|
|
926
|
+
# The asynccontextmanager is going to create a nursery that outlives this nursery!
|
|
927
|
+
nursery.start_soon(
|
|
928
|
+
stack.enter_async_context,
|
|
929
|
+
asynccontextmanager_that_creates_a_nursery_internally(),
|
|
930
|
+
)
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
def test_asyncexitstack_nursery_misnest_cleanup() -> None:
|
|
934
|
+
# We guarantee that abandoned tasks get to do cleanup *eventually*, but exceptions
|
|
935
|
+
# are lost. With more effort it's possible we could reschedule child tasks to exit
|
|
936
|
+
# promptly.
|
|
937
|
+
finally_entered = []
|
|
938
|
+
|
|
939
|
+
async def main() -> None:
|
|
940
|
+
async def unstarted_task() -> None:
|
|
941
|
+
try:
|
|
942
|
+
await _core.checkpoint()
|
|
943
|
+
finally:
|
|
944
|
+
finally_entered.append(True)
|
|
945
|
+
raise ValueError("this exception is lost")
|
|
946
|
+
|
|
947
|
+
# rest of main() is ~identical to the above test
|
|
948
|
+
@asynccontextmanager
|
|
949
|
+
async def asynccontextmanager_that_creates_a_nursery_internally() -> (
|
|
950
|
+
AsyncGenerator[None]
|
|
951
|
+
):
|
|
952
|
+
async with _core.open_nursery() as nursery:
|
|
953
|
+
nursery.start_soon(unstarted_task)
|
|
954
|
+
yield
|
|
955
|
+
|
|
956
|
+
with pytest.RaisesGroup(
|
|
957
|
+
pytest.RaisesGroup(
|
|
958
|
+
pytest.RaisesExc(
|
|
959
|
+
RuntimeError,
|
|
960
|
+
match="Nursery stack corrupted",
|
|
961
|
+
check=no_cause_or_context,
|
|
962
|
+
),
|
|
963
|
+
check=no_cause_or_context,
|
|
964
|
+
),
|
|
965
|
+
check=no_cause_or_context,
|
|
966
|
+
):
|
|
967
|
+
async with AsyncExitStack() as stack, _core.open_nursery() as nursery:
|
|
968
|
+
# The asynccontextmanager is going to create a nursery that outlives this nursery!
|
|
969
|
+
nursery.start_soon(
|
|
970
|
+
stack.enter_async_context,
|
|
971
|
+
asynccontextmanager_that_creates_a_nursery_internally(),
|
|
972
|
+
)
|
|
973
|
+
assert not finally_entered # abandoned task still hasn't been cleaned up
|
|
974
|
+
|
|
975
|
+
_core.run(main)
|
|
976
|
+
assert finally_entered # now it has
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
@slow
|
|
980
|
+
async def test_timekeeping() -> None:
|
|
981
|
+
# probably a good idea to use a real clock for *one* test anyway...
|
|
982
|
+
TARGET = 1.0
|
|
983
|
+
# give it a few tries in case of random CI server flakiness
|
|
984
|
+
for _ in range(4):
|
|
985
|
+
real_start = time.perf_counter()
|
|
986
|
+
with _core.CancelScope() as scope:
|
|
987
|
+
scope.deadline = _core.current_time() + TARGET
|
|
988
|
+
await sleep_forever()
|
|
989
|
+
real_duration = time.perf_counter() - real_start
|
|
990
|
+
accuracy = real_duration / TARGET
|
|
991
|
+
print(accuracy)
|
|
992
|
+
# Actual time elapsed should always be >= target time
|
|
993
|
+
# (== is possible depending on system behavior for time.perf_counter resolution
|
|
994
|
+
if 1.0 <= accuracy < 2: # pragma: no branch
|
|
995
|
+
break
|
|
996
|
+
else: # pragma: no cover
|
|
997
|
+
raise AssertionError()
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
async def test_failed_abort() -> None:
|
|
1001
|
+
stubborn_task: _core.Task | None = None
|
|
1002
|
+
stubborn_scope: _core.CancelScope | None = None
|
|
1003
|
+
record: list[str] = []
|
|
1004
|
+
|
|
1005
|
+
async def stubborn_sleeper() -> None:
|
|
1006
|
+
nonlocal stubborn_task, stubborn_scope
|
|
1007
|
+
stubborn_task = _core.current_task()
|
|
1008
|
+
with _core.CancelScope() as scope:
|
|
1009
|
+
stubborn_scope = scope
|
|
1010
|
+
record.append("sleep")
|
|
1011
|
+
x = await _core.wait_task_rescheduled(lambda _: _core.Abort.FAILED)
|
|
1012
|
+
assert x == 1
|
|
1013
|
+
record.append("woke")
|
|
1014
|
+
try:
|
|
1015
|
+
await _core.checkpoint_if_cancelled()
|
|
1016
|
+
except _core.Cancelled:
|
|
1017
|
+
record.append("cancelled")
|
|
1018
|
+
|
|
1019
|
+
async with _core.open_nursery() as nursery:
|
|
1020
|
+
nursery.start_soon(stubborn_sleeper)
|
|
1021
|
+
await wait_all_tasks_blocked()
|
|
1022
|
+
assert record == ["sleep"]
|
|
1023
|
+
not_none(stubborn_scope).cancel()
|
|
1024
|
+
await wait_all_tasks_blocked()
|
|
1025
|
+
# cancel didn't wake it up
|
|
1026
|
+
assert record == ["sleep"]
|
|
1027
|
+
# wake it up again by hand
|
|
1028
|
+
_core.reschedule(not_none(stubborn_task), outcome.Value(1))
|
|
1029
|
+
assert record == ["sleep", "woke", "cancelled"]
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
@restore_unraisablehook()
|
|
1033
|
+
def test_broken_abort() -> None:
|
|
1034
|
+
async def main() -> None:
|
|
1035
|
+
# These yields are here to work around an annoying warning -- we're
|
|
1036
|
+
# going to crash the main loop, and if we (by chance) do this before
|
|
1037
|
+
# the run_sync_soon task runs for the first time, then Python gives us
|
|
1038
|
+
# a spurious warning about it not being awaited. (I mean, the warning
|
|
1039
|
+
# is correct, but here we're testing our ability to deliver a
|
|
1040
|
+
# semi-meaningful error after things have gone totally pear-shaped, so
|
|
1041
|
+
# it's not relevant.) By letting the run_sync_soon_task run first, we
|
|
1042
|
+
# avoid the warning.
|
|
1043
|
+
await _core.checkpoint()
|
|
1044
|
+
await _core.checkpoint()
|
|
1045
|
+
with _core.CancelScope() as scope:
|
|
1046
|
+
scope.cancel()
|
|
1047
|
+
# None is not a legal return value here
|
|
1048
|
+
await _core.wait_task_rescheduled(lambda _: None) # type: ignore
|
|
1049
|
+
|
|
1050
|
+
with pytest.raises(_core.TrioInternalError):
|
|
1051
|
+
_core.run(main)
|
|
1052
|
+
|
|
1053
|
+
# Because this crashes, various __del__ methods print complaints on
|
|
1054
|
+
# stderr. Make sure that they get run now, so the output is attached to
|
|
1055
|
+
# this test.
|
|
1056
|
+
gc_collect_harder()
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
# This segfaults, so we need to skipif. Remember to remove the skipif once
|
|
1060
|
+
# the upstream issue is resolved.
|
|
1061
|
+
@restore_unraisablehook()
|
|
1062
|
+
@pytest.mark.skipif(
|
|
1063
|
+
sys.version_info[:2] == (3, 14),
|
|
1064
|
+
reason="https://github.com/python/cpython/issues/133932",
|
|
1065
|
+
)
|
|
1066
|
+
def test_error_in_run_loop() -> None:
|
|
1067
|
+
# Blow stuff up real good to check we at least get a TrioInternalError
|
|
1068
|
+
async def main() -> None:
|
|
1069
|
+
task = _core.current_task()
|
|
1070
|
+
task._schedule_points = "hello!" # type: ignore
|
|
1071
|
+
await _core.checkpoint()
|
|
1072
|
+
|
|
1073
|
+
with ignore_coroutine_never_awaited_warnings():
|
|
1074
|
+
with pytest.raises(_core.TrioInternalError):
|
|
1075
|
+
_core.run(main)
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
async def test_spawn_system_task() -> None:
|
|
1079
|
+
record: list[tuple[str, int]] = []
|
|
1080
|
+
|
|
1081
|
+
async def system_task(x: int) -> None:
|
|
1082
|
+
record.append(("x", x))
|
|
1083
|
+
record.append(("ki", _core.currently_ki_protected()))
|
|
1084
|
+
await _core.checkpoint()
|
|
1085
|
+
|
|
1086
|
+
_core.spawn_system_task(system_task, 1)
|
|
1087
|
+
await wait_all_tasks_blocked()
|
|
1088
|
+
assert record == [("x", 1), ("ki", True)]
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
# intentionally make a system task crash
|
|
1092
|
+
def test_system_task_crash() -> None:
|
|
1093
|
+
async def crasher() -> NoReturn:
|
|
1094
|
+
raise KeyError
|
|
1095
|
+
|
|
1096
|
+
async def main() -> None:
|
|
1097
|
+
_core.spawn_system_task(crasher)
|
|
1098
|
+
await sleep_forever()
|
|
1099
|
+
|
|
1100
|
+
with pytest.raises(_core.TrioInternalError):
|
|
1101
|
+
_core.run(main)
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
def test_system_task_crash_ExceptionGroup() -> None:
|
|
1105
|
+
async def crasher1() -> NoReturn:
|
|
1106
|
+
raise KeyError
|
|
1107
|
+
|
|
1108
|
+
async def crasher2() -> NoReturn:
|
|
1109
|
+
raise ValueError
|
|
1110
|
+
|
|
1111
|
+
async def system_task() -> None:
|
|
1112
|
+
async with _core.open_nursery() as nursery:
|
|
1113
|
+
nursery.start_soon(crasher1)
|
|
1114
|
+
nursery.start_soon(crasher2)
|
|
1115
|
+
|
|
1116
|
+
async def main() -> None:
|
|
1117
|
+
_core.spawn_system_task(system_task)
|
|
1118
|
+
await sleep_forever()
|
|
1119
|
+
|
|
1120
|
+
# TrioInternalError is not wrapped
|
|
1121
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
1122
|
+
_core.run(main)
|
|
1123
|
+
|
|
1124
|
+
# the first exceptiongroup is from the first nursery opened in Runner.init()
|
|
1125
|
+
# the second exceptiongroup is from the second nursery opened in Runner.init()
|
|
1126
|
+
# the third exceptongroup is from the nursery defined in `system_task` above
|
|
1127
|
+
assert pytest.RaisesGroup(
|
|
1128
|
+
pytest.RaisesGroup(pytest.RaisesGroup(KeyError, ValueError))
|
|
1129
|
+
).matches(
|
|
1130
|
+
excinfo.value.__cause__,
|
|
1131
|
+
)
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
def test_system_task_crash_plus_Cancelled() -> None:
|
|
1135
|
+
# Set up a situation where a system task crashes with a
|
|
1136
|
+
# ExceptionGroup([Cancelled, ValueError])
|
|
1137
|
+
async def crasher() -> None:
|
|
1138
|
+
try:
|
|
1139
|
+
await sleep_forever()
|
|
1140
|
+
except _core.Cancelled:
|
|
1141
|
+
raise ValueError from None
|
|
1142
|
+
|
|
1143
|
+
async def cancelme() -> None:
|
|
1144
|
+
await sleep_forever()
|
|
1145
|
+
|
|
1146
|
+
async def system_task() -> None:
|
|
1147
|
+
async with _core.open_nursery() as nursery:
|
|
1148
|
+
nursery.start_soon(crasher)
|
|
1149
|
+
nursery.start_soon(cancelme)
|
|
1150
|
+
|
|
1151
|
+
async def main() -> None:
|
|
1152
|
+
_core.spawn_system_task(system_task)
|
|
1153
|
+
# then we exit, triggering a cancellation
|
|
1154
|
+
|
|
1155
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
1156
|
+
_core.run(main)
|
|
1157
|
+
|
|
1158
|
+
# See explanation for triple-wrap in test_system_task_crash_ExceptionGroup
|
|
1159
|
+
assert pytest.RaisesGroup(
|
|
1160
|
+
pytest.RaisesGroup(pytest.RaisesGroup(ValueError))
|
|
1161
|
+
).matches(
|
|
1162
|
+
excinfo.value.__cause__,
|
|
1163
|
+
)
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
def test_system_task_crash_KeyboardInterrupt() -> None:
|
|
1167
|
+
async def ki() -> NoReturn:
|
|
1168
|
+
raise KeyboardInterrupt
|
|
1169
|
+
|
|
1170
|
+
async def main() -> None:
|
|
1171
|
+
_core.spawn_system_task(ki)
|
|
1172
|
+
await sleep_forever()
|
|
1173
|
+
|
|
1174
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
1175
|
+
_core.run(main)
|
|
1176
|
+
# "Only" double-wrapped since ki() doesn't create an exceptiongroup
|
|
1177
|
+
assert pytest.RaisesGroup(pytest.RaisesGroup(KeyboardInterrupt)).matches(
|
|
1178
|
+
excinfo.value.__cause__
|
|
1179
|
+
)
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
# This used to fail because checkpoint was a yield followed by an immediate
|
|
1183
|
+
# reschedule. So we had:
|
|
1184
|
+
# 1) this task yields
|
|
1185
|
+
# 2) this task is rescheduled
|
|
1186
|
+
# ...
|
|
1187
|
+
# 3) next iteration of event loop starts, runs timeouts
|
|
1188
|
+
# 4) this task has timed out
|
|
1189
|
+
# 5) ...but it's on the run queue, so the timeout is queued to be delivered
|
|
1190
|
+
# the next time that it's blocked.
|
|
1191
|
+
async def test_yield_briefly_checks_for_timeout(mock_clock: _core.MockClock) -> None:
|
|
1192
|
+
with _core.CancelScope(deadline=_core.current_time() + 5):
|
|
1193
|
+
await _core.checkpoint()
|
|
1194
|
+
mock_clock.jump(10)
|
|
1195
|
+
with pytest.raises(_core.Cancelled):
|
|
1196
|
+
await _core.checkpoint()
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
# This tests that sys.exc_info is properly saved/restored as we swap between
|
|
1200
|
+
# tasks. It turns out that the interpreter automagically handles this for us
|
|
1201
|
+
# so there's no special code in Trio required to pass this test, but it's
|
|
1202
|
+
# still nice to know that it works :-).
|
|
1203
|
+
#
|
|
1204
|
+
# Update: it turns out I was right to be nervous! see the next test...
|
|
1205
|
+
async def test_exc_info() -> None:
|
|
1206
|
+
record: list[str] = []
|
|
1207
|
+
seq = Sequencer()
|
|
1208
|
+
|
|
1209
|
+
async def child1() -> None:
|
|
1210
|
+
async with seq(0):
|
|
1211
|
+
pass # we don't yield until seq(2) below
|
|
1212
|
+
record.append("child1 raise")
|
|
1213
|
+
with pytest.raises(ValueError, match=r"^child1$") as excinfo:
|
|
1214
|
+
try:
|
|
1215
|
+
raise ValueError("child1")
|
|
1216
|
+
except ValueError:
|
|
1217
|
+
record.append("child1 sleep")
|
|
1218
|
+
async with seq(2):
|
|
1219
|
+
pass
|
|
1220
|
+
assert "child2 wake" in record
|
|
1221
|
+
record.append("child1 re-raise")
|
|
1222
|
+
raise
|
|
1223
|
+
assert excinfo.value.__context__ is None
|
|
1224
|
+
record.append("child1 success")
|
|
1225
|
+
|
|
1226
|
+
async def child2() -> None:
|
|
1227
|
+
async with seq(1):
|
|
1228
|
+
pass # we don't yield until seq(3) below
|
|
1229
|
+
assert "child1 sleep" in record
|
|
1230
|
+
record.append("child2 wake")
|
|
1231
|
+
assert sys.exc_info() == (None, None, None)
|
|
1232
|
+
with pytest.raises(KeyError) as excinfo:
|
|
1233
|
+
try:
|
|
1234
|
+
raise KeyError("child2")
|
|
1235
|
+
except KeyError:
|
|
1236
|
+
record.append("child2 sleep again")
|
|
1237
|
+
async with seq(3):
|
|
1238
|
+
pass
|
|
1239
|
+
assert "child1 re-raise" in record
|
|
1240
|
+
record.append("child2 re-raise")
|
|
1241
|
+
raise
|
|
1242
|
+
assert excinfo.value.__context__ is None
|
|
1243
|
+
record.append("child2 success")
|
|
1244
|
+
|
|
1245
|
+
async with _core.open_nursery() as nursery:
|
|
1246
|
+
nursery.start_soon(child1)
|
|
1247
|
+
nursery.start_soon(child2)
|
|
1248
|
+
|
|
1249
|
+
assert record == [
|
|
1250
|
+
"child1 raise",
|
|
1251
|
+
"child1 sleep",
|
|
1252
|
+
"child2 wake",
|
|
1253
|
+
"child2 sleep again",
|
|
1254
|
+
"child1 re-raise",
|
|
1255
|
+
"child1 success",
|
|
1256
|
+
"child2 re-raise",
|
|
1257
|
+
"child2 success",
|
|
1258
|
+
]
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
# On all CPython versions (at time of writing), using .throw() to raise an
|
|
1262
|
+
# exception inside a coroutine/generator can cause the original `exc_info` state
|
|
1263
|
+
# to be lost, so things like re-raising and exception chaining are broken unless
|
|
1264
|
+
# Trio implements its workaround. At time of writing, CPython main (3.13-dev)
|
|
1265
|
+
# and every CPython release (excluding releases for old Python versions not
|
|
1266
|
+
# supported by Trio) is affected (albeit in differing ways).
|
|
1267
|
+
#
|
|
1268
|
+
# If the `ValueError()` gets sent in via `throw` and is suppressed, then CPython
|
|
1269
|
+
# loses track of the original `exc_info`:
|
|
1270
|
+
# https://bugs.python.org/issue25612 (Example 1)
|
|
1271
|
+
# https://bugs.python.org/issue29587 (Example 2)
|
|
1272
|
+
# This is fixed in CPython >= 3.7.
|
|
1273
|
+
async def test_exc_info_after_throw_suppressed() -> None:
|
|
1274
|
+
child_task: _core.Task | None = None
|
|
1275
|
+
|
|
1276
|
+
async def child() -> None:
|
|
1277
|
+
nonlocal child_task
|
|
1278
|
+
child_task = _core.current_task()
|
|
1279
|
+
|
|
1280
|
+
try:
|
|
1281
|
+
raise KeyError
|
|
1282
|
+
except KeyError:
|
|
1283
|
+
with suppress(ValueError):
|
|
1284
|
+
await sleep_forever()
|
|
1285
|
+
raise
|
|
1286
|
+
|
|
1287
|
+
with pytest.RaisesGroup(
|
|
1288
|
+
pytest.RaisesExc(KeyError, check=lambda e: e.__context__ is None)
|
|
1289
|
+
):
|
|
1290
|
+
async with _core.open_nursery() as nursery:
|
|
1291
|
+
nursery.start_soon(child)
|
|
1292
|
+
await wait_all_tasks_blocked()
|
|
1293
|
+
_core.reschedule(not_none(child_task), outcome.Error(ValueError()))
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
# Similar to previous test -- if the `ValueError()` gets sent in via 'throw' and
|
|
1297
|
+
# propagates out, then CPython doesn't set its `__context__` so normal implicit
|
|
1298
|
+
# exception chaining is broken:
|
|
1299
|
+
# https://bugs.python.org/issue25612 (Example 2)
|
|
1300
|
+
# https://bugs.python.org/issue25683
|
|
1301
|
+
# https://bugs.python.org/issue29587 (Example 1)
|
|
1302
|
+
# This is fixed in CPython >= 3.9, but kept as a regression test.
|
|
1303
|
+
async def test_exception_chaining_after_throw() -> None:
|
|
1304
|
+
child_task: _core.Task | None = None
|
|
1305
|
+
|
|
1306
|
+
async def child() -> None:
|
|
1307
|
+
nonlocal child_task
|
|
1308
|
+
child_task = _core.current_task()
|
|
1309
|
+
|
|
1310
|
+
try:
|
|
1311
|
+
raise KeyError
|
|
1312
|
+
except KeyError:
|
|
1313
|
+
await sleep_forever()
|
|
1314
|
+
|
|
1315
|
+
with pytest.RaisesGroup(
|
|
1316
|
+
pytest.RaisesExc(
|
|
1317
|
+
ValueError,
|
|
1318
|
+
match="error text",
|
|
1319
|
+
check=lambda e: isinstance(e.__context__, KeyError),
|
|
1320
|
+
),
|
|
1321
|
+
):
|
|
1322
|
+
async with _core.open_nursery() as nursery:
|
|
1323
|
+
nursery.start_soon(child)
|
|
1324
|
+
await wait_all_tasks_blocked()
|
|
1325
|
+
_core.reschedule(
|
|
1326
|
+
not_none(child_task),
|
|
1327
|
+
outcome.Error(ValueError("error text")),
|
|
1328
|
+
)
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
# Similar to previous tests -- if the `ValueError()` gets sent into an inner
|
|
1332
|
+
# `await` via 'throw' and is suppressed there, then CPython loses track of
|
|
1333
|
+
# `exc_info` in the inner coroutine:
|
|
1334
|
+
# https://github.com/python/cpython/issues/108668
|
|
1335
|
+
# This is unfixed in CPython at time of writing.
|
|
1336
|
+
async def test_exc_info_after_throw_to_inner_suppressed() -> None:
|
|
1337
|
+
child_task: _core.Task | None = None
|
|
1338
|
+
|
|
1339
|
+
async def child() -> None:
|
|
1340
|
+
nonlocal child_task
|
|
1341
|
+
child_task = _core.current_task()
|
|
1342
|
+
|
|
1343
|
+
try:
|
|
1344
|
+
raise KeyError
|
|
1345
|
+
except KeyError as exc:
|
|
1346
|
+
await inner(exc)
|
|
1347
|
+
raise
|
|
1348
|
+
|
|
1349
|
+
async def inner(exc: BaseException) -> None:
|
|
1350
|
+
with suppress(ValueError):
|
|
1351
|
+
await sleep_forever()
|
|
1352
|
+
assert not_none(sys.exc_info()[1]) is exc
|
|
1353
|
+
|
|
1354
|
+
with pytest.RaisesGroup(
|
|
1355
|
+
pytest.RaisesExc(KeyError, check=lambda e: e.__context__ is None)
|
|
1356
|
+
):
|
|
1357
|
+
async with _core.open_nursery() as nursery:
|
|
1358
|
+
nursery.start_soon(child)
|
|
1359
|
+
await wait_all_tasks_blocked()
|
|
1360
|
+
_core.reschedule(not_none(child_task), outcome.Error(ValueError()))
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
# Similar to previous tests -- if the `ValueError()` gets sent into an inner
|
|
1364
|
+
# `await` via `throw` and propagates out, then CPython incorrectly sets its
|
|
1365
|
+
# `__context__` so normal implicit exception chaining is broken:
|
|
1366
|
+
# https://bugs.python.org/issue40694
|
|
1367
|
+
# This is unfixed in CPython at time of writing.
|
|
1368
|
+
async def test_exception_chaining_after_throw_to_inner() -> None:
|
|
1369
|
+
child_task: _core.Task | None = None
|
|
1370
|
+
|
|
1371
|
+
async def child() -> None:
|
|
1372
|
+
nonlocal child_task
|
|
1373
|
+
child_task = _core.current_task()
|
|
1374
|
+
|
|
1375
|
+
try:
|
|
1376
|
+
raise KeyError
|
|
1377
|
+
except KeyError:
|
|
1378
|
+
await inner()
|
|
1379
|
+
|
|
1380
|
+
async def inner() -> None:
|
|
1381
|
+
try:
|
|
1382
|
+
raise IndexError
|
|
1383
|
+
except IndexError:
|
|
1384
|
+
await sleep_forever()
|
|
1385
|
+
|
|
1386
|
+
with pytest.RaisesGroup(
|
|
1387
|
+
pytest.RaisesExc(
|
|
1388
|
+
ValueError,
|
|
1389
|
+
match="^Unique Text$",
|
|
1390
|
+
check=lambda e: isinstance(e.__context__, IndexError)
|
|
1391
|
+
and isinstance(e.__context__.__context__, KeyError),
|
|
1392
|
+
),
|
|
1393
|
+
):
|
|
1394
|
+
async with _core.open_nursery() as nursery:
|
|
1395
|
+
nursery.start_soon(child)
|
|
1396
|
+
await wait_all_tasks_blocked()
|
|
1397
|
+
_core.reschedule(
|
|
1398
|
+
not_none(child_task),
|
|
1399
|
+
outcome.Error(ValueError("Unique Text")),
|
|
1400
|
+
)
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
async def test_nursery_exception_chaining_doesnt_make_context_loops() -> None:
|
|
1404
|
+
async def crasher() -> NoReturn:
|
|
1405
|
+
raise KeyError
|
|
1406
|
+
|
|
1407
|
+
# the ExceptionGroup should not have the KeyError or ValueError as context
|
|
1408
|
+
with pytest.RaisesGroup(
|
|
1409
|
+
ValueError, KeyError, check=lambda x: x.__context__ is None
|
|
1410
|
+
):
|
|
1411
|
+
async with _core.open_nursery() as nursery:
|
|
1412
|
+
nursery.start_soon(crasher)
|
|
1413
|
+
raise ValueError
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
def test_TrioToken_identity() -> None:
|
|
1417
|
+
async def get_and_check_token() -> _core.TrioToken:
|
|
1418
|
+
token = _core.current_trio_token()
|
|
1419
|
+
# Two calls in the same run give the same object
|
|
1420
|
+
assert token is _core.current_trio_token()
|
|
1421
|
+
return token
|
|
1422
|
+
|
|
1423
|
+
t1 = _core.run(get_and_check_token)
|
|
1424
|
+
t2 = _core.run(get_and_check_token)
|
|
1425
|
+
assert t1 is not t2
|
|
1426
|
+
assert t1 != t2
|
|
1427
|
+
assert hash(t1) != hash(t2)
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
async def test_TrioToken_run_sync_soon_basic() -> None:
|
|
1431
|
+
record: list[tuple[str, int]] = []
|
|
1432
|
+
|
|
1433
|
+
def cb(x: int) -> None:
|
|
1434
|
+
record.append(("cb", x))
|
|
1435
|
+
|
|
1436
|
+
token = _core.current_trio_token()
|
|
1437
|
+
token.run_sync_soon(cb, 1)
|
|
1438
|
+
assert not record
|
|
1439
|
+
await wait_all_tasks_blocked()
|
|
1440
|
+
assert record == [("cb", 1)]
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
def test_TrioToken_run_sync_soon_too_late() -> None:
|
|
1444
|
+
token: _core.TrioToken | None = None
|
|
1445
|
+
|
|
1446
|
+
async def main() -> None:
|
|
1447
|
+
nonlocal token
|
|
1448
|
+
token = _core.current_trio_token()
|
|
1449
|
+
|
|
1450
|
+
_core.run(main)
|
|
1451
|
+
with pytest.raises(_core.RunFinishedError):
|
|
1452
|
+
not_none(token).run_sync_soon(lambda: None) # pragma: no branch
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
async def test_TrioToken_run_sync_soon_idempotent() -> None:
|
|
1456
|
+
record: list[int] = []
|
|
1457
|
+
|
|
1458
|
+
def cb(x: int) -> None:
|
|
1459
|
+
record.append(x)
|
|
1460
|
+
|
|
1461
|
+
token = _core.current_trio_token()
|
|
1462
|
+
token.run_sync_soon(cb, 1)
|
|
1463
|
+
token.run_sync_soon(cb, 1, idempotent=True)
|
|
1464
|
+
token.run_sync_soon(cb, 1, idempotent=True)
|
|
1465
|
+
token.run_sync_soon(cb, 1, idempotent=True)
|
|
1466
|
+
token.run_sync_soon(cb, 2, idempotent=True)
|
|
1467
|
+
token.run_sync_soon(cb, 2, idempotent=True)
|
|
1468
|
+
await wait_all_tasks_blocked()
|
|
1469
|
+
assert len(record) == 3
|
|
1470
|
+
assert sorted(record) == [1, 1, 2]
|
|
1471
|
+
|
|
1472
|
+
# ordering test
|
|
1473
|
+
record = []
|
|
1474
|
+
for _ in range(3):
|
|
1475
|
+
for i in range(100):
|
|
1476
|
+
token.run_sync_soon(cb, i, idempotent=True)
|
|
1477
|
+
await wait_all_tasks_blocked()
|
|
1478
|
+
# We guarantee FIFO
|
|
1479
|
+
assert record == list(range(100))
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
def test_TrioToken_run_sync_soon_idempotent_requeue() -> None:
|
|
1483
|
+
# We guarantee that if a call has finished, queueing it again will call it
|
|
1484
|
+
# again. Due to the lack of synchronization, this effectively means that
|
|
1485
|
+
# we have to guarantee that once a call has *started*, queueing it again
|
|
1486
|
+
# will call it again. Also this is much easier to test :-)
|
|
1487
|
+
record: list[None] = []
|
|
1488
|
+
|
|
1489
|
+
def redo(token: _core.TrioToken) -> None:
|
|
1490
|
+
record.append(None)
|
|
1491
|
+
with suppress(_core.RunFinishedError):
|
|
1492
|
+
token.run_sync_soon(redo, token, idempotent=True)
|
|
1493
|
+
|
|
1494
|
+
async def main() -> None:
|
|
1495
|
+
token = _core.current_trio_token()
|
|
1496
|
+
token.run_sync_soon(redo, token, idempotent=True)
|
|
1497
|
+
await _core.checkpoint()
|
|
1498
|
+
await _core.checkpoint()
|
|
1499
|
+
await _core.checkpoint()
|
|
1500
|
+
|
|
1501
|
+
_core.run(main)
|
|
1502
|
+
|
|
1503
|
+
assert len(record) >= 2
|
|
1504
|
+
|
|
1505
|
+
|
|
1506
|
+
def test_TrioToken_run_sync_soon_after_main_crash() -> None:
|
|
1507
|
+
record: list[str] = []
|
|
1508
|
+
|
|
1509
|
+
async def main() -> None:
|
|
1510
|
+
token = _core.current_trio_token()
|
|
1511
|
+
# After main exits but before finally cleaning up, callback processed
|
|
1512
|
+
# normally
|
|
1513
|
+
token.run_sync_soon(lambda: record.append("sync-cb"))
|
|
1514
|
+
raise ValueError("error text")
|
|
1515
|
+
|
|
1516
|
+
with pytest.raises(ValueError, match=r"^error text$"):
|
|
1517
|
+
_core.run(main)
|
|
1518
|
+
|
|
1519
|
+
assert record == ["sync-cb"]
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
def test_TrioToken_run_sync_soon_crashes() -> None:
|
|
1523
|
+
record: set[str] = set()
|
|
1524
|
+
|
|
1525
|
+
async def main() -> None:
|
|
1526
|
+
token = _core.current_trio_token()
|
|
1527
|
+
token.run_sync_soon(lambda: {}["nope"]) # type: ignore[index]
|
|
1528
|
+
# check that a crashing run_sync_soon callback doesn't stop further
|
|
1529
|
+
# calls to run_sync_soon
|
|
1530
|
+
token.run_sync_soon(lambda: record.add("2nd run_sync_soon ran"))
|
|
1531
|
+
try:
|
|
1532
|
+
await sleep_forever()
|
|
1533
|
+
except _core.Cancelled:
|
|
1534
|
+
record.add("cancelled!")
|
|
1535
|
+
|
|
1536
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
1537
|
+
_core.run(main)
|
|
1538
|
+
# the first exceptiongroup is from the first nursery opened in Runner.init()
|
|
1539
|
+
# the second exceptiongroup is from the second nursery opened in Runner.init()
|
|
1540
|
+
assert pytest.RaisesGroup(pytest.RaisesGroup(KeyError)).matches(
|
|
1541
|
+
excinfo.value.__cause__
|
|
1542
|
+
)
|
|
1543
|
+
assert record == {"2nd run_sync_soon ran", "cancelled!"}
|
|
1544
|
+
|
|
1545
|
+
|
|
1546
|
+
async def test_TrioToken_run_sync_soon_FIFO() -> None:
|
|
1547
|
+
N = 100
|
|
1548
|
+
record = []
|
|
1549
|
+
token = _core.current_trio_token()
|
|
1550
|
+
for i in range(N):
|
|
1551
|
+
token.run_sync_soon(lambda j: record.append(j), i)
|
|
1552
|
+
await wait_all_tasks_blocked()
|
|
1553
|
+
assert record == list(range(N))
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
def test_TrioToken_run_sync_soon_starvation_resistance() -> None:
|
|
1557
|
+
# Even if we push callbacks in from callbacks, so that the callback queue
|
|
1558
|
+
# never empties out, then we still can't starve out other tasks from
|
|
1559
|
+
# running.
|
|
1560
|
+
token: _core.TrioToken | None = None
|
|
1561
|
+
record: list[tuple[str, int]] = []
|
|
1562
|
+
|
|
1563
|
+
def naughty_cb(i: int) -> None:
|
|
1564
|
+
try:
|
|
1565
|
+
not_none(token).run_sync_soon(naughty_cb, i + 1)
|
|
1566
|
+
except _core.RunFinishedError:
|
|
1567
|
+
record.append(("run finished", i))
|
|
1568
|
+
|
|
1569
|
+
async def main() -> None:
|
|
1570
|
+
nonlocal token
|
|
1571
|
+
token = _core.current_trio_token()
|
|
1572
|
+
token.run_sync_soon(naughty_cb, 0)
|
|
1573
|
+
record.append(("starting", 0))
|
|
1574
|
+
for _ in range(20):
|
|
1575
|
+
await _core.checkpoint()
|
|
1576
|
+
|
|
1577
|
+
_core.run(main)
|
|
1578
|
+
assert len(record) == 2
|
|
1579
|
+
assert record[0] == ("starting", 0)
|
|
1580
|
+
assert record[1][0] == "run finished"
|
|
1581
|
+
assert record[1][1] >= 19
|
|
1582
|
+
|
|
1583
|
+
|
|
1584
|
+
def test_TrioToken_run_sync_soon_threaded_stress_test() -> None:
|
|
1585
|
+
cb_counter = 0
|
|
1586
|
+
|
|
1587
|
+
def cb() -> None:
|
|
1588
|
+
nonlocal cb_counter
|
|
1589
|
+
cb_counter += 1
|
|
1590
|
+
|
|
1591
|
+
def stress_thread(token: _core.TrioToken) -> None:
|
|
1592
|
+
try:
|
|
1593
|
+
while True:
|
|
1594
|
+
token.run_sync_soon(cb)
|
|
1595
|
+
time.sleep(0)
|
|
1596
|
+
except _core.RunFinishedError:
|
|
1597
|
+
pass
|
|
1598
|
+
|
|
1599
|
+
async def main() -> None:
|
|
1600
|
+
token = _core.current_trio_token()
|
|
1601
|
+
thread = threading.Thread(target=stress_thread, args=(token,))
|
|
1602
|
+
thread.start()
|
|
1603
|
+
for _ in range(10):
|
|
1604
|
+
start_value = cb_counter
|
|
1605
|
+
while cb_counter == start_value:
|
|
1606
|
+
await sleep(0.01)
|
|
1607
|
+
|
|
1608
|
+
_core.run(main)
|
|
1609
|
+
print(cb_counter)
|
|
1610
|
+
|
|
1611
|
+
|
|
1612
|
+
async def test_TrioToken_run_sync_soon_massive_queue() -> None:
|
|
1613
|
+
# There are edge cases in the wakeup fd code when the wakeup fd overflows,
|
|
1614
|
+
# so let's try to make that happen. This is also just a good stress test
|
|
1615
|
+
# in general. (With the current-as-of-2017-02-14 code using a socketpair
|
|
1616
|
+
# with minimal buffer, Linux takes 6 wakeups to fill the buffer and macOS
|
|
1617
|
+
# takes 1 wakeup. So 1000 is overkill if anything. Windows OTOH takes
|
|
1618
|
+
# ~600,000 wakeups, but has the same code paths...)
|
|
1619
|
+
COUNT = 1000
|
|
1620
|
+
token = _core.current_trio_token()
|
|
1621
|
+
counter = [0]
|
|
1622
|
+
|
|
1623
|
+
def cb(i: int) -> None:
|
|
1624
|
+
# This also tests FIFO ordering of callbacks
|
|
1625
|
+
assert counter[0] == i
|
|
1626
|
+
counter[0] += 1
|
|
1627
|
+
|
|
1628
|
+
for i in range(COUNT):
|
|
1629
|
+
token.run_sync_soon(cb, i)
|
|
1630
|
+
await wait_all_tasks_blocked()
|
|
1631
|
+
assert counter[0] == COUNT
|
|
1632
|
+
|
|
1633
|
+
|
|
1634
|
+
def test_TrioToken_run_sync_soon_late_crash() -> None:
|
|
1635
|
+
# Crash after system nursery is closed -- easiest way to do that is
|
|
1636
|
+
# from an async generator finalizer.
|
|
1637
|
+
record: list[str] = []
|
|
1638
|
+
saved: list[AsyncGenerator[int, None]] = []
|
|
1639
|
+
|
|
1640
|
+
async def agen() -> AsyncGenerator[int, None]:
|
|
1641
|
+
token = _core.current_trio_token()
|
|
1642
|
+
try:
|
|
1643
|
+
yield 1
|
|
1644
|
+
finally:
|
|
1645
|
+
token.run_sync_soon(lambda: {}["nope"]) # type: ignore[index]
|
|
1646
|
+
token.run_sync_soon(lambda: record.append("2nd ran"))
|
|
1647
|
+
|
|
1648
|
+
async def main() -> None:
|
|
1649
|
+
saved.append(agen())
|
|
1650
|
+
await saved[-1].asend(None)
|
|
1651
|
+
record.append("main exiting")
|
|
1652
|
+
|
|
1653
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
1654
|
+
_core.run(main)
|
|
1655
|
+
|
|
1656
|
+
assert pytest.RaisesGroup(KeyError).matches(excinfo.value.__cause__)
|
|
1657
|
+
assert record == ["main exiting", "2nd ran"]
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
async def test_slow_abort_basic() -> None:
|
|
1661
|
+
with _core.CancelScope() as scope:
|
|
1662
|
+
scope.cancel()
|
|
1663
|
+
|
|
1664
|
+
task = _core.current_task()
|
|
1665
|
+
token = _core.current_trio_token()
|
|
1666
|
+
|
|
1667
|
+
def slow_abort(raise_cancel: _core.RaiseCancelT) -> _core.Abort:
|
|
1668
|
+
result = outcome.capture(raise_cancel)
|
|
1669
|
+
token.run_sync_soon(_core.reschedule, task, result)
|
|
1670
|
+
return _core.Abort.FAILED
|
|
1671
|
+
|
|
1672
|
+
with pytest.raises(_core.Cancelled):
|
|
1673
|
+
await _core.wait_task_rescheduled(slow_abort)
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
async def test_slow_abort_edge_cases() -> None:
|
|
1677
|
+
record: list[str] = []
|
|
1678
|
+
|
|
1679
|
+
async def slow_aborter() -> None:
|
|
1680
|
+
task = _core.current_task()
|
|
1681
|
+
token = _core.current_trio_token()
|
|
1682
|
+
|
|
1683
|
+
def slow_abort(raise_cancel: _core.RaiseCancelT) -> _core.Abort:
|
|
1684
|
+
record.append("abort-called")
|
|
1685
|
+
result = outcome.capture(raise_cancel)
|
|
1686
|
+
token.run_sync_soon(_core.reschedule, task, result)
|
|
1687
|
+
return _core.Abort.FAILED
|
|
1688
|
+
|
|
1689
|
+
record.append("sleeping")
|
|
1690
|
+
with pytest.raises(_core.Cancelled):
|
|
1691
|
+
await _core.wait_task_rescheduled(slow_abort)
|
|
1692
|
+
record.append("cancelled")
|
|
1693
|
+
# blocking again, this time it's okay, because we're shielded
|
|
1694
|
+
await _core.checkpoint()
|
|
1695
|
+
record.append("done")
|
|
1696
|
+
|
|
1697
|
+
with _core.CancelScope() as outer1:
|
|
1698
|
+
with _core.CancelScope() as outer2:
|
|
1699
|
+
async with _core.open_nursery() as nursery:
|
|
1700
|
+
# So we have a task blocked on an operation that can't be
|
|
1701
|
+
# aborted immediately
|
|
1702
|
+
nursery.start_soon(slow_aborter)
|
|
1703
|
+
await wait_all_tasks_blocked()
|
|
1704
|
+
assert record == ["sleeping"]
|
|
1705
|
+
# And then we cancel it, so the abort callback gets run
|
|
1706
|
+
outer1.cancel()
|
|
1707
|
+
assert record == ["sleeping", "abort-called"]
|
|
1708
|
+
# In fact that happens twice! (This used to cause the abort
|
|
1709
|
+
# callback to be run twice)
|
|
1710
|
+
outer2.cancel()
|
|
1711
|
+
assert record == ["sleeping", "abort-called"]
|
|
1712
|
+
# But then before the abort finishes, the task gets shielded!
|
|
1713
|
+
nursery.cancel_scope.shield = True
|
|
1714
|
+
# Now we wait for the task to finish...
|
|
1715
|
+
# The cancellation was delivered, even though it was shielded
|
|
1716
|
+
assert record == ["sleeping", "abort-called", "cancelled", "done"]
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
async def test_task_tree_introspection() -> None:
|
|
1720
|
+
tasks: dict[str, _core.Task] = {}
|
|
1721
|
+
nurseries: dict[str, _core.Nursery] = {}
|
|
1722
|
+
|
|
1723
|
+
async def parent(
|
|
1724
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
1725
|
+
) -> None:
|
|
1726
|
+
tasks["parent"] = _core.current_task()
|
|
1727
|
+
|
|
1728
|
+
assert tasks["parent"].child_nurseries == []
|
|
1729
|
+
|
|
1730
|
+
async with _core.open_nursery() as nursery1:
|
|
1731
|
+
async with _core.open_nursery() as nursery2:
|
|
1732
|
+
assert tasks["parent"].child_nurseries == [nursery1, nursery2]
|
|
1733
|
+
|
|
1734
|
+
assert tasks["parent"].child_nurseries == []
|
|
1735
|
+
|
|
1736
|
+
nursery: _core.Nursery | None
|
|
1737
|
+
async with _core.open_nursery() as nursery:
|
|
1738
|
+
nurseries["parent"] = nursery
|
|
1739
|
+
await nursery.start(child1)
|
|
1740
|
+
|
|
1741
|
+
# Upward links survive after tasks/nurseries exit
|
|
1742
|
+
assert nurseries["parent"].parent_task is tasks["parent"]
|
|
1743
|
+
assert tasks["child1"].parent_nursery is nurseries["parent"]
|
|
1744
|
+
assert nurseries["child1"].parent_task is tasks["child1"]
|
|
1745
|
+
assert tasks["child2"].parent_nursery is nurseries["child1"]
|
|
1746
|
+
|
|
1747
|
+
nursery = _core.current_task().parent_nursery
|
|
1748
|
+
# Make sure that chaining eventually gives a nursery of None (and not,
|
|
1749
|
+
# for example, an error)
|
|
1750
|
+
while nursery is not None:
|
|
1751
|
+
t = nursery.parent_task
|
|
1752
|
+
nursery = t.parent_nursery
|
|
1753
|
+
|
|
1754
|
+
async def child2() -> None:
|
|
1755
|
+
tasks["child2"] = _core.current_task()
|
|
1756
|
+
assert tasks["parent"].child_nurseries == [nurseries["parent"]]
|
|
1757
|
+
assert nurseries["parent"].child_tasks == frozenset({tasks["child1"]})
|
|
1758
|
+
assert tasks["child1"].child_nurseries == [nurseries["child1"]]
|
|
1759
|
+
assert nurseries["child1"].child_tasks == frozenset({tasks["child2"]})
|
|
1760
|
+
assert tasks["child2"].child_nurseries == []
|
|
1761
|
+
|
|
1762
|
+
async def child1(
|
|
1763
|
+
*,
|
|
1764
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
1765
|
+
) -> None:
|
|
1766
|
+
me = tasks["child1"] = _core.current_task()
|
|
1767
|
+
assert not_none(me.parent_nursery).parent_task is tasks["parent"]
|
|
1768
|
+
assert me.parent_nursery is not nurseries["parent"]
|
|
1769
|
+
assert me.eventual_parent_nursery is nurseries["parent"]
|
|
1770
|
+
task_status.started()
|
|
1771
|
+
assert me.parent_nursery is nurseries["parent"]
|
|
1772
|
+
assert me.eventual_parent_nursery is None
|
|
1773
|
+
|
|
1774
|
+
# Wait for the start() call to return and close its internal nursery, to
|
|
1775
|
+
# ensure consistent results in child2:
|
|
1776
|
+
await _core.wait_all_tasks_blocked()
|
|
1777
|
+
|
|
1778
|
+
async with _core.open_nursery() as nursery:
|
|
1779
|
+
nurseries["child1"] = nursery
|
|
1780
|
+
nursery.start_soon(child2)
|
|
1781
|
+
|
|
1782
|
+
async with _core.open_nursery() as nursery:
|
|
1783
|
+
nursery.start_soon(parent)
|
|
1784
|
+
|
|
1785
|
+
# There are no pending starts, so no one should have a non-None
|
|
1786
|
+
# eventual_parent_nursery
|
|
1787
|
+
for task in tasks.values():
|
|
1788
|
+
assert task.eventual_parent_nursery is None
|
|
1789
|
+
|
|
1790
|
+
|
|
1791
|
+
async def test_nursery_closure() -> None:
|
|
1792
|
+
async def child1(nursery: _core.Nursery) -> None:
|
|
1793
|
+
# We can add new tasks to the nursery even after entering __aexit__,
|
|
1794
|
+
# so long as there are still tasks running
|
|
1795
|
+
nursery.start_soon(child2)
|
|
1796
|
+
|
|
1797
|
+
async def child2() -> None:
|
|
1798
|
+
pass
|
|
1799
|
+
|
|
1800
|
+
async with _core.open_nursery() as nursery:
|
|
1801
|
+
nursery.start_soon(child1, nursery)
|
|
1802
|
+
|
|
1803
|
+
# But once we've left __aexit__, the nursery is closed
|
|
1804
|
+
with pytest.raises(RuntimeError):
|
|
1805
|
+
nursery.start_soon(child2)
|
|
1806
|
+
|
|
1807
|
+
|
|
1808
|
+
async def test_spawn_name() -> None:
|
|
1809
|
+
async def func1(expected: str) -> None:
|
|
1810
|
+
task = _core.current_task()
|
|
1811
|
+
assert expected in task.name
|
|
1812
|
+
|
|
1813
|
+
async def func2() -> None: # pragma: no cover
|
|
1814
|
+
pass
|
|
1815
|
+
|
|
1816
|
+
async def check( # type: ignore[explicit-any]
|
|
1817
|
+
spawn_fn: Callable[..., object],
|
|
1818
|
+
) -> None:
|
|
1819
|
+
spawn_fn(func1, "func1")
|
|
1820
|
+
spawn_fn(func1, "func2", name=func2)
|
|
1821
|
+
spawn_fn(func1, "func3", name="func3")
|
|
1822
|
+
spawn_fn(functools.partial(func1, "func1"))
|
|
1823
|
+
spawn_fn(func1, "object", name=object())
|
|
1824
|
+
|
|
1825
|
+
async with _core.open_nursery() as nursery:
|
|
1826
|
+
await check(nursery.start_soon)
|
|
1827
|
+
await check(_core.spawn_system_task)
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
async def test_current_effective_deadline(mock_clock: _core.MockClock) -> None:
|
|
1831
|
+
assert _core.current_effective_deadline() == inf
|
|
1832
|
+
|
|
1833
|
+
with _core.CancelScope(deadline=5) as scope1:
|
|
1834
|
+
with _core.CancelScope(deadline=10) as scope2:
|
|
1835
|
+
assert _core.current_effective_deadline() == 5
|
|
1836
|
+
scope2.deadline = 3
|
|
1837
|
+
assert _core.current_effective_deadline() == 3
|
|
1838
|
+
scope2.deadline = 10
|
|
1839
|
+
assert _core.current_effective_deadline() == 5
|
|
1840
|
+
scope2.shield = True
|
|
1841
|
+
assert _core.current_effective_deadline() == 10
|
|
1842
|
+
scope2.shield = False
|
|
1843
|
+
assert _core.current_effective_deadline() == 5
|
|
1844
|
+
scope1.cancel()
|
|
1845
|
+
assert _core.current_effective_deadline() == -inf
|
|
1846
|
+
scope2.shield = True
|
|
1847
|
+
assert _core.current_effective_deadline() == 10
|
|
1848
|
+
assert _core.current_effective_deadline() == -inf
|
|
1849
|
+
assert _core.current_effective_deadline() == inf
|
|
1850
|
+
|
|
1851
|
+
|
|
1852
|
+
def test_nice_error_on_bad_calls_to_run_or_spawn() -> None:
|
|
1853
|
+
def bad_call_run( # type: ignore[explicit-any]
|
|
1854
|
+
func: Callable[..., Awaitable[object]],
|
|
1855
|
+
*args: tuple[object, ...],
|
|
1856
|
+
) -> None:
|
|
1857
|
+
_core.run(func, *args)
|
|
1858
|
+
|
|
1859
|
+
def bad_call_spawn( # type: ignore[explicit-any]
|
|
1860
|
+
func: Callable[..., Awaitable[object]],
|
|
1861
|
+
*args: tuple[object, ...],
|
|
1862
|
+
) -> None:
|
|
1863
|
+
async def main() -> None:
|
|
1864
|
+
async with _core.open_nursery() as nursery:
|
|
1865
|
+
nursery.start_soon(func, *args)
|
|
1866
|
+
|
|
1867
|
+
_core.run(main)
|
|
1868
|
+
|
|
1869
|
+
async def f() -> None: # pragma: no cover
|
|
1870
|
+
pass
|
|
1871
|
+
|
|
1872
|
+
async def async_gen(arg: T) -> AsyncGenerator[T, None]: # pragma: no cover
|
|
1873
|
+
yield arg
|
|
1874
|
+
|
|
1875
|
+
# If/when RaisesGroup/Matcher is added to pytest in some form this test can be
|
|
1876
|
+
# rewritten to use a loop again, and avoid specifying the exceptions twice in
|
|
1877
|
+
# different ways
|
|
1878
|
+
with pytest.raises(
|
|
1879
|
+
TypeError,
|
|
1880
|
+
match=r"^Trio was expecting an async function, but instead it got a coroutine object <.*>",
|
|
1881
|
+
):
|
|
1882
|
+
bad_call_run(f()) # type: ignore[arg-type]
|
|
1883
|
+
with pytest.raises(
|
|
1884
|
+
TypeError,
|
|
1885
|
+
match=r"expected an async function but got an async generator",
|
|
1886
|
+
):
|
|
1887
|
+
bad_call_run(async_gen, 0) # type: ignore
|
|
1888
|
+
|
|
1889
|
+
# bad_call_spawn calls the function inside a nursery, so the exception will be
|
|
1890
|
+
# wrapped in an exceptiongroup
|
|
1891
|
+
with pytest.RaisesGroup(
|
|
1892
|
+
pytest.RaisesExc(TypeError, match="expecting an async function")
|
|
1893
|
+
):
|
|
1894
|
+
bad_call_spawn(f()) # type: ignore[arg-type]
|
|
1895
|
+
|
|
1896
|
+
with pytest.RaisesGroup(
|
|
1897
|
+
pytest.RaisesExc(
|
|
1898
|
+
TypeError, match="expected an async function but got an async generator"
|
|
1899
|
+
),
|
|
1900
|
+
):
|
|
1901
|
+
bad_call_spawn(async_gen, 0) # type: ignore
|
|
1902
|
+
|
|
1903
|
+
|
|
1904
|
+
def test_calling_asyncio_function_gives_nice_error() -> None:
|
|
1905
|
+
async def child_xyzzy() -> None:
|
|
1906
|
+
await create_asyncio_future_in_new_loop()
|
|
1907
|
+
|
|
1908
|
+
async def misguided() -> None:
|
|
1909
|
+
await child_xyzzy()
|
|
1910
|
+
|
|
1911
|
+
with pytest.raises(TypeError, match="asyncio") as excinfo:
|
|
1912
|
+
_core.run(misguided)
|
|
1913
|
+
|
|
1914
|
+
# The traceback should point to the location of the foreign await
|
|
1915
|
+
assert any( # pragma: no branch
|
|
1916
|
+
entry.name == "child_xyzzy" for entry in excinfo.traceback
|
|
1917
|
+
)
|
|
1918
|
+
|
|
1919
|
+
|
|
1920
|
+
async def test_asyncio_function_inside_nursery_does_not_explode() -> None:
|
|
1921
|
+
# Regression test for https://github.com/python-trio/trio/issues/552
|
|
1922
|
+
with pytest.RaisesGroup(pytest.RaisesExc(TypeError, match="asyncio")):
|
|
1923
|
+
async with _core.open_nursery() as nursery:
|
|
1924
|
+
nursery.start_soon(sleep_forever)
|
|
1925
|
+
await create_asyncio_future_in_new_loop()
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
async def test_trivial_yields() -> None:
|
|
1929
|
+
with assert_checkpoints():
|
|
1930
|
+
await _core.checkpoint()
|
|
1931
|
+
|
|
1932
|
+
with assert_checkpoints():
|
|
1933
|
+
await _core.checkpoint_if_cancelled()
|
|
1934
|
+
await _core.cancel_shielded_checkpoint()
|
|
1935
|
+
|
|
1936
|
+
# Weird case: opening and closing a nursery schedules, but doesn't check
|
|
1937
|
+
# for cancellation (unless something inside the nursery does)
|
|
1938
|
+
task = _core.current_task()
|
|
1939
|
+
before_schedule_points = task._schedule_points
|
|
1940
|
+
with _core.CancelScope() as cs:
|
|
1941
|
+
cs.cancel()
|
|
1942
|
+
async with _core.open_nursery():
|
|
1943
|
+
pass
|
|
1944
|
+
assert not cs.cancelled_caught
|
|
1945
|
+
assert task._schedule_points > before_schedule_points
|
|
1946
|
+
|
|
1947
|
+
before_schedule_points = task._schedule_points
|
|
1948
|
+
|
|
1949
|
+
async def noop_with_no_checkpoint() -> None:
|
|
1950
|
+
pass
|
|
1951
|
+
|
|
1952
|
+
with _core.CancelScope() as cs:
|
|
1953
|
+
cs.cancel()
|
|
1954
|
+
async with _core.open_nursery() as nursery:
|
|
1955
|
+
nursery.start_soon(noop_with_no_checkpoint)
|
|
1956
|
+
assert not cs.cancelled_caught
|
|
1957
|
+
|
|
1958
|
+
assert task._schedule_points > before_schedule_points
|
|
1959
|
+
|
|
1960
|
+
with _core.CancelScope() as cancel_scope:
|
|
1961
|
+
cancel_scope.cancel()
|
|
1962
|
+
with pytest.RaisesGroup(KeyError):
|
|
1963
|
+
async with _core.open_nursery():
|
|
1964
|
+
raise KeyError
|
|
1965
|
+
|
|
1966
|
+
|
|
1967
|
+
async def test_nursery_start(autojump_clock: _core.MockClock) -> None:
|
|
1968
|
+
async def no_args() -> None: # pragma: no cover
|
|
1969
|
+
pass
|
|
1970
|
+
|
|
1971
|
+
# Errors in calling convention get raised immediately from start
|
|
1972
|
+
async with _core.open_nursery() as nursery:
|
|
1973
|
+
with pytest.raises(TypeError):
|
|
1974
|
+
await nursery.start(no_args)
|
|
1975
|
+
|
|
1976
|
+
async def sleep_then_start(
|
|
1977
|
+
seconds: int,
|
|
1978
|
+
*,
|
|
1979
|
+
task_status: _core.TaskStatus[int] = _core.TASK_STATUS_IGNORED,
|
|
1980
|
+
) -> None:
|
|
1981
|
+
repr(task_status) # smoke test
|
|
1982
|
+
await sleep(seconds)
|
|
1983
|
+
task_status.started(seconds)
|
|
1984
|
+
await sleep(seconds)
|
|
1985
|
+
|
|
1986
|
+
# Basic happy-path check: start waits for the task to call started(), then
|
|
1987
|
+
# returns, passes back the value, and the given nursery then waits for it
|
|
1988
|
+
# to exit.
|
|
1989
|
+
for seconds in [1, 2]:
|
|
1990
|
+
async with _core.open_nursery() as nursery:
|
|
1991
|
+
assert len(nursery.child_tasks) == 0
|
|
1992
|
+
t0 = _core.current_time()
|
|
1993
|
+
assert await nursery.start(sleep_then_start, seconds) == seconds
|
|
1994
|
+
assert _core.current_time() - t0 == seconds
|
|
1995
|
+
assert len(nursery.child_tasks) == 1
|
|
1996
|
+
assert _core.current_time() - t0 == 2 * seconds
|
|
1997
|
+
|
|
1998
|
+
# Make sure TASK_STATUS_IGNORED works so task function can be called
|
|
1999
|
+
# directly
|
|
2000
|
+
t0 = _core.current_time()
|
|
2001
|
+
await sleep_then_start(3)
|
|
2002
|
+
assert _core.current_time() - t0 == 2 * 3
|
|
2003
|
+
|
|
2004
|
+
# calling started twice
|
|
2005
|
+
async def double_started(
|
|
2006
|
+
*,
|
|
2007
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2008
|
+
) -> None:
|
|
2009
|
+
task_status.started()
|
|
2010
|
+
with pytest.raises(RuntimeError):
|
|
2011
|
+
task_status.started()
|
|
2012
|
+
|
|
2013
|
+
async with _core.open_nursery() as nursery:
|
|
2014
|
+
await nursery.start(double_started)
|
|
2015
|
+
|
|
2016
|
+
# child crashes before calling started -> error comes out of .start()
|
|
2017
|
+
async def raise_keyerror(
|
|
2018
|
+
*,
|
|
2019
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2020
|
+
) -> None:
|
|
2021
|
+
raise KeyError("oops")
|
|
2022
|
+
|
|
2023
|
+
async with _core.open_nursery() as nursery:
|
|
2024
|
+
with pytest.raises(KeyError):
|
|
2025
|
+
await nursery.start(raise_keyerror)
|
|
2026
|
+
|
|
2027
|
+
# child exiting cleanly before calling started -> triggers a RuntimeError
|
|
2028
|
+
async def nothing(
|
|
2029
|
+
*,
|
|
2030
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2031
|
+
) -> None:
|
|
2032
|
+
return
|
|
2033
|
+
|
|
2034
|
+
async with _core.open_nursery() as nursery:
|
|
2035
|
+
with pytest.raises(RuntimeError) as excinfo1:
|
|
2036
|
+
await nursery.start(nothing)
|
|
2037
|
+
assert "exited without calling" in str(excinfo1.value)
|
|
2038
|
+
|
|
2039
|
+
# if the call to start() is cancelled, then the call to started() does
|
|
2040
|
+
# nothing -- the child keeps executing under start(). The value it passed
|
|
2041
|
+
# is ignored; start() raises Cancelled.
|
|
2042
|
+
async def just_started(
|
|
2043
|
+
*,
|
|
2044
|
+
task_status: _core.TaskStatus[str] = _core.TASK_STATUS_IGNORED,
|
|
2045
|
+
) -> None:
|
|
2046
|
+
task_status.started("hi")
|
|
2047
|
+
await _core.checkpoint()
|
|
2048
|
+
|
|
2049
|
+
async with _core.open_nursery() as nursery:
|
|
2050
|
+
with _core.CancelScope() as cs:
|
|
2051
|
+
cs.cancel()
|
|
2052
|
+
with pytest.raises(_core.Cancelled):
|
|
2053
|
+
await nursery.start(just_started)
|
|
2054
|
+
|
|
2055
|
+
# but if the task does not execute any checkpoints, and exits, then start()
|
|
2056
|
+
# doesn't raise Cancelled, since the task completed successfully.
|
|
2057
|
+
async def started_with_no_checkpoint(
|
|
2058
|
+
*,
|
|
2059
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2060
|
+
) -> None:
|
|
2061
|
+
task_status.started(None)
|
|
2062
|
+
|
|
2063
|
+
async with _core.open_nursery() as nursery:
|
|
2064
|
+
with _core.CancelScope() as cs:
|
|
2065
|
+
cs.cancel()
|
|
2066
|
+
await nursery.start(started_with_no_checkpoint)
|
|
2067
|
+
assert not cs.cancelled_caught
|
|
2068
|
+
|
|
2069
|
+
# and since starting in a cancelled context makes started() a no-op, if
|
|
2070
|
+
# the child crashes after calling started(), the error can *still* come
|
|
2071
|
+
# out of start()
|
|
2072
|
+
async def raise_keyerror_after_started(
|
|
2073
|
+
*,
|
|
2074
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2075
|
+
) -> None:
|
|
2076
|
+
task_status.started()
|
|
2077
|
+
raise KeyError("whoopsiedaisy")
|
|
2078
|
+
|
|
2079
|
+
async with _core.open_nursery() as nursery:
|
|
2080
|
+
with _core.CancelScope() as cs:
|
|
2081
|
+
cs.cancel()
|
|
2082
|
+
with pytest.raises(KeyError):
|
|
2083
|
+
await nursery.start(raise_keyerror_after_started)
|
|
2084
|
+
|
|
2085
|
+
# trying to start in a closed nursery raises an error immediately
|
|
2086
|
+
async with _core.open_nursery() as closed_nursery:
|
|
2087
|
+
pass
|
|
2088
|
+
t0 = _core.current_time()
|
|
2089
|
+
with pytest.raises(RuntimeError):
|
|
2090
|
+
await closed_nursery.start(sleep_then_start, 7)
|
|
2091
|
+
# sub-second delays can be caused by unrelated multitasking by an OS
|
|
2092
|
+
assert int(_core.current_time()) == int(t0)
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
async def test_task_nursery_stack() -> None:
|
|
2096
|
+
task = _core.current_task()
|
|
2097
|
+
assert task._child_nurseries == []
|
|
2098
|
+
async with _core.open_nursery() as nursery1:
|
|
2099
|
+
assert task._child_nurseries == [nursery1]
|
|
2100
|
+
with pytest.RaisesGroup(KeyError):
|
|
2101
|
+
async with _core.open_nursery() as nursery2:
|
|
2102
|
+
assert task._child_nurseries == [nursery1, nursery2]
|
|
2103
|
+
raise KeyError
|
|
2104
|
+
assert task._child_nurseries == [nursery1]
|
|
2105
|
+
assert task._child_nurseries == []
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
async def test_nursery_start_with_cancelled_nursery() -> None:
|
|
2109
|
+
# This function isn't testing task_status, it's using task_status as a
|
|
2110
|
+
# convenient way to get a nursery that we can test spawning stuff into.
|
|
2111
|
+
async def setup_nursery(
|
|
2112
|
+
task_status: _core.TaskStatus[_core.Nursery] = _core.TASK_STATUS_IGNORED,
|
|
2113
|
+
) -> None:
|
|
2114
|
+
async with _core.open_nursery() as nursery:
|
|
2115
|
+
task_status.started(nursery)
|
|
2116
|
+
await sleep_forever()
|
|
2117
|
+
|
|
2118
|
+
# Calls started() while children are asleep, so we can make sure
|
|
2119
|
+
# that the cancellation machinery notices and aborts when a sleeping task
|
|
2120
|
+
# is moved into a cancelled scope.
|
|
2121
|
+
async def sleeping_children(
|
|
2122
|
+
fn: Callable[[], object],
|
|
2123
|
+
*,
|
|
2124
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2125
|
+
) -> None:
|
|
2126
|
+
async with _core.open_nursery() as nursery:
|
|
2127
|
+
nursery.start_soon(sleep_forever)
|
|
2128
|
+
nursery.start_soon(sleep_forever)
|
|
2129
|
+
await wait_all_tasks_blocked()
|
|
2130
|
+
fn()
|
|
2131
|
+
task_status.started()
|
|
2132
|
+
|
|
2133
|
+
# Cancelling the setup_nursery just *before* calling started()
|
|
2134
|
+
async with _core.open_nursery() as nursery:
|
|
2135
|
+
value = await nursery.start(setup_nursery)
|
|
2136
|
+
assert isinstance(value, _core.Nursery)
|
|
2137
|
+
target_nursery: _core.Nursery = value
|
|
2138
|
+
await target_nursery.start(
|
|
2139
|
+
sleeping_children,
|
|
2140
|
+
target_nursery.cancel_scope.cancel,
|
|
2141
|
+
)
|
|
2142
|
+
|
|
2143
|
+
# Cancelling the setup_nursery just *after* calling started()
|
|
2144
|
+
async with _core.open_nursery() as nursery:
|
|
2145
|
+
value = await nursery.start(setup_nursery)
|
|
2146
|
+
assert isinstance(value, _core.Nursery)
|
|
2147
|
+
target_nursery = value
|
|
2148
|
+
await target_nursery.start(sleeping_children, lambda: None)
|
|
2149
|
+
target_nursery.cancel_scope.cancel()
|
|
2150
|
+
|
|
2151
|
+
|
|
2152
|
+
async def test_nursery_start_keeps_nursery_open(
|
|
2153
|
+
autojump_clock: _core.MockClock,
|
|
2154
|
+
) -> None:
|
|
2155
|
+
async def sleep_a_bit(
|
|
2156
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2157
|
+
) -> None:
|
|
2158
|
+
await sleep(2)
|
|
2159
|
+
task_status.started()
|
|
2160
|
+
await sleep(3)
|
|
2161
|
+
|
|
2162
|
+
async with _core.open_nursery() as nursery1:
|
|
2163
|
+
t0 = _core.current_time()
|
|
2164
|
+
async with _core.open_nursery() as nursery2:
|
|
2165
|
+
# Start the 'start' call running in the background
|
|
2166
|
+
nursery1.start_soon(nursery2.start, sleep_a_bit)
|
|
2167
|
+
# Sleep a bit
|
|
2168
|
+
await sleep(1)
|
|
2169
|
+
# Start another one.
|
|
2170
|
+
nursery1.start_soon(nursery2.start, sleep_a_bit)
|
|
2171
|
+
# Then exit this nursery. At this point, there are no tasks
|
|
2172
|
+
# present in this nursery -- the only thing keeping it open is
|
|
2173
|
+
# that the tasks will be placed into it soon, when they call
|
|
2174
|
+
# started().
|
|
2175
|
+
assert _core.current_time() - t0 == 6
|
|
2176
|
+
|
|
2177
|
+
# Check that it still works even if the task that the nursery is waiting
|
|
2178
|
+
# for ends up crashing, and never actually enters the nursery.
|
|
2179
|
+
async def sleep_then_crash(
|
|
2180
|
+
task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED,
|
|
2181
|
+
) -> None:
|
|
2182
|
+
await sleep(7)
|
|
2183
|
+
raise KeyError
|
|
2184
|
+
|
|
2185
|
+
async def start_sleep_then_crash(nursery: _core.Nursery) -> None:
|
|
2186
|
+
with pytest.raises(KeyError):
|
|
2187
|
+
await nursery.start(sleep_then_crash)
|
|
2188
|
+
|
|
2189
|
+
async with _core.open_nursery() as nursery1:
|
|
2190
|
+
t0 = _core.current_time()
|
|
2191
|
+
async with _core.open_nursery() as nursery2:
|
|
2192
|
+
nursery1.start_soon(start_sleep_then_crash, nursery2)
|
|
2193
|
+
await wait_all_tasks_blocked()
|
|
2194
|
+
assert _core.current_time() - t0 == 7
|
|
2195
|
+
|
|
2196
|
+
|
|
2197
|
+
async def test_nursery_explicit_exception() -> None:
|
|
2198
|
+
with pytest.RaisesGroup(KeyError):
|
|
2199
|
+
async with _core.open_nursery():
|
|
2200
|
+
raise KeyError()
|
|
2201
|
+
|
|
2202
|
+
|
|
2203
|
+
async def test_nursery_stop_iteration() -> None:
|
|
2204
|
+
async def fail() -> NoReturn:
|
|
2205
|
+
raise ValueError
|
|
2206
|
+
|
|
2207
|
+
with pytest.RaisesGroup(StopIteration, ValueError):
|
|
2208
|
+
async with _core.open_nursery() as nursery:
|
|
2209
|
+
nursery.start_soon(fail)
|
|
2210
|
+
raise StopIteration
|
|
2211
|
+
|
|
2212
|
+
|
|
2213
|
+
async def test_nursery_stop_async_iteration() -> None:
|
|
2214
|
+
class it:
|
|
2215
|
+
def __init__(self, count: int) -> None:
|
|
2216
|
+
self.count = count
|
|
2217
|
+
self.val = 0
|
|
2218
|
+
|
|
2219
|
+
async def __anext__(self) -> int:
|
|
2220
|
+
await sleep(0)
|
|
2221
|
+
val = self.val
|
|
2222
|
+
if val >= self.count:
|
|
2223
|
+
raise StopAsyncIteration
|
|
2224
|
+
self.val += 1
|
|
2225
|
+
return val
|
|
2226
|
+
|
|
2227
|
+
class async_zip:
|
|
2228
|
+
def __init__(self, *largs: it) -> None:
|
|
2229
|
+
self.nexts = [obj.__anext__ for obj in largs]
|
|
2230
|
+
|
|
2231
|
+
async def _accumulate(
|
|
2232
|
+
self,
|
|
2233
|
+
f: Callable[[], Awaitable[int]],
|
|
2234
|
+
items: list[int],
|
|
2235
|
+
i: int,
|
|
2236
|
+
) -> None:
|
|
2237
|
+
items[i] = await f()
|
|
2238
|
+
|
|
2239
|
+
def __aiter__(self) -> async_zip:
|
|
2240
|
+
return self
|
|
2241
|
+
|
|
2242
|
+
async def __anext__(self) -> list[int]:
|
|
2243
|
+
nexts = self.nexts
|
|
2244
|
+
items: list[int] = [-1] * len(nexts)
|
|
2245
|
+
|
|
2246
|
+
try:
|
|
2247
|
+
async with _core.open_nursery() as nursery:
|
|
2248
|
+
for i, f in enumerate(nexts):
|
|
2249
|
+
nursery.start_soon(self._accumulate, f, items, i)
|
|
2250
|
+
except ExceptionGroup as e:
|
|
2251
|
+
# With strict_exception_groups enabled, users now need to unwrap
|
|
2252
|
+
# StopAsyncIteration and re-raise it.
|
|
2253
|
+
# This would be relatively clean on python3.11+ with except*.
|
|
2254
|
+
# We could also use pytest.RaisesGroup, but that's primarily meant as
|
|
2255
|
+
# test infra, not as a runtime tool.
|
|
2256
|
+
if len(e.exceptions) == 1 and isinstance(
|
|
2257
|
+
e.exceptions[0],
|
|
2258
|
+
StopAsyncIteration,
|
|
2259
|
+
):
|
|
2260
|
+
raise e.exceptions[0] from None
|
|
2261
|
+
else: # pragma: no cover
|
|
2262
|
+
raise AssertionError("unknown error in _accumulate") from e
|
|
2263
|
+
|
|
2264
|
+
return items
|
|
2265
|
+
|
|
2266
|
+
result: list[list[int]] = [vals async for vals in async_zip(it(4), it(2))]
|
|
2267
|
+
assert result == [[0, 0], [1, 1]]
|
|
2268
|
+
|
|
2269
|
+
|
|
2270
|
+
async def test_traceback_frame_removal() -> None:
|
|
2271
|
+
async def my_child_task() -> NoReturn:
|
|
2272
|
+
raise KeyError()
|
|
2273
|
+
|
|
2274
|
+
def check_traceback(exc: KeyError) -> bool:
|
|
2275
|
+
# The top frame in the exception traceback should be inside the child
|
|
2276
|
+
# task, not trio/contextvars internals. And there's only one frame
|
|
2277
|
+
# inside the child task, so this will also detect if our frame-removal
|
|
2278
|
+
# is too eager.
|
|
2279
|
+
tb = exc.__traceback__
|
|
2280
|
+
assert tb is not None
|
|
2281
|
+
return tb.tb_frame.f_code is my_child_task.__code__
|
|
2282
|
+
|
|
2283
|
+
with pytest.RaisesGroup(pytest.RaisesExc(KeyError, check=check_traceback)):
|
|
2284
|
+
# For now cancel/nursery scopes still leave a bunch of tb gunk behind.
|
|
2285
|
+
# But if there's an Exceptiongroup, they leave it on the group,
|
|
2286
|
+
# which lets us get a clean look at the KeyError itself.
|
|
2287
|
+
async with _core.open_nursery() as nursery:
|
|
2288
|
+
nursery.start_soon(my_child_task)
|
|
2289
|
+
|
|
2290
|
+
|
|
2291
|
+
def test_contextvar_support() -> None:
|
|
2292
|
+
var: contextvars.ContextVar[str] = contextvars.ContextVar("test")
|
|
2293
|
+
var.set("before")
|
|
2294
|
+
|
|
2295
|
+
assert var.get() == "before"
|
|
2296
|
+
|
|
2297
|
+
async def inner() -> None:
|
|
2298
|
+
task = _core.current_task()
|
|
2299
|
+
assert task.context.get(var) == "before"
|
|
2300
|
+
assert var.get() == "before"
|
|
2301
|
+
var.set("after")
|
|
2302
|
+
assert var.get() == "after"
|
|
2303
|
+
assert var in task.context
|
|
2304
|
+
assert task.context.get(var) == "after"
|
|
2305
|
+
|
|
2306
|
+
_core.run(inner)
|
|
2307
|
+
assert var.get() == "before"
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
async def test_contextvar_multitask() -> None:
|
|
2311
|
+
var = contextvars.ContextVar("test", default="hmmm")
|
|
2312
|
+
|
|
2313
|
+
async def t1() -> None:
|
|
2314
|
+
assert var.get() == "hmmm"
|
|
2315
|
+
var.set("hmmmm")
|
|
2316
|
+
assert var.get() == "hmmmm"
|
|
2317
|
+
|
|
2318
|
+
async def t2() -> None:
|
|
2319
|
+
assert var.get() == "hmmmm"
|
|
2320
|
+
|
|
2321
|
+
async with _core.open_nursery() as n:
|
|
2322
|
+
n.start_soon(t1)
|
|
2323
|
+
await wait_all_tasks_blocked()
|
|
2324
|
+
assert var.get() == "hmmm"
|
|
2325
|
+
var.set("hmmmm")
|
|
2326
|
+
n.start_soon(t2)
|
|
2327
|
+
await wait_all_tasks_blocked()
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
def test_system_task_contexts() -> None:
|
|
2331
|
+
cvar: contextvars.ContextVar[str] = contextvars.ContextVar("qwilfish")
|
|
2332
|
+
cvar.set("water")
|
|
2333
|
+
|
|
2334
|
+
async def system_task() -> None:
|
|
2335
|
+
assert cvar.get() == "water"
|
|
2336
|
+
|
|
2337
|
+
async def regular_task() -> None:
|
|
2338
|
+
assert cvar.get() == "poison"
|
|
2339
|
+
|
|
2340
|
+
async def inner() -> None:
|
|
2341
|
+
async with _core.open_nursery() as nursery:
|
|
2342
|
+
cvar.set("poison")
|
|
2343
|
+
nursery.start_soon(regular_task)
|
|
2344
|
+
_core.spawn_system_task(system_task)
|
|
2345
|
+
await wait_all_tasks_blocked()
|
|
2346
|
+
|
|
2347
|
+
_core.run(inner)
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
async def test_Nursery_init() -> None:
|
|
2351
|
+
"""Test that nurseries cannot be constructed directly."""
|
|
2352
|
+
# This function is async so that we have access to a task object we can
|
|
2353
|
+
# pass in. It should never be accessed though.
|
|
2354
|
+
task = _core.current_task()
|
|
2355
|
+
scope = _core.CancelScope()
|
|
2356
|
+
with pytest.raises(TypeError):
|
|
2357
|
+
_core._run.Nursery(task, scope, True)
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
async def test_Nursery_private_init() -> None:
|
|
2361
|
+
# context manager creation should not raise
|
|
2362
|
+
async with _core.open_nursery() as nursery:
|
|
2363
|
+
assert not nursery._closed
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
def test_Nursery_subclass() -> None:
|
|
2367
|
+
with pytest.raises(TypeError):
|
|
2368
|
+
type("Subclass", (_core._run.Nursery,), {})
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
def test_CancelScope_subclass() -> None:
|
|
2372
|
+
with pytest.raises(TypeError):
|
|
2373
|
+
type("Subclass", (_core.CancelScope,), {})
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
def test_sniffio_integration() -> None:
|
|
2377
|
+
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
|
|
2378
|
+
sniffio.current_async_library()
|
|
2379
|
+
|
|
2380
|
+
async def check_inside_trio() -> None:
|
|
2381
|
+
assert sniffio.current_async_library() == "trio"
|
|
2382
|
+
|
|
2383
|
+
def check_function_returning_coroutine() -> Awaitable[object]:
|
|
2384
|
+
assert sniffio.current_async_library() == "trio"
|
|
2385
|
+
return check_inside_trio()
|
|
2386
|
+
|
|
2387
|
+
_core.run(check_inside_trio)
|
|
2388
|
+
|
|
2389
|
+
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
|
|
2390
|
+
sniffio.current_async_library()
|
|
2391
|
+
|
|
2392
|
+
@contextmanager
|
|
2393
|
+
def alternate_sniffio_library() -> Generator[None, None, None]:
|
|
2394
|
+
prev_token = sniffio.current_async_library_cvar.set("nullio")
|
|
2395
|
+
prev_library, sniffio.thread_local.name = sniffio.thread_local.name, "nullio"
|
|
2396
|
+
try:
|
|
2397
|
+
yield
|
|
2398
|
+
assert sniffio.current_async_library() == "nullio"
|
|
2399
|
+
finally:
|
|
2400
|
+
sniffio.thread_local.name = prev_library
|
|
2401
|
+
sniffio.current_async_library_cvar.reset(prev_token)
|
|
2402
|
+
|
|
2403
|
+
async def check_new_task_resets_sniffio_library() -> None:
|
|
2404
|
+
with alternate_sniffio_library():
|
|
2405
|
+
_core.spawn_system_task(check_inside_trio)
|
|
2406
|
+
async with _core.open_nursery() as nursery:
|
|
2407
|
+
with alternate_sniffio_library():
|
|
2408
|
+
nursery.start_soon(check_inside_trio)
|
|
2409
|
+
nursery.start_soon(check_function_returning_coroutine)
|
|
2410
|
+
|
|
2411
|
+
_core.run(check_new_task_resets_sniffio_library)
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
async def test_Task_custom_sleep_data() -> None:
|
|
2415
|
+
task = _core.current_task()
|
|
2416
|
+
assert task.custom_sleep_data is None
|
|
2417
|
+
task.custom_sleep_data = 1
|
|
2418
|
+
assert task.custom_sleep_data == 1
|
|
2419
|
+
await _core.checkpoint()
|
|
2420
|
+
assert task.custom_sleep_data is None
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
@types.coroutine
|
|
2424
|
+
def async_yield(value: T) -> Generator[T, None, None]:
|
|
2425
|
+
yield value
|
|
2426
|
+
|
|
2427
|
+
|
|
2428
|
+
async def test_permanently_detach_coroutine_object() -> None:
|
|
2429
|
+
task: _core.Task | None = None
|
|
2430
|
+
pdco_outcome: outcome.Outcome[str] | None = None
|
|
2431
|
+
|
|
2432
|
+
async def detachable_coroutine(
|
|
2433
|
+
task_outcome: outcome.Outcome[None],
|
|
2434
|
+
yield_value: object,
|
|
2435
|
+
) -> None:
|
|
2436
|
+
await sleep(0)
|
|
2437
|
+
nonlocal task, pdco_outcome
|
|
2438
|
+
task = _core.current_task()
|
|
2439
|
+
# `No overload variant of "acapture" matches argument types "Callable[[Outcome[object]], Coroutine[Any, Any, object]]", "Outcome[None]"`
|
|
2440
|
+
pdco_outcome = await outcome.acapture( # type: ignore[call-overload]
|
|
2441
|
+
_core.permanently_detach_coroutine_object,
|
|
2442
|
+
task_outcome,
|
|
2443
|
+
)
|
|
2444
|
+
await async_yield(yield_value)
|
|
2445
|
+
|
|
2446
|
+
async with _core.open_nursery() as nursery:
|
|
2447
|
+
nursery.start_soon(detachable_coroutine, outcome.Value(None), "I'm free!")
|
|
2448
|
+
|
|
2449
|
+
# If we get here then Trio thinks the task has exited... but the coroutine
|
|
2450
|
+
# is still iterable. At that point anything can be sent into the coroutine, so the .coro type
|
|
2451
|
+
# is wrong.
|
|
2452
|
+
assert pdco_outcome is None
|
|
2453
|
+
# `Argument 1 to "send" of "Coroutine" has incompatible type "str"; expected "Outcome[object]"`
|
|
2454
|
+
assert not_none(task).coro.send("be free!") == "I'm free!" # type: ignore[arg-type]
|
|
2455
|
+
assert pdco_outcome == outcome.Value("be free!")
|
|
2456
|
+
with pytest.raises(StopIteration):
|
|
2457
|
+
not_none(task).coro.send(None) # type: ignore[arg-type]
|
|
2458
|
+
|
|
2459
|
+
# Check the exception paths too
|
|
2460
|
+
task = None
|
|
2461
|
+
pdco_outcome = None
|
|
2462
|
+
with pytest.RaisesGroup(KeyError):
|
|
2463
|
+
async with _core.open_nursery() as nursery:
|
|
2464
|
+
nursery.start_soon(detachable_coroutine, outcome.Error(KeyError()), "uh oh")
|
|
2465
|
+
throw_in = ValueError()
|
|
2466
|
+
assert isinstance(task, _core.Task) # For type checkers.
|
|
2467
|
+
assert not_none(task).coro.throw(throw_in) == "uh oh"
|
|
2468
|
+
assert pdco_outcome == outcome.Error(throw_in)
|
|
2469
|
+
with pytest.raises(StopIteration):
|
|
2470
|
+
task.coro.send(None)
|
|
2471
|
+
|
|
2472
|
+
async def bad_detach() -> None:
|
|
2473
|
+
async with _core.open_nursery():
|
|
2474
|
+
with pytest.raises(RuntimeError) as excinfo:
|
|
2475
|
+
await _core.permanently_detach_coroutine_object(outcome.Value(None))
|
|
2476
|
+
assert "open nurser" in str(excinfo.value)
|
|
2477
|
+
|
|
2478
|
+
async with _core.open_nursery() as nursery:
|
|
2479
|
+
nursery.start_soon(bad_detach)
|
|
2480
|
+
|
|
2481
|
+
|
|
2482
|
+
async def test_detach_and_reattach_coroutine_object() -> None:
|
|
2483
|
+
unrelated_task: _core.Task | None = None
|
|
2484
|
+
task: _core.Task | None = None
|
|
2485
|
+
|
|
2486
|
+
async def unrelated_coroutine() -> None:
|
|
2487
|
+
nonlocal unrelated_task
|
|
2488
|
+
unrelated_task = _core.current_task()
|
|
2489
|
+
|
|
2490
|
+
async def reattachable_coroutine() -> None:
|
|
2491
|
+
nonlocal task
|
|
2492
|
+
await sleep(0)
|
|
2493
|
+
|
|
2494
|
+
task = _core.current_task()
|
|
2495
|
+
|
|
2496
|
+
def abort_fn(_: _core.RaiseCancelT) -> _core.Abort: # pragma: no cover
|
|
2497
|
+
return _core.Abort.FAILED
|
|
2498
|
+
|
|
2499
|
+
got = await _core.temporarily_detach_coroutine_object(abort_fn)
|
|
2500
|
+
assert got == "not trio!"
|
|
2501
|
+
|
|
2502
|
+
await async_yield(1)
|
|
2503
|
+
await async_yield(2)
|
|
2504
|
+
|
|
2505
|
+
with pytest.raises(RuntimeError) as excinfo:
|
|
2506
|
+
await _core.reattach_detached_coroutine_object(
|
|
2507
|
+
not_none(unrelated_task),
|
|
2508
|
+
None,
|
|
2509
|
+
)
|
|
2510
|
+
assert "does not match" in str(excinfo.value)
|
|
2511
|
+
|
|
2512
|
+
await _core.reattach_detached_coroutine_object(task, "byebye")
|
|
2513
|
+
|
|
2514
|
+
await sleep(0)
|
|
2515
|
+
|
|
2516
|
+
async with _core.open_nursery() as nursery:
|
|
2517
|
+
nursery.start_soon(unrelated_coroutine)
|
|
2518
|
+
nursery.start_soon(reattachable_coroutine)
|
|
2519
|
+
await wait_all_tasks_blocked()
|
|
2520
|
+
|
|
2521
|
+
# Okay, it's detached. Here's our coroutine runner:
|
|
2522
|
+
# `Argument 1 to "send" of "Coroutine" has incompatible type "str"; expected "Outcome[object]"`
|
|
2523
|
+
assert not_none(task).coro.send("not trio!") == 1 # type: ignore[arg-type]
|
|
2524
|
+
assert not_none(task).coro.send(None) == 2 # type: ignore[arg-type]
|
|
2525
|
+
assert not_none(task).coro.send(None) == "byebye" # type: ignore[arg-type]
|
|
2526
|
+
|
|
2527
|
+
# Now it's been reattached, and we can leave the nursery
|
|
2528
|
+
|
|
2529
|
+
|
|
2530
|
+
async def test_detached_coroutine_cancellation() -> None:
|
|
2531
|
+
abort_fn_called = False
|
|
2532
|
+
task: _core.Task | None = None
|
|
2533
|
+
|
|
2534
|
+
async def reattachable_coroutine() -> None:
|
|
2535
|
+
await sleep(0)
|
|
2536
|
+
|
|
2537
|
+
nonlocal task
|
|
2538
|
+
task = _core.current_task()
|
|
2539
|
+
|
|
2540
|
+
def abort_fn(_: _core.RaiseCancelT) -> _core.Abort:
|
|
2541
|
+
nonlocal abort_fn_called
|
|
2542
|
+
abort_fn_called = True
|
|
2543
|
+
return _core.Abort.FAILED
|
|
2544
|
+
|
|
2545
|
+
await _core.temporarily_detach_coroutine_object(abort_fn)
|
|
2546
|
+
await _core.reattach_detached_coroutine_object(task, None)
|
|
2547
|
+
with pytest.raises(_core.Cancelled):
|
|
2548
|
+
await sleep(0)
|
|
2549
|
+
|
|
2550
|
+
async with _core.open_nursery() as nursery:
|
|
2551
|
+
nursery.start_soon(reattachable_coroutine)
|
|
2552
|
+
await wait_all_tasks_blocked()
|
|
2553
|
+
assert task is not None
|
|
2554
|
+
nursery.cancel_scope.cancel()
|
|
2555
|
+
# `Argument 1 to "send" of "Coroutine" has incompatible type "None"; expected "Outcome[object]"`
|
|
2556
|
+
task.coro.send(None) # type: ignore[arg-type]
|
|
2557
|
+
|
|
2558
|
+
assert abort_fn_called
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
@restore_unraisablehook()
|
|
2562
|
+
def test_async_function_implemented_in_C() -> None:
|
|
2563
|
+
# These used to crash because we'd try to mutate the coroutine object's
|
|
2564
|
+
# cr_frame, but C functions don't have Python frames.
|
|
2565
|
+
|
|
2566
|
+
async def agen_fn(record: list[str]) -> AsyncIterator[None]:
|
|
2567
|
+
assert not _core.currently_ki_protected()
|
|
2568
|
+
record.append("the generator ran")
|
|
2569
|
+
yield
|
|
2570
|
+
|
|
2571
|
+
run_record: list[str] = []
|
|
2572
|
+
agen = agen_fn(run_record)
|
|
2573
|
+
_core.run(agen.__anext__)
|
|
2574
|
+
assert run_record == ["the generator ran"]
|
|
2575
|
+
|
|
2576
|
+
async def main() -> None:
|
|
2577
|
+
start_soon_record: list[str] = []
|
|
2578
|
+
agen = agen_fn(start_soon_record)
|
|
2579
|
+
async with _core.open_nursery() as nursery:
|
|
2580
|
+
nursery.start_soon(agen.__anext__)
|
|
2581
|
+
assert start_soon_record == ["the generator ran"]
|
|
2582
|
+
|
|
2583
|
+
_core.run(main)
|
|
2584
|
+
|
|
2585
|
+
|
|
2586
|
+
async def test_very_deep_cancel_scope_nesting() -> None:
|
|
2587
|
+
# This used to crash with a RecursionError in CancelStatus.recalculate
|
|
2588
|
+
with ExitStack() as exit_stack:
|
|
2589
|
+
outermost_scope = _core.CancelScope()
|
|
2590
|
+
exit_stack.enter_context(outermost_scope)
|
|
2591
|
+
for _ in range(5000):
|
|
2592
|
+
exit_stack.enter_context(_core.CancelScope())
|
|
2593
|
+
outermost_scope.cancel()
|
|
2594
|
+
|
|
2595
|
+
|
|
2596
|
+
async def test_cancel_scope_deadline_duplicates() -> None:
|
|
2597
|
+
# This exercises an assert in Deadlines._prune, by intentionally creating
|
|
2598
|
+
# duplicate entries in the deadline heap.
|
|
2599
|
+
now = _core.current_time()
|
|
2600
|
+
with _core.CancelScope() as cscope:
|
|
2601
|
+
for _ in range(DEADLINE_HEAP_MIN_PRUNE_THRESHOLD * 2):
|
|
2602
|
+
cscope.deadline = now + 9998
|
|
2603
|
+
cscope.deadline = now + 9999
|
|
2604
|
+
await sleep(0.01)
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
# I don't know if this one can fail anymore, the `del` next to the comment that used to
|
|
2608
|
+
# refer to this only seems to break test_cancel_scope_exit_doesnt_create_cyclic_garbage
|
|
2609
|
+
# We're keeping it for now to cover Outcome and potential future refactoring
|
|
2610
|
+
@pytest.mark.skipif(
|
|
2611
|
+
sys.implementation.name != "cpython",
|
|
2612
|
+
reason="Only makes sense with refcounting GC",
|
|
2613
|
+
)
|
|
2614
|
+
async def test_simple_cancel_scope_usage_doesnt_create_cyclic_garbage() -> None:
|
|
2615
|
+
# https://github.com/python-trio/trio/issues/1770
|
|
2616
|
+
gc.collect()
|
|
2617
|
+
|
|
2618
|
+
async def do_a_cancel() -> None:
|
|
2619
|
+
with _core.CancelScope() as cscope:
|
|
2620
|
+
cscope.cancel()
|
|
2621
|
+
await sleep_forever()
|
|
2622
|
+
|
|
2623
|
+
async def crasher() -> NoReturn:
|
|
2624
|
+
raise ValueError("this is a crash")
|
|
2625
|
+
|
|
2626
|
+
old_flags = gc.get_debug()
|
|
2627
|
+
try:
|
|
2628
|
+
gc.collect()
|
|
2629
|
+
gc.set_debug(gc.DEBUG_SAVEALL)
|
|
2630
|
+
|
|
2631
|
+
# cover outcome.Error.unwrap
|
|
2632
|
+
# (See https://github.com/python-trio/outcome/pull/29)
|
|
2633
|
+
await do_a_cancel()
|
|
2634
|
+
# cover outcome.Error.unwrap if unrolled_run hangs on to exception refs
|
|
2635
|
+
# (See https://github.com/python-trio/trio/pull/1864)
|
|
2636
|
+
await do_a_cancel()
|
|
2637
|
+
|
|
2638
|
+
with pytest.RaisesGroup(
|
|
2639
|
+
pytest.RaisesExc(ValueError, match="^this is a crash$")
|
|
2640
|
+
):
|
|
2641
|
+
async with _core.open_nursery() as nursery:
|
|
2642
|
+
# cover NurseryManager.__aexit__
|
|
2643
|
+
nursery.start_soon(crasher)
|
|
2644
|
+
|
|
2645
|
+
gc.collect()
|
|
2646
|
+
assert not gc.garbage
|
|
2647
|
+
finally:
|
|
2648
|
+
gc.set_debug(old_flags)
|
|
2649
|
+
gc.garbage.clear()
|
|
2650
|
+
|
|
2651
|
+
|
|
2652
|
+
@pytest.mark.skipif(
|
|
2653
|
+
sys.implementation.name != "cpython",
|
|
2654
|
+
reason="Only makes sense with refcounting GC",
|
|
2655
|
+
)
|
|
2656
|
+
async def test_cancel_scope_exit_doesnt_create_cyclic_garbage() -> None:
|
|
2657
|
+
# https://github.com/python-trio/trio/pull/2063
|
|
2658
|
+
gc.collect()
|
|
2659
|
+
|
|
2660
|
+
async def crasher() -> NoReturn:
|
|
2661
|
+
raise ValueError("this is a crash")
|
|
2662
|
+
|
|
2663
|
+
old_flags = gc.get_debug()
|
|
2664
|
+
try:
|
|
2665
|
+
with (
|
|
2666
|
+
pytest.RaisesGroup(
|
|
2667
|
+
pytest.RaisesExc(ValueError, match="^this is a crash$"),
|
|
2668
|
+
),
|
|
2669
|
+
_core.CancelScope() as outer,
|
|
2670
|
+
):
|
|
2671
|
+
async with _core.open_nursery() as nursery:
|
|
2672
|
+
gc.collect()
|
|
2673
|
+
gc.set_debug(gc.DEBUG_SAVEALL)
|
|
2674
|
+
# One child that gets cancelled by the outer scope
|
|
2675
|
+
nursery.start_soon(sleep_forever)
|
|
2676
|
+
outer.cancel()
|
|
2677
|
+
# And one that raises a different error
|
|
2678
|
+
nursery.start_soon(crasher)
|
|
2679
|
+
# so that outer filters a Cancelled from the ExceptionGroup and
|
|
2680
|
+
# covers CancelScope.__exit__ (and NurseryManager.__aexit__)
|
|
2681
|
+
# (See https://github.com/python-trio/trio/pull/2063)
|
|
2682
|
+
|
|
2683
|
+
gc.collect()
|
|
2684
|
+
assert not gc.garbage
|
|
2685
|
+
finally:
|
|
2686
|
+
gc.set_debug(old_flags)
|
|
2687
|
+
gc.garbage.clear()
|
|
2688
|
+
|
|
2689
|
+
|
|
2690
|
+
@pytest.mark.skipif(
|
|
2691
|
+
sys.implementation.name != "cpython",
|
|
2692
|
+
reason="Only makes sense with refcounting GC",
|
|
2693
|
+
)
|
|
2694
|
+
async def test_nursery_cancel_doesnt_create_cyclic_garbage() -> None:
|
|
2695
|
+
collected = False
|
|
2696
|
+
|
|
2697
|
+
# https://github.com/python-trio/trio/issues/1770#issuecomment-730229423
|
|
2698
|
+
def toggle_collected() -> None:
|
|
2699
|
+
nonlocal collected
|
|
2700
|
+
collected = True
|
|
2701
|
+
|
|
2702
|
+
gc.collect()
|
|
2703
|
+
old_flags = gc.get_debug()
|
|
2704
|
+
try:
|
|
2705
|
+
gc.set_debug(0)
|
|
2706
|
+
gc.collect()
|
|
2707
|
+
gc.set_debug(gc.DEBUG_SAVEALL)
|
|
2708
|
+
|
|
2709
|
+
# cover Nursery._nested_child_finished
|
|
2710
|
+
async with _core.open_nursery() as nursery:
|
|
2711
|
+
nursery.cancel_scope.cancel()
|
|
2712
|
+
|
|
2713
|
+
weakref.finalize(nursery, toggle_collected)
|
|
2714
|
+
del nursery
|
|
2715
|
+
# a checkpoint clears the nursery from the internals, apparently
|
|
2716
|
+
# TODO: stop event loop from hanging on to the nursery at this point
|
|
2717
|
+
await _core.checkpoint()
|
|
2718
|
+
|
|
2719
|
+
assert collected
|
|
2720
|
+
gc.collect()
|
|
2721
|
+
assert not gc.garbage
|
|
2722
|
+
finally:
|
|
2723
|
+
gc.set_debug(old_flags)
|
|
2724
|
+
gc.garbage.clear()
|
|
2725
|
+
|
|
2726
|
+
|
|
2727
|
+
@pytest.mark.skipif(
|
|
2728
|
+
sys.implementation.name != "cpython",
|
|
2729
|
+
reason="Only makes sense with refcounting GC",
|
|
2730
|
+
)
|
|
2731
|
+
async def test_locals_destroyed_promptly_on_cancel() -> None:
|
|
2732
|
+
destroyed = False
|
|
2733
|
+
|
|
2734
|
+
def finalizer() -> None:
|
|
2735
|
+
nonlocal destroyed
|
|
2736
|
+
destroyed = True
|
|
2737
|
+
|
|
2738
|
+
class A:
|
|
2739
|
+
pass
|
|
2740
|
+
|
|
2741
|
+
async def task() -> None:
|
|
2742
|
+
a = A()
|
|
2743
|
+
weakref.finalize(a, finalizer)
|
|
2744
|
+
await _core.checkpoint()
|
|
2745
|
+
|
|
2746
|
+
async with _core.open_nursery() as nursery:
|
|
2747
|
+
nursery.start_soon(task)
|
|
2748
|
+
nursery.cancel_scope.cancel()
|
|
2749
|
+
assert destroyed
|
|
2750
|
+
|
|
2751
|
+
|
|
2752
|
+
def _create_kwargs(strictness: bool | None) -> dict[str, bool]:
|
|
2753
|
+
"""Turn a bool|None into a kwarg dict that can be passed to `run` or `open_nursery`"""
|
|
2754
|
+
|
|
2755
|
+
if strictness is None:
|
|
2756
|
+
return {}
|
|
2757
|
+
return {"strict_exception_groups": strictness}
|
|
2758
|
+
|
|
2759
|
+
|
|
2760
|
+
@pytest.mark.filterwarnings(
|
|
2761
|
+
"ignore:.*strict_exception_groups=False:trio.TrioDeprecationWarning",
|
|
2762
|
+
)
|
|
2763
|
+
@pytest.mark.parametrize("run_strict", [True, False, None])
|
|
2764
|
+
@pytest.mark.parametrize("open_nursery_strict", [True, False, None])
|
|
2765
|
+
@pytest.mark.parametrize("multiple_exceptions", [True, False])
|
|
2766
|
+
def test_setting_strict_exception_groups(
|
|
2767
|
+
run_strict: bool | None,
|
|
2768
|
+
open_nursery_strict: bool | None,
|
|
2769
|
+
multiple_exceptions: bool,
|
|
2770
|
+
) -> None:
|
|
2771
|
+
"""
|
|
2772
|
+
Test default values and that nurseries can both inherit and override the global context
|
|
2773
|
+
setting of strict_exception_groups.
|
|
2774
|
+
"""
|
|
2775
|
+
|
|
2776
|
+
async def raise_error() -> NoReturn:
|
|
2777
|
+
raise RuntimeError("test error")
|
|
2778
|
+
|
|
2779
|
+
async def main() -> None:
|
|
2780
|
+
"""Open a nursery, and raise one or two errors inside"""
|
|
2781
|
+
async with _core.open_nursery(**_create_kwargs(open_nursery_strict)) as nursery:
|
|
2782
|
+
nursery.start_soon(raise_error)
|
|
2783
|
+
if multiple_exceptions:
|
|
2784
|
+
nursery.start_soon(raise_error)
|
|
2785
|
+
|
|
2786
|
+
def run_main() -> None:
|
|
2787
|
+
# mypy doesn't like kwarg magic
|
|
2788
|
+
_core.run(main, **_create_kwargs(run_strict)) # type: ignore[arg-type]
|
|
2789
|
+
|
|
2790
|
+
matcher = pytest.RaisesExc(RuntimeError, match=r"^test error$")
|
|
2791
|
+
|
|
2792
|
+
if multiple_exceptions:
|
|
2793
|
+
with pytest.RaisesGroup(matcher, matcher):
|
|
2794
|
+
run_main()
|
|
2795
|
+
elif open_nursery_strict or (
|
|
2796
|
+
open_nursery_strict is None and run_strict is not False
|
|
2797
|
+
):
|
|
2798
|
+
with pytest.RaisesGroup(matcher):
|
|
2799
|
+
run_main()
|
|
2800
|
+
else:
|
|
2801
|
+
with pytest.raises(RuntimeError, match=r"^test error$"):
|
|
2802
|
+
run_main()
|
|
2803
|
+
|
|
2804
|
+
|
|
2805
|
+
@pytest.mark.filterwarnings(
|
|
2806
|
+
"ignore:.*strict_exception_groups=False:trio.TrioDeprecationWarning",
|
|
2807
|
+
)
|
|
2808
|
+
@pytest.mark.parametrize("strict", [True, False, None])
|
|
2809
|
+
async def test_nursery_collapse(strict: bool | None) -> None:
|
|
2810
|
+
"""
|
|
2811
|
+
Test that a single exception from a nested nursery gets collapsed correctly
|
|
2812
|
+
depending on strict_exception_groups value when CancelledErrors are stripped from it.
|
|
2813
|
+
"""
|
|
2814
|
+
|
|
2815
|
+
async def raise_error() -> NoReturn:
|
|
2816
|
+
raise RuntimeError("test error")
|
|
2817
|
+
|
|
2818
|
+
# mypy requires explicit type for conditional expression
|
|
2819
|
+
maybe_wrapped_runtime_error: (
|
|
2820
|
+
type[RuntimeError] | pytest.RaisesGroup[RuntimeError]
|
|
2821
|
+
) = (RuntimeError if strict is False else pytest.RaisesGroup(RuntimeError))
|
|
2822
|
+
|
|
2823
|
+
with pytest.RaisesGroup(RuntimeError, maybe_wrapped_runtime_error):
|
|
2824
|
+
async with _core.open_nursery() as nursery:
|
|
2825
|
+
nursery.start_soon(sleep_forever)
|
|
2826
|
+
nursery.start_soon(raise_error)
|
|
2827
|
+
async with _core.open_nursery(**_create_kwargs(strict)) as nursery2:
|
|
2828
|
+
nursery2.start_soon(sleep_forever)
|
|
2829
|
+
nursery2.start_soon(raise_error)
|
|
2830
|
+
nursery.cancel_scope.cancel()
|
|
2831
|
+
|
|
2832
|
+
|
|
2833
|
+
async def test_cancel_scope_no_cancellederror() -> None:
|
|
2834
|
+
"""
|
|
2835
|
+
Test that when a cancel scope encounters an exception group that does NOT contain
|
|
2836
|
+
a Cancelled exception, it will NOT set the ``cancelled_caught`` flag.
|
|
2837
|
+
"""
|
|
2838
|
+
|
|
2839
|
+
with pytest.RaisesGroup(RuntimeError, RuntimeError, match="test"):
|
|
2840
|
+
with _core.CancelScope() as scope:
|
|
2841
|
+
scope.cancel()
|
|
2842
|
+
raise ExceptionGroup("test", [RuntimeError(), RuntimeError()])
|
|
2843
|
+
|
|
2844
|
+
assert not scope.cancelled_caught
|
|
2845
|
+
|
|
2846
|
+
|
|
2847
|
+
@pytest.mark.filterwarnings(
|
|
2848
|
+
"ignore:.*strict_exception_groups=False:trio.TrioDeprecationWarning",
|
|
2849
|
+
)
|
|
2850
|
+
@pytest.mark.parametrize("run_strict", [False, True])
|
|
2851
|
+
@pytest.mark.parametrize("start_raiser_strict", [False, True, None])
|
|
2852
|
+
@pytest.mark.parametrize("raise_after_started", [False, True])
|
|
2853
|
+
@pytest.mark.parametrize("raise_custom_exc_grp", [False, True])
|
|
2854
|
+
def test_trio_run_strict_before_started(
|
|
2855
|
+
run_strict: bool,
|
|
2856
|
+
start_raiser_strict: bool | None,
|
|
2857
|
+
raise_after_started: bool,
|
|
2858
|
+
raise_custom_exc_grp: bool,
|
|
2859
|
+
) -> None:
|
|
2860
|
+
"""
|
|
2861
|
+
Regression tests for #2611, where exceptions raised before
|
|
2862
|
+
`TaskStatus.started()` caused `Nursery.start()` to wrap them in an
|
|
2863
|
+
ExceptionGroup when using `run(..., strict_exception_groups=True)`.
|
|
2864
|
+
|
|
2865
|
+
Regression tests for #2844, where #2611 was initially fixed in a way that
|
|
2866
|
+
had unintended side effects.
|
|
2867
|
+
"""
|
|
2868
|
+
|
|
2869
|
+
raiser_exc: ValueError | ExceptionGroup[ValueError]
|
|
2870
|
+
if raise_custom_exc_grp:
|
|
2871
|
+
raiser_exc = ExceptionGroup("my group", [ValueError()])
|
|
2872
|
+
else:
|
|
2873
|
+
raiser_exc = ValueError()
|
|
2874
|
+
|
|
2875
|
+
async def raiser(*, task_status: _core.TaskStatus[None]) -> None:
|
|
2876
|
+
if raise_after_started:
|
|
2877
|
+
task_status.started()
|
|
2878
|
+
raise raiser_exc
|
|
2879
|
+
|
|
2880
|
+
async def start_raiser() -> None:
|
|
2881
|
+
try:
|
|
2882
|
+
async with _core.open_nursery(
|
|
2883
|
+
strict_exception_groups=start_raiser_strict,
|
|
2884
|
+
) as nursery:
|
|
2885
|
+
await nursery.start(raiser)
|
|
2886
|
+
except BaseExceptionGroup as exc_group:
|
|
2887
|
+
if start_raiser_strict:
|
|
2888
|
+
# Iff the code using the nursery *forced* it to be strict
|
|
2889
|
+
# (overriding the runner setting) then it may replace the bland
|
|
2890
|
+
# exception group raised by trio with a more specific one (subtype,
|
|
2891
|
+
# different message, etc.).
|
|
2892
|
+
raise BaseExceptionGroup(
|
|
2893
|
+
"start_raiser nursery custom message",
|
|
2894
|
+
exc_group.exceptions,
|
|
2895
|
+
) from None
|
|
2896
|
+
raise
|
|
2897
|
+
|
|
2898
|
+
with pytest.raises(BaseException) as exc_info: # noqa: PT011 # no `match`
|
|
2899
|
+
_core.run(start_raiser, strict_exception_groups=run_strict)
|
|
2900
|
+
|
|
2901
|
+
if start_raiser_strict or (run_strict and start_raiser_strict is None):
|
|
2902
|
+
# start_raiser's nursery was strict.
|
|
2903
|
+
assert isinstance(exc_info.value, BaseExceptionGroup)
|
|
2904
|
+
if start_raiser_strict:
|
|
2905
|
+
# start_raiser didn't unknowingly inherit its nursery strictness
|
|
2906
|
+
# from `run`---it explicitly chose for its nursery to be strict.
|
|
2907
|
+
assert exc_info.value.message == "start_raiser nursery custom message"
|
|
2908
|
+
assert len(exc_info.value.exceptions) == 1
|
|
2909
|
+
should_be_raiser_exc = exc_info.value.exceptions[0]
|
|
2910
|
+
else:
|
|
2911
|
+
# start_raiser's nursery was not strict.
|
|
2912
|
+
should_be_raiser_exc = exc_info.value
|
|
2913
|
+
if isinstance(raiser_exc, ValueError):
|
|
2914
|
+
assert should_be_raiser_exc is raiser_exc
|
|
2915
|
+
else:
|
|
2916
|
+
# Check attributes, not identity, because should_be_raiser_exc may be a
|
|
2917
|
+
# copy of raiser_exc rather than raiser_exc by identity.
|
|
2918
|
+
assert type(should_be_raiser_exc) is type(raiser_exc)
|
|
2919
|
+
assert should_be_raiser_exc.message == raiser_exc.message
|
|
2920
|
+
assert should_be_raiser_exc.exceptions == raiser_exc.exceptions
|
|
2921
|
+
|
|
2922
|
+
|
|
2923
|
+
async def test_internal_error_old_nursery_multiple_tasks() -> None:
|
|
2924
|
+
async def error_func() -> None:
|
|
2925
|
+
raise ValueError
|
|
2926
|
+
|
|
2927
|
+
async def spawn_tasks_in_old_nursery(task_status: _core.TaskStatus[None]) -> None:
|
|
2928
|
+
old_nursery = _core.current_task().parent_nursery
|
|
2929
|
+
assert old_nursery is not None
|
|
2930
|
+
old_nursery.start_soon(error_func)
|
|
2931
|
+
old_nursery.start_soon(error_func)
|
|
2932
|
+
|
|
2933
|
+
async with _core.open_nursery() as nursery:
|
|
2934
|
+
with pytest.raises(_core.TrioInternalError) as excinfo:
|
|
2935
|
+
await nursery.start(spawn_tasks_in_old_nursery)
|
|
2936
|
+
assert pytest.RaisesGroup(ValueError, ValueError).matches(excinfo.value.__cause__)
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
if sys.version_info >= (3, 11):
|
|
2940
|
+
|
|
2941
|
+
def no_other_refs() -> list[object]:
|
|
2942
|
+
return []
|
|
2943
|
+
|
|
2944
|
+
else:
|
|
2945
|
+
|
|
2946
|
+
def no_other_refs() -> list[object]:
|
|
2947
|
+
return [sys._getframe(1)]
|
|
2948
|
+
|
|
2949
|
+
|
|
2950
|
+
@pytest.mark.skipif(
|
|
2951
|
+
sys.implementation.name != "cpython",
|
|
2952
|
+
reason="Only makes sense with refcounting GC",
|
|
2953
|
+
)
|
|
2954
|
+
@pytest.mark.xfail(
|
|
2955
|
+
sys.version_info >= (3, 14),
|
|
2956
|
+
reason="https://github.com/python/cpython/issues/125603",
|
|
2957
|
+
)
|
|
2958
|
+
async def test_ki_protection_doesnt_leave_cyclic_garbage() -> None:
|
|
2959
|
+
class MyException(Exception):
|
|
2960
|
+
pass
|
|
2961
|
+
|
|
2962
|
+
async def demo() -> None:
|
|
2963
|
+
async def handle_error() -> None:
|
|
2964
|
+
try:
|
|
2965
|
+
raise MyException
|
|
2966
|
+
except MyException as e:
|
|
2967
|
+
exceptions.append(e)
|
|
2968
|
+
|
|
2969
|
+
exceptions: list[MyException] = []
|
|
2970
|
+
try:
|
|
2971
|
+
async with _core.open_nursery() as n:
|
|
2972
|
+
n.start_soon(handle_error)
|
|
2973
|
+
raise ExceptionGroup("errors", exceptions)
|
|
2974
|
+
finally:
|
|
2975
|
+
exceptions = []
|
|
2976
|
+
|
|
2977
|
+
exc: Exception | None = None
|
|
2978
|
+
try:
|
|
2979
|
+
await demo()
|
|
2980
|
+
except ExceptionGroup as excs:
|
|
2981
|
+
exc = excs.exceptions[0]
|
|
2982
|
+
|
|
2983
|
+
assert isinstance(exc, MyException)
|
|
2984
|
+
assert gc.get_referrers(exc) == no_other_refs()
|
|
2985
|
+
|
|
2986
|
+
|
|
2987
|
+
def test_context_run_tb_frames() -> None:
|
|
2988
|
+
class Context:
|
|
2989
|
+
def run(self, fn: Callable[[], object]) -> object:
|
|
2990
|
+
return fn()
|
|
2991
|
+
|
|
2992
|
+
with mock.patch("trio._core._run.copy_context", return_value=Context()):
|
|
2993
|
+
assert _count_context_run_tb_frames() == 1
|
|
2994
|
+
|
|
2995
|
+
|
|
2996
|
+
@restore_unraisablehook()
|
|
2997
|
+
def test_trio_context_detection() -> None:
|
|
2998
|
+
assert not _core.in_trio_run()
|
|
2999
|
+
assert not _core.in_trio_task()
|
|
3000
|
+
|
|
3001
|
+
def inner() -> None:
|
|
3002
|
+
assert _core.in_trio_run()
|
|
3003
|
+
assert _core.in_trio_task()
|
|
3004
|
+
|
|
3005
|
+
def sync_inner() -> None:
|
|
3006
|
+
assert not _core.in_trio_run()
|
|
3007
|
+
assert not _core.in_trio_task()
|
|
3008
|
+
|
|
3009
|
+
def inner_abort(_: object) -> _core.Abort:
|
|
3010
|
+
assert _core.in_trio_run()
|
|
3011
|
+
assert _core.in_trio_task()
|
|
3012
|
+
return _core.Abort.SUCCEEDED
|
|
3013
|
+
|
|
3014
|
+
async def main() -> None:
|
|
3015
|
+
assert _core.in_trio_run()
|
|
3016
|
+
assert _core.in_trio_task()
|
|
3017
|
+
|
|
3018
|
+
inner()
|
|
3019
|
+
|
|
3020
|
+
await to_thread_run_sync(sync_inner)
|
|
3021
|
+
with _core.CancelScope(deadline=_core.current_time() - 1):
|
|
3022
|
+
await _core.wait_task_rescheduled(inner_abort)
|
|
3023
|
+
|
|
3024
|
+
_core.run(main)
|