Skip to content

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

Closed

Description

@albertz

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

Activity

  1. added a commit that references this issue on May 13, 2024
    0a55415
  2. self-assigned this
    on Sep 6, 2025
  3. gpshead commented on Sep 6, 2025

    @gpshead
    Member

    nice edge case find!

  4. moved this to In Progress in Multiprocessing issueson Sep 6, 2025
  5. added a commit that references this issue on Sep 7, 2025
  6. added 2 commits that reference this issue on Sep 7, 2025
  7. added a commit that references this issue on Sep 7, 2025
  8. added a commit that references this issue on Sep 9, 2025
  9. added a commit that references this issue on Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions