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

test_signal and test_terminate occasionally return SIGKILL on MacOS #6671

Merged
merged 2 commits into from
Jul 6, 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
48 changes: 30 additions & 18 deletions distributed/tests/test_asyncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tornado.ioloop import IOLoop
from tornado.locks import Event

from distributed.compatibility import WINDOWS
from distributed.compatibility import LINUX, MACOS, WINDOWS
from distributed.metrics import time
from distributed.process import AsyncProcess
from distributed.utils import get_mp_context
Expand All @@ -36,10 +36,10 @@ def exit_now(rc=0):
sys.exit(rc)


def exit_with_signal(signum):
def exit_with_sigint():
signal.signal(signal.SIGINT, signal.SIG_DFL)
os.kill(os.getpid(), signal.SIGINT)
while True:
os.kill(os.getpid(), signum)
sleep(0.01)


Expand Down Expand Up @@ -159,45 +159,57 @@ async def test_exitcode():
assert proc.exitcode is None

q.put(5)
await proc.join(timeout=30)
await proc.join()
assert not proc.is_alive()
assert proc.exitcode == 5


@pytest.mark.skipif(WINDOWS, reason="POSIX only")
def assert_exit_code(proc: AsyncProcess, expect: signal.Signals) -> None:
if WINDOWS:
# multiprocessing.Process.terminate() sets exit code -15 like in Linux, but
# os.kill(pid, signal.SIGTERM) sets exit code +15
assert proc.exitcode in (-expect, expect)
elif MACOS:
# FIXME this happens very frequently on GitHub MacOSX CI. Reason unknown.
if expect != signal.SIGKILL and proc.exitcode == -signal.SIGKILL:
raise pytest.xfail(reason="https://github.com/dask/distributed/issues/6393")
assert proc.exitcode == -expect
else:
assert LINUX
assert proc.exitcode == -expect


@gen_test()
async def test_signal():
proc = AsyncProcess(target=exit_with_signal, args=(signal.SIGINT,))
proc.daemon = True
async def test_sigint_from_same_process():
proc = AsyncProcess(target=exit_with_sigint)
assert not proc.is_alive()
assert proc.exitcode is None

await proc.start()
await proc.join(timeout=30)
await proc.join()

assert not proc.is_alive()
# Can be 255 with forkserver, see https://bugs.python.org/issue30589
assert proc.exitcode in (-signal.SIGINT, 255)
assert_exit_code(proc, signal.SIGINT)


@gen_test()
async def test_sigterm_from_parent_process():
proc = AsyncProcess(target=wait)
await proc.start()
os.kill(proc.pid, signal.SIGTERM)
await proc.join(timeout=30)

await proc.join()
assert not proc.is_alive()
assert proc.exitcode in (-signal.SIGTERM, 255)
assert_exit_code(proc, signal.SIGTERM)


@gen_test()
async def test_terminate():
proc = AsyncProcess(target=wait)
proc.daemon = True
await proc.start()
await proc.terminate()

await proc.join()
assert not proc.is_alive()
assert proc.exitcode in (-signal.SIGTERM, 255)
assert_exit_code(proc, signal.SIGTERM)


@gen_test()
Expand Down Expand Up @@ -333,7 +345,7 @@ async def test_kill():
await proc.kill()
await proc.join()
assert not proc.is_alive()
assert proc.exitcode in (-signal.SIGKILL, 255)
assert proc.exitcode == -signal.SIGKILL


def _worker_process(worker_ready, child_pipe):
Expand Down
3 changes: 2 additions & 1 deletion distributed/tests/test_semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import logging
import pickle
import signal
from datetime import timedelta
from time import sleep

Expand Down Expand Up @@ -146,7 +147,7 @@ def f(x, sem, kill_address):
if worker.address == kill_address:
import os

os.kill(os.getpid(), 15)
os.kill(os.getpid(), signal.SIGTERM)
return x

futures = client.map(
Expand Down