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

Restore install_signal_handlers due to downstream dependencies #6366

Merged
Merged
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
31 changes: 31 additions & 0 deletions distributed/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import signal
from typing import Any

from tornado.ioloop import IOLoop

logger = logging.getLogger(__name__)


Expand All @@ -26,3 +28,32 @@ def handle_signal(signum, frame):
old_handlers[sig] = signal.signal(sig, handle_signal)

await event.wait()


def install_signal_handlers(loop=None, cleanup=None):
"""
Install global signal handlers to halt the Tornado IOLoop in case of
a SIGINT or SIGTERM. *cleanup* is an optional callback called,
before the loop stops, with a single signal number argument.
"""
import signal
jacobtomlinson marked this conversation as resolved.
Show resolved Hide resolved

loop = loop or IOLoop.current()

old_handlers = {}

def handle_signal(sig, frame):
async def cleanup_and_stop():
try:
if cleanup is not None:
await cleanup(sig)
finally:
loop.stop()

loop.add_callback_from_signal(cleanup_and_stop)
# Restore old signal handler to allow for a quicker exit
# if the user sends the signal again.
signal.signal(sig, old_handlers[sig])

for sig in [signal.SIGINT, signal.SIGTERM]:
old_handlers[sig] = signal.signal(sig, handle_signal)