Skip to content

Commit

Permalink
Merge pull request #149 from oremanj/system-exit
Browse files Browse the repository at this point in the history
Fix spurious error when asyncio.run() task raises SystemExit
  • Loading branch information
axiomiety authored Apr 23, 2024
2 parents 4ac0555 + d7987f1 commit 6f2870a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions newsfragments/149.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trio-asyncio no longer raises a spurious "Event loop stopped before Future
completed!" exception if a function passed to :func:`asyncio.run` calls
:func:`sys.exit`.
10 changes: 10 additions & 0 deletions tests/test_trio_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,13 @@ def noop():
await trio_asyncio.aio_as_trio(loop.run_in_executor)(executor, noop)

assert not scope.cancelled_caught


def test_system_exit():
async def main():
raise SystemExit(42)

with pytest.raises(SystemExit) as scope:
asyncio.run(main())

assert scope.value.code == 42
8 changes: 8 additions & 0 deletions trio_asyncio/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ def is_done(_):
nonlocal result

result = outcome.capture(future.result)
if isinstance(result, outcome.Error) and isinstance(
result.error, (SystemExit, KeyboardInterrupt)
):
# These exceptions propagate out of the event loop;
# don't stop the event loop again, or else it will
# interfere with cleanup actions like
# run_until_complete(shutdown_asyncgens)
return
self.stop()

future.add_done_callback(is_done)
Expand Down

0 comments on commit 6f2870a

Please sign in to comment.