Skip to content

pipe2/dup3 O_CLOEXEC - #1520

Open
jakepetroules wants to merge 1 commit into
swiftlang:mainfrom
jakepetroules:worktree-cloexec-swift-system-1.7
Open

pipe2/dup3 O_CLOEXEC#1520
jakepetroules wants to merge 1 commit into
swiftlang:mainfrom
jakepetroules:worktree-cloexec-swift-system-1.7

Conversation

@jakepetroules

Copy link
Copy Markdown
Collaborator

Add safeOpen / safePipe / safeDuplicate helpers on FileDescriptor that always request close-on-exec, so descriptors created by the build system are not leaked into the child processes it spawns. They route through the swift-system pipe2/dup3 wrappers where those are available:

  • safePipe uses FileDescriptor.pipe(options: .closeOnExec) on non-Apple platforms (including Windows), and on Apple platforms running OS 27 or later via the SDK's System framework; older Apple OSes fall back to a plain pipe().
  • safeDuplicate(as:) uses FileDescriptor.duplicate(as:options:) on non-Apple platforms (dup3 is unavailable on Windows) and on Apple OS 27 or later; elsewhere it falls back to a plain dup2().
  • safeDuplicate() (no target) uses fcntl(F_DUPFD_CLOEXEC) to atomically obtain the lowest-numbered unused descriptor with close-on-exec set; on Windows, which has no F_DUPFD_CLOEXEC, it falls back to a plain dup().
  • safeOpen forces OpenOptions.closeOnExec.

Adopt these wrappers in FSProxy, PbxCp, XCBuildDataArchive, LineReader, and the build service entry point, and require swift-system 1.7.3 (which exposes OpenOptions.closeOnExec on Windows).

Add safeOpen / safePipe / safeDuplicate helpers on FileDescriptor that
always request close-on-exec, so descriptors created by the build system
are not leaked into the child processes it spawns. They route through the
swift-system pipe2/dup3 wrappers where those are available:

- safePipe uses FileDescriptor.pipe(options: .closeOnExec) on non-Apple
  platforms (including Windows), and on Apple platforms running OS 27 or
  later via the SDK's System framework; older Apple OSes fall back to a
  plain pipe().
- safeDuplicate(as:) uses FileDescriptor.duplicate(as:options:) on non-Apple
  platforms (dup3 is unavailable on Windows) and on Apple OS 27 or later;
  elsewhere it falls back to a plain dup2().
- safeDuplicate() (no target) uses fcntl(F_DUPFD_CLOEXEC) to atomically
  obtain the lowest-numbered unused descriptor with close-on-exec set; on
  Windows, which has no F_DUPFD_CLOEXEC, it falls back to a plain dup().
- safeOpen forces OpenOptions.closeOnExec.

Adopt these wrappers in FSProxy, PbxCp, XCBuildDataArchive, LineReader, and
the build service entry point, and require swift-system 1.7.3.
@jakepetroules
jakepetroules force-pushed the worktree-cloexec-swift-system-1.7 branch from b85bab9 to 12e2137 Compare July 8, 2026 01:38
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.

1 participant