Huny's Dev Blog
huny.dev

Python subprocess Pipe EOF Delays: Control Descriptor Inheritance with close_fds and pass_fds

Reproduce delayed pipe EOF caused by an inherited writer, then compare non-inheritable descriptors, close_fds, and pass_fds across four verified Linux cases.

Hun Jang
Hun Jang Dec 7, 2026

If reading a pipe never finishes after a Python parent closes its write end, check whether a child process inherited the same write end. When launching an external command on Linux, exclude unnecessary extra descriptors with close_fds=True, pass only required descriptors through pass_fds, and close them when finished. The example below tests four combinations and shows that merely retaining a descriptor can delay EOF even when the child writes no data.

Conceptual illustration of a retained endpoint in a child process and a gate admitting only required connections; not an actual process diagram or measurement.

Written and documentation checked: December 7, 2026. Execution environment: Linux/amd64, CPython 3.12.16. This is a POSIX descriptor example; Windows handle inheritance was not tested. These results are not from the latest Python release.

Closing the parent's copy does not close another process's copy

The Linux pipe documentation explains that EOF becomes observable after remaining data is consumed and every descriptor referring to the write end is closed. Closing the parent's copy does not close the write end held by another process. Whether the child actually sends data through that pipe is a separate question.

This reproduction never writes a byte into the tested pipe. It makes the read end nonblocking, reporting BlockingIOError as waiting when a writer remains and an empty byte string as EOF. This isolates the effect of an inherited writer without filling an output buffer.

This does not mean Python's defaults always leak descriptors. The Python os documentation states that descriptors created by Python are non-inheritable by default. The first case deliberately enables inheritance and relaxes the closing policy to reproduce the problem.

Separate the inheritance flag from the launch allowlist

os.set_inheritable changes a descriptor's inheritable flag. Separately, Popen controls which descriptors remain in the launched child. Read the close_fds and pass_fds contracts together in the subprocess documentation. The descriptors discussed here are additional descriptors, not standard input, output, or error.

Case
Parent writer's inheritable flag
close_fds
pass_fds
Purpose
inheritable-open-policy
True
False
Empty
Reproduce unintended inheritance
noninheritable-open-policy
False
False
Empty
Check exclusion through the flag
inheritable-close-policy
True
True
Empty
Check the launch closing policy
explicit-allowlist
False
True
The writer only
Check the overriding effect of explicit passing

The final case verifies that pass_fds is not merely a filter that rechecks the inheritable flag. Explicitly passing a non-inheritable writer still leaves it in the child. This is a feature for intentional IPC, so passing a descriptor is not inherently a mistake. Its closing point must also be defined.

Synchronize with READY and inspect EOF

The child prints READY to its separate standard output, then waits for one line on standard input. After receiving READY, the parent closes its own writer and checks the pipe before sending the newline that lets the child exit. This compares states after the new program has started without inserting an arbitrary sleep.

Save this as fd-inheritance-demo.py. Do not use -O, which disables assert checks.

Run:

On CPython 3.12.16, it exited with code 0 and printed:

In the first and fourth cases, the child retained the writer and the parent's nonblocking read reported waiting. In the second and third, EOF was observable while the child was still alive. Every case reached EOF after the child exited. The code checks expected states and exit codes, so success need not be inferred from output alone.

The tested pipe and the standard I/O pipes used for READY and exit control are separate. The child never uses the tested writer; retaining it alone changes the termination condition. Here, communicate sends the newline and reaps the child. It is not being used as a fix that automatically removes an unnecessarily inherited writer.

Pass only required ends and define ownership through cleanup

When reviewing external-command launch code, first establish why close_fds=False is needed. If the goal is to pass a particular IPC endpoint, consider specifying only required descriptors with pass_fds on POSIX. The receiving child must close its endpoint when work ends, and the parent must close copies it no longer uses. If grandchildren exist, inspect their launch paths too. This example verifies only one direct-child level.

The UNIX inheritance section in the os documentation places the closing boundary for non-inheritable descriptors at execution of a new program. A child created by fork alone still receives copies of non-inheritable descriptors. Setting that flag therefore does not remove the need to close unused endpoints immediately after fork. This example checks the state after Popen launches a new Python program.

Do not interpret close_fds=True as disabling all communication. Standard-stream redirection and explicitly allowed descriptors are separate. Conversely, closing every descriptor while investigating EOF delays can break intended IPC. Record the owner, recipient, and closing point for each pipe end to guide the actual fix.

This verification covers local anonymous pipes, direct children, and a single-threaded test program. It does not test multithreaded launch races, descriptors opened by C extensions, sockets, Windows handles, or buffer stalls from large output. Production systems also need separate timeout and process-tree cleanup policies.

한국어판: Python subprocess 파이프 EOF 지연: close_fds와 pass_fds로 상속 제어하기Python subprocess 파이프 EOF 지연: close_fds와 pass_fds로 상속 제어하기

추천 글