Skip to content

call exit callback even if AsyncProcess is reaped elsewhere #6684

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

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
18 changes: 15 additions & 3 deletions distributed/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ def _start():
def _watch_process(cls, selfref, process, state, q):
r = repr(selfref())
process.join()
exitcode = process.exitcode
assert exitcode is not None
logger.debug("[%s] process %r exited with code %r", r, state.pid, exitcode)
exitcode = original_exit_code = process.exitcode
if exitcode is None:
# The child process is already reaped
# (may happen if waitpid() is called elsewhere).
exitcode = 255
Comment on lines +237 to +239
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspired by:

        try:
            _, status = os.waitpid(pid, 0)
        except ChildProcessError:
            # The child process is already reaped
            # (may happen if waitpid() is called elsewhere).
            returncode = 255
            logger.warning(
                "child process pid %d exit status already read: "
                " will report returncode 255",
                pid)

https://github.com/python/cpython/blob/c5819c1f6c67f04e08f058ac7f24dec86e0e4554/Lib/asyncio/unix_events.py#L944-L952

state.is_alive = False
state.exitcode = exitcode
# Make sure the process is removed from the global list
Expand All @@ -247,6 +249,16 @@ def _watch_process(cls, selfref, process, state, q):
finally:
self = None # lose reference

# logging may fail - defer calls to after the callback is added
if original_exit_code is None:
logger.warning(
"[%s] process %r exit status was already read will report exitcode 255",
r,
state.pid,
)
else:
logger.debug("[%s] process %r exited with code %r", r, state.pid, exitcode)

def start(self):
"""
Start the child process.
Expand Down
13 changes: 3 additions & 10 deletions distributed/tests/test_asyncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import sys
import threading
import weakref
from datetime import timedelta
from time import sleep

import psutil
import pytest
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.locks import Event

from distributed.compatibility import LINUX, MACOS, WINDOWS
from distributed.metrics import time
Expand Down Expand Up @@ -238,13 +235,9 @@ async def test_close():
async def test_exit_callback():
to_child = get_mp_context().Queue()
from_child = get_mp_context().Queue()
evt = Event()
evt = asyncio.Event()

# FIXME: this breaks if changed to async def...
Copy link
Member Author

@graingert graingert Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks because async functions are not supported. set_exit_callback calls synchronous callbacks on the event loop thread, and this is intentional: https://github.com/dask/distributed/pull/6526/files

@gen.coroutine
def on_stop(_proc):
assert _proc is proc
yield gen.moment
evt.set()

# Normal process exit
Expand All @@ -259,7 +252,7 @@ def on_stop(_proc):
assert not evt.is_set()

to_child.put(None)
await evt.wait(timedelta(seconds=5))
await asyncio.wait_for(evt.wait(), 5)
assert evt.is_set()
assert not proc.is_alive()

Expand All @@ -275,7 +268,7 @@ def on_stop(_proc):
assert not evt.is_set()

await proc.terminate()
await evt.wait(timedelta(seconds=5))
await asyncio.wait_for(evt.wait(), 5)
assert evt.is_set()


Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ filterwarnings =
ignore:Scheduler already contains a plugin with name nonidempotentplugin. overwriting:UserWarning
ignore:Increasing number of chunks by factor of 20:dask.array.core.PerformanceWarning
ignore::distributed.versions.VersionMismatchWarning
ignore:(?s)Exception in thread AsyncProcess Dask Worker process \(from Nanny\) watch process join.*assert exitcode is not None:pytest.PytestUnhandledThreadExceptionWarning
ignore:(?s)Exception in thread AsyncProcess SpawnProcess-\d+ watch process join.*assert exitcode is not None:pytest.PytestUnhandledThreadExceptionWarning
ignore:(?s)Exception in thread.*old_ssh.*channel\.send\(b"\\x03"\).*Socket is closed:pytest.PytestUnhandledThreadExceptionWarning
ignore:(?s)Exception in thread.*paramiko\.ssh_exception\.NoValidConnectionsError:pytest.PytestUnhandledThreadExceptionWarning
ignore:(?s)Exception ignored in. <Finalize object, dead>.*sem_unlink.*FileNotFoundError:pytest.PytestUnraisableExceptionWarning
Expand Down