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

Quiet KeyboardInterrupt #2383

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def test_run_match_config_params() -> None:
if key not in ("self", "timeout_notify", "callback_notify")
}
run_params = {
key: repr(value) for key, value in inspect.signature(run).parameters.items() if key not in ("app_dir",)
key: repr(value)
for key, value in inspect.signature(run).parameters.items()
if key not in ("app_dir", "suppress_ki")
}
assert config_params == run_params

Expand Down
7 changes: 7 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ def run(
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
suppress_ki: bool = False,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down Expand Up @@ -575,6 +576,12 @@ def run(
Multiprocess(config, target=server.run, sockets=[sock]).run()
else:
server.run()
except KeyboardInterrupt:
# KI is received deep inside asyncio and comes with a noisy traceback
# suppress it or raise a new KI here to shorten the traceback
if suppress_ki:
return
raise KeyboardInterrupt("quit uvicorn") from None
finally:
if config.uds and os.path.exists(config.uds):
os.remove(config.uds) # pragma: py-win32
Expand Down
Loading