diff --git a/docs/changelog.markdown b/docs/changelog.markdown index cd91f3106..21ea0dad7 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -26,6 +26,7 @@ brief notes about them may accumulate here. * macOS: Adjusted default dpi to 72 to bring point sizes into alignment with other macOS apps. [#332](https://github.com/wez/wezterm/issues/332) * Improved font fallback; we now try harder to find a system-provided font for glyphs that are not found in your explicitly configured fonts. * Revised pty output processing and removed the related `ratelimit_output_bytes_per_second` option +* Workaround Cocoa leaking window position saved state file descriptors to child processes on macOS Big Sur ### 20201101-103216-403d002d diff --git a/pty/src/unix.rs b/pty/src/unix.rs index e7fc4fc21..560ce4e97 100644 --- a/pty/src/unix.rs +++ b/pty/src/unix.rs @@ -98,6 +98,36 @@ impl Read for PtyFd { } } +// On Big Sur, Cocoa leaks various file descriptors to child processes, +// so we need to make a pass through the open descriptors beyond just the +// stdio descriptors and close them all out. +// This is approximately equivalent to the darwin `posix_spawnattr_setflags` +// option POSIX_SPAWN_CLOEXEC_DEFAULT which is used as a bit of a cheat +// on macOS. +#[cfg(target_os = "macos")] +fn close_random_fds() { + if let Ok(dir) = std::fs::read_dir("/dev/fd") { + let mut fds = vec![]; + for entry in dir { + if let Some(num) = entry + .ok() + .map(|e| e.file_name()) + .and_then(|s| s.into_string().ok()) + .and_then(|n| n.parse::().ok()) + { + if num > 2 { + fds.push(num); + } + } + } + for fd in fds { + unsafe { + libc::close(fd); + } + } + } +} + impl PtyFd { fn resize(&self, size: PtySize) -> Result<(), Error> { let ws_size = winsize { @@ -188,6 +218,10 @@ impl PtyFd { return Err(io::Error::last_os_error()); } } + + #[cfg(target_os = "macos")] + close_random_fds(); + Ok(()) }) };