Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Raise startup failure outside nursery #397

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _trio_parallel_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def worker_behavior(recv_pipe, send_pipe, idle_timeout, init, retire):
# between processes. (Trio will take charge via cancellation.)
signal.signal(signal.SIGINT, signal.SIG_IGN)
try:
if isinstance(init, bytes): # true except on "fork"
# Signal successful startup to spawn/forkserver parents.
if sys.platform == "win32":
# Signal successful startup.
send_pipe.send_bytes(ACK)
if isinstance(init, bytes): # true except on "fork"
init = loads(init)
if isinstance(retire, bytes): # true except on "fork"
retire = loads(retire)
init()
while safe_poll(recv_pipe, idle_timeout):
Expand Down
20 changes: 13 additions & 7 deletions trio_parallel/_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,29 @@ async def start(self):
self._child_send_pipe.close()
self._child_recv_pipe.close()

# The following is mainly needed in the case of accidental recursive spawn
async def wait_then_fail():
await self.wait()
raise BrokenWorkerProcessError("Worker failed to start", self.proc)
if sys.platform != "win32":
return

async with trio.open_nursery() as nursery:
nursery.start_soon(wait_then_fail)
# Give a nice error on accidental recursive spawn instead of hanging
async def wait_for_ack():
try:
code = await self._receive_chan.receive()
assert code == tp_workers.ACK
except BaseException:
self.kill()
with trio.CancelScope(shield=True):
await self.wait() # noqa: TRIO102
raise
assert code == tp_workers.ACK
nursery.cancel_scope.cancel()

exitcode = None
async with trio.open_nursery() as nursery:
nursery.start_soon(wait_for_ack)
exitcode = await self.wait()
nursery.cancel_scope.cancel()
if exitcode is not None:
raise BrokenWorkerProcessError("Worker failed to start", self.proc)

async def run_sync(self, sync_fn: Callable, *args) -> Optional[Outcome]:
try:
job = dumps((sync_fn, args), protocol=HIGHEST_PROTOCOL)
Expand Down
2 changes: 2 additions & 0 deletions trio_parallel/_tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def test_startup_failure_doesnt_hang(tmp_path):
)
assert not result.stdout
assert b"An attempt has been made to start a new process" in result.stderr
assert b"ExceptionGroup" not in result.stderr
assert b"MultiError" not in result.stderr
assert result.returncode


Expand Down
Loading