Skip to content

Fix Subprocess.wait_for_exit hanging when child is already reaped - #3791

Open
inchang-ing wants to merge 1 commit into
tornadoweb:masterfrom
inchang-ing:fix-wait-for-exit-already-reaped
Open

inchang-ing wants to merge 1 commit into
tornadoweb:masterfrom
inchang-ing:fix-wait-for-exit-already-reaped

Conversation

@inchang-ing

Copy link
Copy Markdown

Summary

Fixes #3364.

Subprocess.wait_for_exit (and set_exit_callback) eventually call os.waitpid. If the child process has already been reaped — for example by a direct call to subprocess.Popen.wait() before wait_for_exit is invoked — os.waitpid raises ChildProcessError. The previous code swallowed this exception and returned without resolving the Future, so wait_for_exit hung forever.

Now, when ChildProcessError is raised and the pid is still being waited on, the Subprocess is retrieved from the _waiting dict and its exit status is resolved from the Popen object (proc.returncode, which is already in the decoded form tornado uses) instead of going through os.waitpid again.

Reproduction

import asyncio
from tornado.process import Subprocess

async def f():
    p = Subprocess("ls")
    p.proc.wait()
    return await p.wait_for_exit()

asyncio.get_event_loop().run_until_complete(f())

Test plan

  • Added test_wait_for_exit_after_proc_wait and test_wait_for_exit_after_proc_wait_nonzero to tornado/test/process_test.py (Unix-only, matching the bug's availability).
  • Verified locally that the fix resolves the Future while the pre-fix code path leaves it unresolved (hangs).

If the child process has already been reaped (for example by a direct
call to subprocess.Popen.wait) before wait_for_exit / set_exit_callback
is invoked, os.waitpid raises ChildProcessError. The previous code
swallowed this exception and returned without resolving the Future, so
wait_for_exit would hang forever (tornado#3364).

Now resolve the exit status from the Popen object (proc.returncode,
which is already in decoded form) when the pid is still being waited on.

fixes tornadoweb#3364
Comment thread tornado/process.py
# ``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().

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subprocess.wait_for_exit never resolves if process terminated before it is called

2 participants