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

multiprocessing.popen_spawn_posix does not close child fds after spawn, can hang #118981

Open
albertz opened this issue May 13, 2024 · 0 comments · May be fixed by #118982
Open

multiprocessing.popen_spawn_posix does not close child fds after spawn, can hang #118981

albertz opened this issue May 13, 2024 · 0 comments · May be fixed by #118982
Labels
type-bug An unexpected behavior, bug, or error

Comments

@albertz
Copy link
Contributor

albertz commented May 13, 2024

Bug report

Bug description:

parent_r = child_w = child_r = parent_w = None
try:
parent_r, child_w = os.pipe()
child_r, parent_w = os.pipe()
cmd = spawn.get_command_line(tracker_fd=tracker_fd,
pipe_handle=child_r)
self._fds.extend([child_r, child_w])
self.pid = util.spawnv_passfds(spawn.get_executable(),
cmd, self._fds)
self.sentinel = parent_r
with open(parent_w, 'wb', closefd=False) as f:
f.write(fp.getbuffer())
finally:
fds_to_close = []
for fd in (parent_r, parent_w):
if fd is not None:
fds_to_close.append(fd)
self.finalizer = util.Finalize(self, util.close_fds, fds_to_close)
for fd in (child_r, child_w):
if fd is not None:
os.close(fd)

What happens here:

  • Two pipes get opened.
  • The child proc gets spawned, the child fds are passed to it.
  • It is written into the parent fd.
  • The child fds are closed.

This order is wrong and can lead to hangs when the child proc crashes in between for whatever reason. Specifically, when the client crashes, then the parent will hang while trying to write into the parent fd, in this line:

f.write(fp.getbuffer())

(I have this case because of some unpickling error happening in the child, but that's not really the point of the issue here.)

The fix should be easy: Add extra code to close the child fds, right after the spawn. This is also the standard pattern for this kind of code.

Also reported here: rwth-i6/returnn#1514

CPython versions tested on:

CPython main branch, CPython 3.11

Operating systems tested on:

Linux, Other

Linked PRs

@albertz albertz added the type-bug An unexpected behavior, bug, or error label May 13, 2024
albertz added a commit to albertz/cpython-1 that referenced this issue May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant