-
-
Notifications
You must be signed in to change notification settings - Fork 749
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
Cooperative signal handling #1600
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3d21f06
test desired signal behaviour
maxfischer2781 5c9bd41
capture and restore signal handlers
maxfischer2781 c456659
ruff
maxfischer2781 ab22bc5
checks
maxfischer2781 89f6751
test asyncio handlers
maxfischer2781 6c53405
add note on signal handler handling
maxfischer2781 8682a94
remove legacy signal raising
maxfischer2781 103e51b
test SIGBREAK on windows
maxfischer2781 c8018a6
remove test guard
maxfischer2781 4abad94
include convered branch
maxfischer2781 3dd7f80
Update docs/index.md
Kludex bb99977
Update docs/index.md
Kludex bb102dd
Merge branch 'master' into forward-asyncio-signal
Kludex ce740d8
Merge branch 'master' into forward-asyncio-signal
Kludex 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,67 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import contextlib | ||
import signal | ||
import sys | ||
from typing import Callable, ContextManager, Generator | ||
|
||
import pytest | ||
|
||
from uvicorn.config import Config | ||
from uvicorn.server import Server | ||
|
||
|
||
# asyncio does NOT allow raising in signal handlers, so to detect | ||
# raised signals raised a mutable `witness` receives the signal | ||
@contextlib.contextmanager | ||
def capture_signal_sync(sig: signal.Signals) -> Generator[list[int], None, None]: | ||
"""Replace `sig` handling with a normal exception via `signal""" | ||
witness: list[int] = [] | ||
original_handler = signal.signal(sig, lambda signum, frame: witness.append(signum)) | ||
yield witness | ||
signal.signal(sig, original_handler) | ||
|
||
|
||
@contextlib.contextmanager | ||
def capture_signal_async(sig: signal.Signals) -> Generator[list[int], None, None]: # pragma: py-win32 | ||
"""Replace `sig` handling with a normal exception via `asyncio""" | ||
witness: list[int] = [] | ||
original_handler = signal.getsignal(sig) | ||
asyncio.get_running_loop().add_signal_handler(sig, witness.append, sig) | ||
yield witness | ||
signal.signal(sig, original_handler) | ||
|
||
|
||
async def dummy_app(scope, receive, send): # pragma: py-win32 | ||
pass | ||
|
||
|
||
if sys.platform == "win32": | ||
signals = [signal.SIGBREAK] | ||
signal_captures = [capture_signal_sync] | ||
else: | ||
signals = [signal.SIGTERM, signal.SIGINT] | ||
signal_captures = [capture_signal_sync, capture_signal_async] | ||
|
||
|
||
@pytest.mark.anyio | ||
@pytest.mark.parametrize("exception_signal", signals) | ||
@pytest.mark.parametrize("capture_signal", signal_captures) | ||
async def test_server_interrupt( | ||
exception_signal: signal.Signals, capture_signal: Callable[[signal.Signals], ContextManager[None]] | ||
): # pragma: py-win32 | ||
"""Test interrupting a Server that is run explicitly inside asyncio""" | ||
|
||
async def interrupt_running(srv: Server): | ||
while not srv.started: | ||
await asyncio.sleep(0.01) | ||
signal.raise_signal(exception_signal) | ||
|
||
server = Server(Config(app=dummy_app, loop="asyncio")) | ||
asyncio.create_task(interrupt_running(server)) | ||
with capture_signal(exception_signal) as witness: | ||
await server.serve() | ||
assert witness | ||
# set by the server's graceful exit handler | ||
assert server.should_exit |
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.
Why did we use
loop.add_signal_handler
before?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 haven't found a technical reason for it. It seems to have been added in #141 for no specific and then copied over the years. I assume it was used because the
asyncio
docs promote it as a Linux feature that is better in unspecified ways.On the technical side the only advantage of
loop.add_signal_handler
is that it allows synchronously triggering async code (e.g.event.set()
), and the handlers don't use that. Since the code has to be compatible with Windows, handlers cannot rely on being invoked by the loop anyways.