Skip to content

Use posix_spawn_file_actions_adddup2() to clear FD_CLOEXEC. #1147

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

Merged
merged 10 commits into from
Jun 10, 2025

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Jun 10, 2025

On FreeBSD, OpenBSD, Android, and Glibc ≥ 2.29, posix_spawn_file_actions_adddup2() automatically clears FD_CLOEXEC if the file descriptors passed to it are equal. Relying on this behaviour eliminates a race condition when spawning child processes. This functionality is standardized in POSIX.1-2024 thanks to Austin Group Defect #411.

Some older Linuxes (Amazon Linux 2 in particular) don't have this functionality, so we do a runtime check of the Glibc version.

This PR is a follow-up to #1145.
Resolves #1140.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

On FreeBSD, OpenBSD, Android, and Glibc ≥ 2.29, `posix_spawn_file_actions_adddup2()` automatically clears `FD_CLOEXEC` if the file descriptors passed to it are equal. Relying on this behaviour eliminates a race condition when spawning child processes.

Some older Linuxes (Amazon Linux 2 in particular) don't have this functionality, so we do a runtime check of the Glibc version.
@grynspan grynspan added this to the Swift 6.x milestone Jun 10, 2025
@grynspan grynspan self-assigned this Jun 10, 2025
@grynspan grynspan added enhancement New feature or request linux 🐧 Linux support (all distros) exit-tests ☠️ Work related to exit tests android 🤖 Android support freebsd 😈 FreeBSD support openbsd 🐡 OpenBSD support labels Jun 10, 2025
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan requested a review from jmschonfeld June 10, 2025 14:26
@grynspan grynspan marked this pull request as ready for review June 10, 2025 14:26
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test Windows

@grynspan
Copy link
Contributor Author

@swift-ci test

@jmschonfeld
Copy link

Confirmed that swift-foundation exit tests still work when running with this branch of swift-testing on Ubuntu 20.04

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@swift-ci test Linux

@grynspan
Copy link
Contributor Author

@swift-ci test

1 similar comment
@grynspan
Copy link
Contributor Author

@swift-ci test

@stmontgomery
Copy link
Contributor

@swift-ci please test

@grynspan grynspan merged commit 3278217 into main Jun 10, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/dup2-cloexec branch June 10, 2025 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android 🤖 Android support enhancement New feature or request exit-tests ☠️ Work related to exit tests freebsd 😈 FreeBSD support linux 🐧 Linux support (all distros) openbsd 🐡 OpenBSD support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exit tests hang indefinitely on Ubuntu 20.04
3 participants