Skip to content

Conversation

@michael-yuji
Copy link
Member

Use closefrom(2) instead of close(2) in a loop to reduce the window of race condition with other part of the process cloud be polling/processing the file descriptors.

Motivation:

Currently, a loop is used to close all possible file descriptors prior to execve(2) being called. This has the side effect that file descriptors owned by different components of the same process is being closed unexpectedly, which cause issue #8197.

Use closefrom(2) instead of close in a loop to significantly reduce the window between closing the file descriptors and calling execve(2), to minimize the impact.

In #8197, linking a debug build of libdispatch can randomly crash swift-run, due to dispatch catching that the file descriptors it works on no longer being valid, which causes an assertion and crash the process.

Modifications:

Use closefrom(2) instead of close(2) in a loop on platforms supports it.

Result:

Reduce the probability swift-run crash due to race conditions.

@michael-yuji
Copy link
Member Author

@3405691582 I'm making the assumption that closefrom(2) is also available on the OpenBSD Swift toolchain.

@jakepetroules
Copy link
Contributor

@3405691582 I'm making the assumption that closefrom(2) is also available on the OpenBSD Swift toolchain.

Looks like it should be: https://man.openbsd.org/closefrom

@3405691582
Copy link
Member

Yes, it's on the platform, but don't we need to import Glibc for that? Or am I missing something?

@jakepetroules
Copy link
Contributor

Ah, that I don't know. Are there any libc imports needed for FreeBSD?

@michael-yuji
Copy link
Member Author

On FreeBSD, no further imports are needed. 🤔

@3405691582
Copy link
Member

It's probably fine then. It's been a while for me so I'm guessing it's probably some transitive import?

In the worst case, when I get back to tweaking SPM, can revisit.

@michael-yuji
Copy link
Member Author

@swift-ci please test

@jakepetroules
Copy link
Contributor

@swift-ci please test Windows

@jakepetroules jakepetroules merged commit b28cdfb into swiftlang:main Jan 11, 2025
5 checks passed
@jakepetroules
Copy link
Contributor

Merged at @michael-yuji's request as he does not yet have write access.

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.

3 participants