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

Avoid logging exception from run_app() that is also raised #8951

Merged
merged 4 commits into from
Sep 1, 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
1 change: 1 addition & 0 deletions CHANGES/6807.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stopped logging exceptions from ``web.run_app()`` that would be raised regardless -- by :user:`Dreamsorcerer`.
15 changes: 10 additions & 5 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from argparse import ArgumentParser
from collections.abc import Iterable
from contextlib import suppress
from importlib import import_module
from typing import (
Any,
Expand Down Expand Up @@ -501,11 +502,15 @@ def run_app(
except (GracefulExit, KeyboardInterrupt): # pragma: no cover
pass
finally:
_cancel_tasks({main_task}, loop)
_cancel_tasks(asyncio.all_tasks(loop), loop)
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
asyncio.set_event_loop(None)
try:
main_task.cancel()
with suppress(asyncio.CancelledError):
bdraco marked this conversation as resolved.
Show resolved Hide resolved
loop.run_until_complete(main_task)
finally:
_cancel_tasks(asyncio.all_tasks(loop), loop)
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
asyncio.set_event_loop(None)


def main(argv: List[str]) -> None:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,23 @@ async def init() -> web.Application:
assert count == 3


def test_run_app_raises_exception(patched_loop: asyncio.AbstractEventLoop) -> None:
async def context(app: web.Application) -> AsyncIterator[None]:
raise RuntimeError("foo")
yield # pragma: no cover

app = web.Application()
app.cleanup_ctx.append(context)

with mock.patch.object(
patched_loop, "call_exception_handler", autospec=True, spec_set=True
) as m:
with pytest.raises(RuntimeError, match="foo"):
web.run_app(app, loop=patched_loop)

assert not m.called


class TestShutdown:
def raiser(self) -> NoReturn:
raise KeyboardInterrupt
Expand Down
Loading