Disable inheritance of file descriptors created by Swift Testing by default.#1145
Merged
Conversation
…efault. This PR makes file descriptors created by Swift Testing `FD_CLOEXEC` by default (on Windows, `~HANDLE_FLAG_INHERIT`.) We then clear `FD_CLOEXEC` for specific file descriptors that should be inherited. On Darwin, this is effectively ignored because we use `POSIX_SPAWN_CLOEXEC_DEFAULT`, and on Windows we use `PROC_THREAD_ATTRIBUTE_HANDLE_LIST` which has much the same effect. (On non-Darwin POSIX platforms, there's no reliable way to ensure only one child process inherits a particular file descriptor.) This change is speculative. Resolves #1140.
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
Contributor
Author
|
@swift-ci test |
stmontgomery
reviewed
Jun 9, 2025
|
Confirmed that a swift-foundation branch with exit tests enabled successfully ran those exit tests with this swift-testing version on Ubuntu 20.04 (but failed previously) |
Contributor
Author
|
@swift-ci test |
stmontgomery
approved these changes
Jun 9, 2025
Contributor
|
@swift-ci Please test |
Contributor
Author
|
Relevant glibc change (non-portable and only on relatively recent glibc versions though.) https://sourceware.org/bugzilla/show_bug.cgi?id=23640 |
Contributor
Author
|
FreeBSD also clears |
Contributor
Author
Contributor
Author
Contributor
Author
2 tasks
grynspan
added a commit
that referenced
this pull request
Jun 10, 2025
On [FreeBSD](https://man.freebsd.org/cgi/man.cgi?query=posix_spawn_file_actions_adddup2), [OpenBSD](https://github.com/openbsd/src/blob/master/lib/libc/gen/posix_spawn.c#L155), [Android](https://android.googlesource.com/platform/bionic/+/master/libc/bionic/spawn.cpp#103), and [Glibc ≥ 2.29](https://sourceware.org/bugzilla/show_bug.cgi?id=23640), `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](https://pubs.opengroup.org/onlinepubs/9799919799/) thanks to [Austin Group Defect #411](https://www.austingroupbugs.net/view.php?id=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: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
owenv
added a commit
to owenv/swift-testing
that referenced
this pull request
Jun 16, 2025
…ing by default. (swiftlang#1145)" This reverts commit b0a1efd.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR makes file descriptors created by Swift Testing
FD_CLOEXECby default (on Windows,~HANDLE_FLAG_INHERIT.) We then clearFD_CLOEXECfor specific file descriptors that should be inherited. On Darwin, this is effectively ignored because we usePOSIX_SPAWN_CLOEXEC_DEFAULT, and on Windows we usePROC_THREAD_ATTRIBUTE_HANDLE_LISTwhich has much the same effect. (On non-Darwin POSIX platforms, there's no reliable way to ensure only one child process inherits a particular file descriptor.)This change is speculative. No additional unit tests are added because existing test coverage should be sufficient; the reported issue is on a platform (Ubuntu 20.04) where we don't have any CI jobs.
Resolves #1140.
Checklist: