Skip to content

Commit e2ec4d1

Browse files
authored
call exit callback even if AsyncProcess is reaped elsewhere (#6684)
1 parent 602148f commit e2ec4d1

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

distributed/process.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,11 @@ def _start():
234234
def _watch_process(cls, selfref, process, state, q):
235235
r = repr(selfref())
236236
process.join()
237-
exitcode = process.exitcode
238-
assert exitcode is not None
239-
logger.debug("[%s] process %r exited with code %r", r, state.pid, exitcode)
237+
exitcode = original_exit_code = process.exitcode
238+
if exitcode is None:
239+
# The child process is already reaped
240+
# (may happen if waitpid() is called elsewhere).
241+
exitcode = 255
240242
state.is_alive = False
241243
state.exitcode = exitcode
242244
# Make sure the process is removed from the global list
@@ -249,6 +251,16 @@ def _watch_process(cls, selfref, process, state, q):
249251
finally:
250252
self = None # lose reference
251253

254+
# logging may fail - defer calls to after the callback is added
255+
if original_exit_code is None:
256+
logger.warning(
257+
"[%s] process %r exit status was already read will report exitcode 255",
258+
r,
259+
state.pid,
260+
)
261+
else:
262+
logger.debug("[%s] process %r exited with code %r", r, state.pid, exitcode)
263+
252264
def start(self):
253265
"""
254266
Start the child process.

distributed/tests/test_asyncprocess.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
import sys
88
import threading
99
import weakref
10-
from datetime import timedelta
1110
from time import sleep
1211

1312
import psutil
1413
import pytest
15-
from tornado import gen
1614
from tornado.ioloop import IOLoop
17-
from tornado.locks import Event
1815

1916
from distributed.compatibility import LINUX, MACOS, WINDOWS
2017
from distributed.metrics import time
@@ -239,13 +236,9 @@ async def test_close():
239236
async def test_exit_callback():
240237
to_child = get_mp_context().Queue()
241238
from_child = get_mp_context().Queue()
242-
evt = Event()
239+
evt = asyncio.Event()
243240

244-
# FIXME: this breaks if changed to async def...
245-
@gen.coroutine
246241
def on_stop(_proc):
247-
assert _proc is proc
248-
yield gen.moment
249242
evt.set()
250243

251244
# Normal process exit
@@ -260,7 +253,7 @@ def on_stop(_proc):
260253
assert not evt.is_set()
261254

262255
to_child.put(None)
263-
await evt.wait(timedelta(seconds=5))
256+
await asyncio.wait_for(evt.wait(), 5)
264257
assert evt.is_set()
265258
assert not proc.is_alive()
266259

@@ -276,7 +269,7 @@ def on_stop(_proc):
276269
assert not evt.is_set()
277270

278271
await proc.terminate()
279-
await evt.wait(timedelta(seconds=5))
272+
await asyncio.wait_for(evt.wait(), 5)
280273
assert evt.is_set()
281274

282275

setup.cfg

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

0 commit comments

Comments
 (0)