Skip to content

Commit

Permalink
Merge pull request #139 from oremanj/fix-defaultexc-error
Browse files Browse the repository at this point in the history
Fix spew when event loop exception handler is invoked after loop closure
  • Loading branch information
oremanj authored Jan 30, 2024
2 parents bbcf7a0 + 8fb0645 commit 4268ffa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions newsfragments/134.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix an issue where a call to ``TrioEventLoop.call_exception_handler()`` after
the loop was closed would attempt to call a method on ``None``. This pattern
can be encountered if an ``aiohttp`` session is garbage-collected without being
properly closed, for example.
9 changes: 9 additions & 0 deletions tests/test_trio_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ async def test_get_running_loop():
assert asyncio.get_running_loop() == loop


@pytest.mark.trio
async def test_exception_after_closed(caplog):
async with trio_asyncio.open_loop() as loop:
pass
loop.call_exception_handler({"message": "Test exception after loop closed"})
assert len(caplog.records) == 1
assert caplog.records[0].message == "Test exception after loop closed"


@pytest.mark.trio
async def test_tasks_get_cancelled():
record = []
Expand Down
7 changes: 7 additions & 0 deletions trio_asyncio/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def default_exception_handler(self, context):
# Call the original default handler so we get the full info in the log
super().default_exception_handler(context)

if self._nursery is None:
# Event loop is already closed; don't do anything further.
# Some asyncio libraries call the asyncio exception handler
# from their __del__ methods, e.g., aiohttp for "Unclosed
# client session".
return

# Also raise an exception so it can't go unnoticed
exception = context.get("exception")
if exception is None:
Expand Down

0 comments on commit 4268ffa

Please sign in to comment.