Skip to content
Open
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
20 changes: 20 additions & 0 deletions tornado/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ def _try_cleanup_process(cls, pid: int) -> None:
try:
ret_pid, status = os.waitpid(pid, os.WNOHANG) # type: ignore
except ChildProcessError:
# The child has already been reaped by something else (for
# example a direct call to ``subprocess.Popen.wait``). If we
# were waiting on it, resolve its exit status from the
# ``Popen`` object so that ``wait_for_exit`` /
# ``set_exit_callback`` don't hang forever.
subproc = cls._waiting.pop(pid, None)
if subproc is not None and subproc.proc.returncode is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If subproc.proc.returncode is None, this will still wait forever. This fixes things for the specific case of using both proc.wait() and await proc.wait_for_exit(), but that's not the only way processes can be reaped (and it doesn't seem likely to me that someone would use these two methods together).

I'm more concerned about action at a distance via the os.wait() function. This waits for an unspecified child process and reaps the first one it finds. Basically it never makes sense to use os.wait() and os.waitpid() in the same process, since wait makes waitpid unpredictable. But we can't control what else happens in the process so I think there's a good chance this is what happened in #3364.

I'm not sure there's anything we can do to handle that situation cleanly (the exit status of the child process is lost to us if we don't control the call to os.wait()). If we deprecated set_exit_callback we could cause wait_for_exit to raise an error. Or we could just log an error and hope that's enough to get someone to dig into their use of os.wait().

subproc.io_loop.add_callback(subproc._set_returncode_from_popen)
return
if ret_pid == 0:
return
Expand All @@ -360,3 +368,15 @@ def _set_returncode(self, status: int) -> None:
callback = self._exit_callback
self._exit_callback = None
callback(self.returncode)

def _set_returncode_from_popen(self) -> None:
# The process was reaped elsewhere (e.g. ``subprocess.Popen.wait``),
# so ``proc.returncode`` is already in the decoded form that
# ``_set_returncode`` would produce. Reuse it directly instead of
# going through ``os.waitpid`` again, which would raise
# ``ChildProcessError``.
self.returncode = self.proc.returncode
if self._exit_callback:
callback = self._exit_callback
self._exit_callback = None
callback(self.returncode)
26 changes: 26 additions & 0 deletions tornado/test/process_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,29 @@ def test_wait_for_exit_raise_disabled(self):
subproc = Subprocess([sys.executable, "-c", "import sys; sys.exit(1)"])
ret = yield subproc.wait_for_exit(raise_error=False)
self.assertEqual(ret, 1)

@gen_test
def test_wait_for_exit_after_proc_wait(self):
# Regression test for https://github.com/tornadoweb/tornado/issues/3364
# If the child has already been reaped (e.g. via a direct call to
# ``subprocess.Popen.wait``) before ``wait_for_exit`` is called, the
# returned Future must still resolve instead of hanging forever.
Subprocess.initialize()
self.addCleanup(Subprocess.uninitialize)
subproc = Subprocess([sys.executable, "-c", "pass"])
subproc.proc.wait()
ret = yield subproc.wait_for_exit()
self.assertEqual(ret, 0)
self.assertEqual(subproc.returncode, ret)

@gen_test
def test_wait_for_exit_after_proc_wait_nonzero(self):
# Same as above but with a non-zero exit status to verify the
# decoded return code is propagated correctly.
Subprocess.initialize()
self.addCleanup(Subprocess.uninitialize)
subproc = Subprocess([sys.executable, "-c", "import sys; sys.exit(3)"])
subproc.proc.wait()
ret = yield subproc.wait_for_exit(raise_error=False)
self.assertEqual(ret, 3)
self.assertEqual(subproc.returncode, ret)