From 9d5a8a9643c7af4e4a48b7b4b041a24fb92a21ad Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Fri, 6 Nov 2026 20:07:22 +0000 Subject: [PATCH] Raise a failed task's error even if the scope's block catches it When a task spawned in a scope fails, its error is thrown into the scope's main task, wherever the block is currently awaiting. If the block catches it, e.g. with a broad `except Exception:` around an await, and then exits normally, new_scope finished the remaining tasks and returned normally, so the failure was silently lost: async with duet.new_scope() as scope: scope.spawn(failing_task) try: await something() except Exception: log_and_continue() # Previously reached without an error. Record the first error of a failed task in the Scope. If the block exits normally, raise it when the block exits, before waiting for the remaining tasks, so that they are interrupted with ScopeExit as for any other error. If the block raises an error of its own, that error is raised as before. Only Exceptions are recorded. A BaseException such as KeyboardInterrupt that the block deliberately handled is not raised again. This matches the structured-concurrency guarantee that a failure of a spawned task is never lost. Behaviour change: a block that deliberately catches a spawned task's error and carries on now still raises that error at the end of the block; to handle a task's errors, catch them inside the task. --- duet/api.py | 16 +++++++- duet/api_test.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/duet/api.py b/duet/api.py index 900a9cc..5b3b61d 100644 --- a/duet/api.py +++ b/duet/api.py @@ -322,7 +322,10 @@ async def new_scope( spawned tasks, all other background tasks will be interrupted and the block will raise an error. Background tasks are interrupted by raising `ScopeExit`, which is a `BaseException` and should not be swallowed, similar to other - base exceptions like `KeyboardInterrupt`. + base exceptions like `KeyboardInterrupt`. The error of a failed background + task is raised in the block itself; if the block catches it and exits + normally, the error is raised again when the block exits, so that it is + not lost. Args: deadline: Absolute time in epoch seconds when the scope should exit. @@ -351,6 +354,12 @@ async def finish_tasks(): main_task.push_deadline(deadline, scope._timeout_error) try: yield scope + if scope._error is not None: + # A spawned task failed but the block exited normally, e.g. because + # it caught the error that was raised in it. Don't lose the error; + # raising it here also interrupts the remaining tasks below. + error, scope._error = scope._error, None + raise error if scope._spawned: await finish_tasks() except BaseException: @@ -380,6 +389,7 @@ def __init__( self._tasks = tasks self._spawned = False self._timeout_error = TimeoutError() + self._error: Optional[Exception] = None def cancel(self) -> None: self._main_task.interrupt(CancelledError()) @@ -394,6 +404,10 @@ async def _run(self, func: Callable[..., Awaitable[Any]], *args, **kwds) -> None task = impl.current_task() try: await func(*args, **kwds) + except Exception as e: + if self._error is None: + self._error = e + raise finally: self._tasks.discard(task) diff --git a/duet/api_test.py b/duet/api_test.py index 84d3f03..98e1fe7 100644 --- a/duet/api_test.py +++ b/duet/api_test.py @@ -607,6 +607,102 @@ async def func(i: int) -> None: assert len(caught) == 1 assert isinstance(caught[0], duet.ScopeExit) + @pytest.mark.parametrize("wait", ["future", "nested_scope"]) + def test_task_error_raised_if_caught_in_block(self, wait: str) -> None: + """A failed task's error is raised even if the block catches it.""" + caught: List[BaseException] = [] + + async def fail() -> None: + await duet.completed_future(None) + raise Fail() + + async def main() -> None: + async with duet.new_scope() as scope: + scope.spawn(fail) + try: + if wait == "future": + await duet.AwaitableFuture() # Never completes. + else: + await duet.sleep(10) + except Exception as e: + caught.append(e) + + with pytest.raises(Fail) as exc_info: + duet.run(main) + assert len(caught) == 1 + assert caught[0] is exc_info.value + + def test_block_error_takes_precedence_over_caught_task_error(self) -> None: + """If the block catches a task's error and raises another, that one is raised.""" + + async def fail() -> None: + await duet.completed_future(None) + raise Fail() + + async def main() -> None: + async with duet.new_scope() as scope: + scope.spawn(fail) + try: + await duet.AwaitableFuture() # Never completes. + except Fail as e: + raise ValueError("handled") from e + + with pytest.raises(ValueError, match="handled") as exc_info: + duet.run(main) + assert isinstance(exc_info.value.__cause__, Fail) + + def test_caught_task_error_interrupts_other_tasks(self) -> None: + """If the block catches a task's error, the scope still interrupts its other tasks.""" + caught: List[BaseException] = [] + + async def fail() -> None: + await duet.completed_future(None) + raise Fail() + + async def sleep() -> None: + try: + await duet.sleep(1) + except BaseException as e: + caught.append(e) + raise + + async def main() -> None: + async with duet.new_scope() as scope: + scope.spawn(fail) + scope.spawn(sleep) + try: + await duet.AwaitableFuture() # Never completes. + except Exception: + pass + + start = time.time() + with pytest.raises(Fail): + duet.run(main) + assert [type(e) for e in caught] == [duet.ScopeExit] + assert time.time() - start < 0.5 + + def test_caught_keyboard_interrupt_from_task_not_reraised(self) -> None: + """Only Exceptions are re-raised; a BaseException that the block handled is not.""" + + async def interrupt() -> None: + await duet.completed_future(None) + raise KeyboardInterrupt() + + async def main() -> str: + async with duet.new_scope() as scope: + scope.spawn(interrupt) + try: + await duet.AwaitableFuture() # Never completes. + except KeyboardInterrupt: + pass + return "done" + + try: + result = duet.run(main) + except KeyboardInterrupt: + pytest.fail("the handled KeyboardInterrupt was re-raised") + assert result == "done" + @duet.sync async def test_timeout(self): future = duet.AwaitableFuture()