Skip to content

Commit

Permalink
restore signal handlers after wait for signals is done (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert authored May 20, 2022
1 parent cc383a6 commit 90efa71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion distributed/_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def handle_signal(signum, frame):
for sig in signals:
old_handlers[sig] = signal.signal(sig, handle_signal)

await event.wait()
try:
await event.wait()
finally:
for sig in signals:
signal.signal(sig, old_handlers[sig])
19 changes: 18 additions & 1 deletion distributed/cli/tests/test_dask_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def test_version_option():


@pytest.mark.slow
def test_idle_timeout(loop):
def test_idle_timeout():
start = time()
runner = CliRunner()
result = runner.invoke(
Expand All @@ -424,6 +424,23 @@ def test_idle_timeout(loop):
assert result.exit_code == 0


@pytest.mark.slow
def test_restores_signal_handler():
# another test could have altered the signal handler, so use a new function
# that both has sensible sigint behaviour *and* can be used as a sentinel
def raise_ki():
raise KeyboardInterrupt

original_handler = signal.signal(signal.SIGINT, raise_ki)
try:
CliRunner().invoke(
distributed.cli.dask_scheduler.main, ["--idle-timeout", "1s"]
)
assert signal.getsignal(signal.SIGINT) is raise_ki
finally:
signal.signal(signal.SIGINT, original_handler)


def test_multiple_workers_2(loop):
text = """
def dask_setup(worker):
Expand Down

0 comments on commit 90efa71

Please sign in to comment.