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.
This commit is contained in:
Wez Furlong committed 2020-10-09 18:53:54 -07:00
1 parent ab2b2cff45
commit 0cd17b0407
2 files changed
+34 -20

No files matched your search

+33 -19
View File
@@ -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
+1 -1
View File
@@ -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();
}