-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Use a new loop in run_app() #5572
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
99d1c7f
Add test for run_app() loop error.
Dreamsorcerer c3e8c3c
Non-async test
Dreamsorcerer 2049e21
Use new_event_loop()
Dreamsorcerer 13ae734
Create 5572.bugfix
Dreamsorcerer 0c79ed3
Update 5572.bugfix
Dreamsorcerer f7ee104
Move set_event_loop() into try block (to match asyncio.run())
Dreamsorcerer dce79e0
Update CHANGES/5572.bugfix
Dreamsorcerer a2aa0a9
Move create_task() before try.
Dreamsorcerer 870ddda
Add explicit loop parameter.
Dreamsorcerer 1d89276
Use patched loop in run_app()
Dreamsorcerer 5bdc309
Black
Dreamsorcerer a5dd9b8
Commas
Dreamsorcerer 03dffb7
Merge branch 'master' into Dreamsorcerer-patch-3
Dreamsorcerer 0621e30
Update and rename 5572.bugfix to 5572.feature
Dreamsorcerer add49b0
Update tests/test_web_runner.py
Dreamsorcerer 84f2a13
Update test_web_runner.py
Dreamsorcerer c9f1536
Update test_web_runner.py
Dreamsorcerer cdf1835
Update test_web_runner.py
Dreamsorcerer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Always create a new event loop in ``aiohttp.web.run_app()``. | ||
This adds better compatibility with ``asyncio.run()`` or if trying to run multiple apps in sequence. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ def test_run_app_http(patched_loop: Any) -> None: | |
cleanup_handler = make_mocked_coro() | ||
app.on_cleanup.append(cleanup_handler) | ||
|
||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, None, 8080, ssl=None, backlog=128, reuse_address=None, reuse_port=None | ||
|
@@ -105,7 +105,7 @@ def test_run_app_http(patched_loop: Any) -> None: | |
|
||
def test_run_app_close_loop(patched_loop: Any) -> None: | ||
app = web.Application() | ||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, None, 8080, ssl=None, backlog=128, reuse_address=None, reuse_port=None | ||
|
@@ -428,7 +428,7 @@ def test_run_app_mixed_bindings( | |
patched_loop: Any, | ||
) -> None: | ||
app = web.Application() | ||
web.run_app(app, print=stopper(patched_loop), **run_app_kwargs) | ||
web.run_app(app, print=stopper(patched_loop), **run_app_kwargs, loop=patched_loop) | ||
|
||
assert patched_loop.create_unix_server.mock_calls == expected_unix_server_calls | ||
assert patched_loop.create_server.mock_calls == expected_server_calls | ||
|
@@ -438,7 +438,9 @@ def test_run_app_https(patched_loop: Any) -> None: | |
app = web.Application() | ||
|
||
ssl_context = ssl.create_default_context() | ||
web.run_app(app, ssl_context=ssl_context, print=stopper(patched_loop)) | ||
web.run_app( | ||
app, ssl_context=ssl_context, print=stopper(patched_loop), loop=patched_loop | ||
) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, | ||
|
@@ -458,7 +460,9 @@ def test_run_app_nondefault_host_port( | |
host = "127.0.0.1" | ||
|
||
app = web.Application() | ||
web.run_app(app, host=host, port=port, print=stopper(patched_loop)) | ||
web.run_app( | ||
app, host=host, port=port, print=stopper(patched_loop), loop=patched_loop | ||
) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, host, port, ssl=None, backlog=128, reuse_address=None, reuse_port=None | ||
|
@@ -469,7 +473,7 @@ def test_run_app_multiple_hosts(patched_loop: Any) -> None: | |
hosts = ("127.0.0.1", "127.0.0.2") | ||
|
||
app = web.Application() | ||
web.run_app(app, host=hosts, print=stopper(patched_loop)) | ||
web.run_app(app, host=hosts, print=stopper(patched_loop), loop=patched_loop) | ||
|
||
calls = map( | ||
lambda h: mock.call( | ||
|
@@ -488,7 +492,7 @@ def test_run_app_multiple_hosts(patched_loop: Any) -> None: | |
|
||
def test_run_app_custom_backlog(patched_loop: Any) -> None: | ||
app = web.Application() | ||
web.run_app(app, backlog=10, print=stopper(patched_loop)) | ||
web.run_app(app, backlog=10, print=stopper(patched_loop), loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, None, 8080, ssl=None, backlog=10, reuse_address=None, reuse_port=None | ||
|
@@ -497,7 +501,13 @@ def test_run_app_custom_backlog(patched_loop: Any) -> None: | |
|
||
def test_run_app_custom_backlog_unix(patched_loop: Any) -> None: | ||
app = web.Application() | ||
web.run_app(app, path="/tmp/tmpsock.sock", backlog=10, print=stopper(patched_loop)) | ||
web.run_app( | ||
app, | ||
path="/tmp/tmpsock.sock", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note: ouch, this looks awfully hardcoded. It's probably a good idea to fix such hardcoded socket paths across tests (in a dedicated PR, of course). |
||
backlog=10, | ||
print=stopper(patched_loop), | ||
loop=patched_loop, | ||
) | ||
|
||
patched_loop.create_unix_server.assert_called_with( | ||
mock.ANY, "/tmp/tmpsock.sock", ssl=None, backlog=10 | ||
|
@@ -510,7 +520,7 @@ def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None: | |
|
||
sock_path = str(tmp_path / "socket.sock") | ||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, path=sock_path, print=printer) | ||
web.run_app(app, path=sock_path, print=printer, loop=patched_loop) | ||
|
||
patched_loop.create_unix_server.assert_called_with( | ||
mock.ANY, sock_path, ssl=None, backlog=128 | ||
|
@@ -525,7 +535,9 @@ def test_run_app_https_unix_socket(patched_loop: Any, tmp_path: Any) -> None: | |
sock_path = str(tmp_path / "socket.sock") | ||
ssl_context = ssl.create_default_context() | ||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, path=sock_path, ssl_context=ssl_context, print=printer) | ||
web.run_app( | ||
app, path=sock_path, ssl_context=ssl_context, print=printer, loop=patched_loop | ||
) | ||
|
||
patched_loop.create_unix_server.assert_called_with( | ||
mock.ANY, sock_path, ssl=ssl_context, backlog=128 | ||
|
@@ -539,7 +551,10 @@ def test_run_app_abstract_linux_socket(patched_loop: Any) -> None: | |
sock_path = b"\x00" + uuid4().hex.encode("ascii") | ||
app = web.Application() | ||
web.run_app( | ||
app, path=sock_path.decode("ascii", "ignore"), print=stopper(patched_loop) | ||
app, | ||
path=sock_path.decode("ascii", "ignore"), | ||
print=stopper(patched_loop), | ||
loop=patched_loop, | ||
) | ||
|
||
patched_loop.create_unix_server.assert_called_with( | ||
|
@@ -556,7 +571,7 @@ def test_run_app_preexisting_inet_socket(patched_loop: Any, mocker: Any) -> None | |
_, port = sock.getsockname() | ||
|
||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, sock=sock, print=printer) | ||
web.run_app(app, sock=sock, print=printer, loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, sock=sock, backlog=128, ssl=None | ||
|
@@ -574,7 +589,7 @@ def test_run_app_preexisting_inet6_socket(patched_loop: Any) -> None: | |
port = sock.getsockname()[1] | ||
|
||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, sock=sock, print=printer) | ||
web.run_app(app, sock=sock, print=printer, loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, sock=sock, backlog=128, ssl=None | ||
|
@@ -593,7 +608,7 @@ def test_run_app_preexisting_unix_socket(patched_loop: Any, mocker: Any) -> None | |
os.unlink(sock_path) | ||
|
||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, sock=sock, print=printer) | ||
web.run_app(app, sock=sock, print=printer, loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, sock=sock, backlog=128, ssl=None | ||
|
@@ -613,7 +628,7 @@ def test_run_app_multiple_preexisting_sockets(patched_loop: Any) -> None: | |
_, port2 = sock2.getsockname() | ||
|
||
printer = mock.Mock(wraps=stopper(patched_loop)) | ||
web.run_app(app, sock=(sock1, sock2), print=printer) | ||
web.run_app(app, sock=(sock1, sock2), print=printer, loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_has_calls( | ||
[ | ||
|
@@ -671,7 +686,7 @@ def test_startup_cleanup_signals_even_on_failure(patched_loop: Any) -> None: | |
app.on_cleanup.append(cleanup_handler) | ||
|
||
with pytest.raises(RuntimeError): | ||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
|
||
startup_handler.assert_called_once_with(app) | ||
cleanup_handler.assert_called_once_with(app) | ||
|
@@ -689,7 +704,7 @@ async def make_app(): | |
app.on_cleanup.append(cleanup_handler) | ||
return app | ||
|
||
web.run_app(make_app(), print=stopper(patched_loop)) | ||
web.run_app(make_app(), print=stopper(patched_loop), loop=patched_loop) | ||
|
||
patched_loop.create_server.assert_called_with( | ||
mock.ANY, None, 8080, ssl=None, backlog=128, reuse_address=None, reuse_port=None | ||
|
@@ -709,7 +724,13 @@ def test_run_app_default_logger(monkeypatch: Any, patched_loop: Any) -> None: | |
mock_logger.configure_mock(**attrs) | ||
|
||
app = web.Application() | ||
web.run_app(app, debug=True, print=stopper(patched_loop), access_log=mock_logger) | ||
web.run_app( | ||
app, | ||
debug=True, | ||
print=stopper(patched_loop), | ||
access_log=mock_logger, | ||
loop=patched_loop, | ||
) | ||
mock_logger.setLevel.assert_any_call(logging.DEBUG) | ||
mock_logger.hasHandlers.assert_called_with() | ||
assert isinstance(mock_logger.addHandler.call_args[0][0], logging.StreamHandler) | ||
|
@@ -726,7 +747,13 @@ def test_run_app_default_logger_setup_requires_debug(patched_loop: Any) -> None: | |
mock_logger.configure_mock(**attrs) | ||
|
||
app = web.Application() | ||
web.run_app(app, debug=False, print=stopper(patched_loop), access_log=mock_logger) | ||
web.run_app( | ||
app, | ||
debug=False, | ||
print=stopper(patched_loop), | ||
access_log=mock_logger, | ||
loop=patched_loop, | ||
) | ||
mock_logger.setLevel.assert_not_called() | ||
mock_logger.hasHandlers.assert_not_called() | ||
mock_logger.addHandler.assert_not_called() | ||
|
@@ -745,7 +772,13 @@ def test_run_app_default_logger_setup_requires_default_logger( | |
mock_logger.configure_mock(**attrs) | ||
|
||
app = web.Application() | ||
web.run_app(app, debug=True, print=stopper(patched_loop), access_log=mock_logger) | ||
web.run_app( | ||
app, | ||
debug=True, | ||
print=stopper(patched_loop), | ||
access_log=mock_logger, | ||
loop=patched_loop, | ||
) | ||
mock_logger.setLevel.assert_not_called() | ||
mock_logger.hasHandlers.assert_not_called() | ||
mock_logger.addHandler.assert_not_called() | ||
|
@@ -762,7 +795,13 @@ def test_run_app_default_logger_setup_only_if_unconfigured(patched_loop: Any) -> | |
mock_logger.configure_mock(**attrs) | ||
|
||
app = web.Application() | ||
web.run_app(app, debug=True, print=stopper(patched_loop), access_log=mock_logger) | ||
web.run_app( | ||
app, | ||
debug=True, | ||
print=stopper(patched_loop), | ||
access_log=mock_logger, | ||
loop=patched_loop, | ||
) | ||
mock_logger.setLevel.assert_not_called() | ||
mock_logger.hasHandlers.assert_called_with() | ||
mock_logger.addHandler.assert_not_called() | ||
|
@@ -779,7 +818,7 @@ async def on_startup(app): | |
|
||
app.on_startup.append(on_startup) | ||
|
||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
assert task.cancelled() | ||
|
||
|
||
|
@@ -797,7 +836,7 @@ async def on_startup(app): | |
|
||
app.on_startup.append(on_startup) | ||
|
||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
assert task.done() | ||
|
||
|
||
|
@@ -823,7 +862,7 @@ async def on_startup(app): | |
|
||
exc_handler = mock.Mock() | ||
patched_loop.set_exception_handler(exc_handler) | ||
web.run_app(app, print=stopper(patched_loop)) | ||
web.run_app(app, print=stopper(patched_loop), loop=patched_loop) | ||
assert task.done() | ||
|
||
msg = { | ||
|
@@ -846,7 +885,12 @@ def base_runner_init_spy(self, *args, **kwargs): | |
|
||
app = web.Application() | ||
monkeypatch.setattr(BaseRunner, "__init__", base_runner_init_spy) | ||
web.run_app(app, keepalive_timeout=new_timeout, print=stopper(patched_loop)) | ||
web.run_app( | ||
app, | ||
keepalive_timeout=new_timeout, | ||
print=stopper(patched_loop), | ||
loop=patched_loop, | ||
) | ||
|
||
|
||
def test_run_app_context_vars(patched_loop: Any): | ||
|
@@ -877,5 +921,5 @@ async def init(): | |
count += 1 | ||
return app | ||
|
||
web.run_app(init(), print=stopper(patched_loop)) | ||
web.run_app(init(), print=stopper(patched_loop), loop=patched_loop) | ||
assert count == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a good idea to have it here. At least, put it right before
try:
. The reason is that it's an antipattern to have more than one instruction in a try block (it tends to shadow exceptions sometimes and causes hard-to-debug situations).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just noticed that it was inside the try block in
asyncio.run()
, figured there was probably a good reason for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, you could add a separate try/except if you want to call
loop.close()
and re-raise. There's no need toasyncio.set_event_loop(None)
because setting it didn't happen anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept these 2 lines together in the try to match https://github.com/python/cpython/blob/main/Lib/asyncio/runners.py#L40-L44
But, I've moved the create_task() to before the try statement, as it doesn't seem to have any business being there.