mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
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:
parent
66a2545858
commit
2730ea3397
1 changed files with 20 additions and 7 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue