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

REST API does not suppress KeyboardInterrupt #259

Merged
merged 2 commits into from
Aug 15, 2022
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
9 changes: 8 additions & 1 deletion tardis/rest/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .app.security import UserCredentials
from cobald.daemon import service
from cobald.daemon.plugins import yaml_tag
import threading

from uvicorn.config import Config
from uvicorn.server import Server
Expand Down Expand Up @@ -47,4 +48,10 @@ def get_user(self, user_name: str) -> Optional[UserCredentials]:
return None

async def run(self) -> None:
await Server(config=self._config).serve()
server = Server(config=self._config)
await server.serve()
# See https://github.com/encode/uvicorn/issues/1579
# The server has shut down after receiving *and suppressing* a signal.
# Explicitly raise the corresponding shutdown exception as a workaround.
if server.should_exit and threading.current_thread() is threading.main_thread():
raise KeyboardInterrupt
5 changes: 4 additions & 1 deletion tests/rest_t/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def test_run(self, mocked_server):

mocked_server.reset_mock()

run_async(self.rest_service.run)
# Mocking the server means that all its attributes are set, including
# the "I got killed by SIGINT" flag, which triggers its shutdown heuristic.
with self.assertRaises(KeyboardInterrupt):
run_async(self.rest_service.run)
mocked_server.assert_called_with(config=self.rest_service._config)

def test_secret_key(self):
Expand Down