Close-on-exec belongs at descriptor creation
A file descriptor can carry authority into a new program image. Setting close-on-exec after creation leaves a race in multithreaded programs; request it atomically when opening the descriptor, then make intentional inheritance explicit.
An exec starts with inherited handles
A process does not begin with only its arguments and environment. On Linux, open file descriptors remain open across a successful execve by default. A descriptor marked FD_CLOEXEC is closed during that transition. The new program therefore inherits any descriptor whose close-on-exec flag is clear, whether or not the new program knows what that number means. [3] [2]
That inheritance can be intentional: a parent may arrange standard input and output or hand a child a specific socket. But it is also ambient authority. An inherited listening socket, private file, pipe end or directory handle may let an unrelated child read, write, keep a connection alive or prevent another process from observing end-of-file. Descriptor hygiene is thus part of process isolation and resource-lifetime design, not just cleanup.
The two-call pattern leaves a window
A tempting pattern is fd = open(path, flags); fcntl(fd, F_SETFD, FD_CLOEXEC);. It appears to close the leak by setting the flag immediately. In a program with threads, however, another thread can create a child between those calls. If that thread forks and successfully execs before the flag is set, the child program receives the descriptor. The interval may be tiny, but a scheduler can place the operations in that order. [1] [2]
The bug is a race between operations that look locally adjacent but are not one kernel operation. A lock inside the code that opens a file only helps if every possible fork-and-exec path takes the same lock. Libraries can create descriptors too, so a local convention is easy to violate. The robust boundary is the descriptor-creation operation itself.
Ask the kernel to create a protected descriptor
On Linux, pass O_CLOEXEC to open or openat. The descriptor is then created with close-on-exec already enabled, avoiding the gap between obtaining it and changing its flag. The open reference specifically calls this flag essential in some multithreaded programs and notes that other descriptor-creating system calls have corresponding close-on-exec options. [1]
The same design applies wherever an API creates a descriptor: prefer its atomic close-on-exec variant when available, such as a close-on-exec pipe or socket creation operation. Check the target platform and API rather than assuming every call has identical flags. If an interface lacks an atomic form, the fallback must coordinate all descriptor creation with every process-creation path, or use another design that prevents concurrent inheritance.
Keep descriptor flags distinct from file status
FD_CLOEXEC is a flag on a descriptor. It is not a property of the underlying file’s contents, nor is it the same kind of setting as append mode or nonblocking I/O. F_GETFD reads descriptor flags, and F_SETFD changes them; execve consults the close-on-exec bit when replacing the process image. A failed execve does not close the descriptor, since the image transition did not succeed. [2]
This distinction helps explain why “the file is private” is not an answer to inheritance. The kernel is carrying a reference associated with the process, not copying the pathname into the child. Once inherited, the child can operate through that reference subject to the access already granted by it. A pathname-based permission check in the child is not required to rediscover the object.
Treat intended inheritance as an allowlist
A useful policy is to make descriptors close-on-exec by default and identify the few that a child must receive. Before launching a subprocess, map its standard streams and any explicitly passed handles, and close or mark the rest. This reverses the fragile assumption that every descriptor remains available unless someone remembers to close it. It also gives code review a concrete question: which capabilities cross this process boundary, and for what purpose?
The exception still needs correct timing. If a descriptor is meant for a child, arranging that inheritance should be part of a controlled spawn operation, not a broad interval during which arbitrary concurrent children can observe it. Conversely, close-on-exec does not revoke the descriptor from other threads or from a child that already inherited it. It governs the successful exec transition only.
Review the whole descriptor lifecycle
When auditing a process launcher, inventory descriptor creation, duplication, inheritance, and closure. Look for APIs that can create descriptors without close-on-exec, older fallback code that applies fcntl later, and error paths that leak descriptors before a spawn. Also test the child’s actual descriptor table in a controlled fixture; a successful parent-side flag check alone does not prove every creation path follows the policy.
The broader lesson is that process boundaries carry state as well as code. Arguments and environment are explicit inputs, while open descriptors can silently carry resources and authority. Creating them with the intended inheritance rule makes the boundary understandable at the moment it is formed.