Fix children being killed with SIGKILL on macOS after GPU drivers are loaded

The child side of spawn() closed every inherited fd from 3 to 255. On macOS
system frameworks such as Metal and libdispatch create guarded file
descriptors and calling close() on one of them raises EXC_GUARD which kills
the process with SIGKILL. Instead mark the inherited fds close-on-exec, which
is always allowed for guarded fds, and let exec() close them.
This commit is contained in:
Kovid Goyal 2026-11-14 09:03:21 +05:30
parent 66a2545858
commit 2730ea3397
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C

View file

@ -68,6 +68,16 @@ write_to_stderr(const char *text) {
exit(EXIT_FAILURE); \
}
static void
mark_close_on_exec(int fd) {
// Only async-signal-safe functions are used as this is called in the child after fork()
int flags = fcntl(fd, F_GETFD);
if (flags == -1) return; // fd is not open
if (!(flags & FD_CLOEXEC)) {
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) exit_on_err("Failed to set the close-on-exec flag on an inherited file descriptor");
}
}
static void
wait_for_terminal_ready(int fd) {
char data;
@ -229,13 +239,16 @@ spawn(PyObject *self UNUSED, PyObject *args) {
wait_for_terminal_ready(ready_read_fd);
safe_close(ready_read_fd, __FILE__, __LINE__);
// Close any extra fds inherited from parent
if (has_preserved_fds) {
for (int c = 3; c < 256; c++) {
if (!FD_ISSET(c, &passed_fds)) safe_close(c, __FILE__, __LINE__);
}
} else
for (int c = 3; c < 256; c++) { safe_close(c, __FILE__, __LINE__); }
// Ensure any extra fds inherited from parent are closed by exec().
// They are marked close-on-exec rather than closed, as on macOS
// some of them may be guarded file descriptors created by system
// frameworks such as Metal or libdispatch and calling close() on a
// guarded file descriptor causes the kernel to kill the process
// with SIGKILL (EXC_GUARD). Guarded file descriptors are always
// close-on-exec so setting the flag on them is allowed.
for (int c = 3; c < 256; c++) {
if (!has_preserved_fds || !FD_ISSET(c, &passed_fds)) mark_close_on_exec(c);
}
extern char **environ;
environ = env;