From 0cd17b040796586f86199f2d57a8eda2a86b0461 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 9 Oct 2020 18:53:54 -0700 Subject: [PATCH] fix a post-fork issue that broke the mux server One of the updates in the past week made the reactor/async-io stuff sensitive to the fork performed by daemonizing. We can "simply" re-exec ourselves post-fork to resolve this. --- wezterm-mux-server/src/main.rs | 52 +++++++++++++++++++++------------- wezterm/src/server/client.rs | 2 +- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rs index c3869a4f5..12a9d1eb0 100644 --- a/wezterm-mux-server/src/main.rs +++ b/wezterm-mux-server/src/main.rs @@ -4,6 +4,7 @@ use mux::domain::{Domain, LocalDomain}; use mux::Mux; use portable_pty::cmdbuilder::CommandBuilder; use std::ffi::OsString; +use std::process::Command; use std::rc::Rc; use std::sync::Arc; use std::thread; @@ -67,35 +68,48 @@ fn run() -> anyhow::Result<()> { let config = config::configuration(); if opts.daemonize { daemonize::daemonize(&config)?; + // When we reach this line, we are in a forked child process, + // and the fork will have broken the async-io/reactor state + // of the smol runtime. + // To resolve this, we will re-exec ourselves in the block + // below that was originally Windows-specific } } - #[cfg(windows)] - { - use std::os::windows::process::CommandExt; - use std::process::Command; - // We can't literally daemonize, but we can spawn another copy + if opts.daemonize { + // On Windows we can't literally daemonize, but we can spawn another copy // of ourselves in the background! - if opts.daemonize { - let mut cmd = Command::new(std::env::current_exe().unwrap()); - if opts.skip_config { - cmd.arg("-n"); - } - if let Some(cwd) = opts.cwd { - cmd.arg("--cwd"); - cmd.arg(cwd); - } - if !opts.prog.is_empty() { - cmd.arg("--"); - for a in &opts.prog { - cmd.arg(a); - } + // On Unix, forking breaks the global state maintained by `smol`, + // so we need to re-exec ourselves to start things back up properly. + let mut cmd = Command::new(std::env::current_exe().unwrap()); + if opts.skip_config { + cmd.arg("-n"); + } + if let Some(cwd) = opts.cwd { + cmd.arg("--cwd"); + cmd.arg(cwd); + } + if !opts.prog.is_empty() { + cmd.arg("--"); + for a in &opts.prog { + cmd.arg(a); } + } + + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; cmd.creation_flags(winapi::um::winbase::DETACHED_PROCESS); let child = cmd.spawn(); drop(child); return Ok(()); } + + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + return Err(anyhow::anyhow!("failed to re-exec: {:?}", cmd.exec())); + } } // Remove some environment variables that aren't super helpful or diff --git a/wezterm/src/server/client.rs b/wezterm/src/server/client.rs index 7f951daf0..11ce351de 100644 --- a/wezterm/src/server/client.rs +++ b/wezterm/src/server/client.rs @@ -196,7 +196,7 @@ async fn client_thread_async( impl Promises { fn fail_all(&mut self, reason: &str) { - log::error!("failing all promises: {}", reason); + log::trace!("failing all promises: {}", reason); for (_, promise) in self.map.drain() { promise.try_send(Err(anyhow!("{}", reason))).unwrap(); }