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

Fix double ctrl-c kill #2634

Merged
merged 2 commits into from Dec 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions sanic/worker/manager.py
Expand Up @@ -40,7 +40,7 @@ def __init__(
self.monitor_publisher, self.monitor_subscriber = monitor_pubsub
self.worker_state = worker_state
self.worker_state[self.MAIN_IDENT] = {"pid": self.pid}
self.terminated = False
self._shutting_down = False
self._serve = serve
self._server_settings = server_settings
self._server_count = count()
Expand Down Expand Up @@ -116,10 +116,9 @@ def join(self):
self.join()

def terminate(self):
if not self.terminated:
if not self._shutting_down:
for process in self.processes:
process.terminate()
self.terminated = True

def restart(
self,
Expand Down Expand Up @@ -257,7 +256,7 @@ def kill(self):
raise ServerKilled

def shutdown_signal(self, signal, frame):
if self.terminated:
if self._shutting_down:
logger.info("Shutdown interrupted. Killing.")
with suppress(ServerKilled):
self.kill()
Expand All @@ -270,6 +269,7 @@ def shutdown(self):
for process in self.processes:
if process.is_alive():
process.terminate()
self._shutting_down = True

@property
def pid(self):
Expand Down
20 changes: 18 additions & 2 deletions tests/worker/test_manager.py
Expand Up @@ -33,9 +33,7 @@ def test_terminate(os_mock: Mock):
context = Mock()
context.Process.return_value = process
manager = WorkerManager(1, fake_serve, {}, context, (Mock(), Mock()), {})
assert manager.terminated is False
manager.terminate()
assert manager.terminated is True
os_mock.kill.assert_called_once_with(1234, SIGINT)


Expand Down Expand Up @@ -63,6 +61,24 @@ def test_kill(os_mock: Mock):
os_mock.kill.assert_called_once_with(1234, SIGKILL)


@patch("sanic.worker.process.os")
@patch("sanic.worker.manager.os")
def test_shutdown_signal_send_kill(
manager_os_mock: Mock, process_os_mock: Mock
):
process = Mock()
process.pid = 1234
context = Mock()
context.Process.return_value = process
manager = WorkerManager(1, fake_serve, {}, context, (Mock(), Mock()), {})
assert manager._shutting_down is False
manager.shutdown_signal(SIGINT, None)
assert manager._shutting_down is True
process_os_mock.kill.assert_called_once_with(1234, SIGINT)
manager.shutdown_signal(SIGINT, None)
manager_os_mock.kill.assert_called_once_with(1234, SIGKILL)


def test_restart_all():
p1 = Mock()
p2 = Mock()
Expand Down