|
| 1 | +//! A detached daemon must outlive the terminal that launched it (issue #2808). |
| 2 | +//! |
| 3 | +//! Reported symptom: `fresh -a`, then `Detach` — the client exits and |
| 4 | +//! `fresh --cmd daemon list` still shows the daemon. Close the terminal that |
| 5 | +//! originally launched it and the daemon dies anyway, so `fresh -a` comes back |
| 6 | +//! with `Connection refused`; worse, a *second* client attached from another |
| 7 | +//! terminal is killed along with it. The docs promise the opposite: "Terminal |
| 8 | +//! closes, but the Fresh daemon keeps running in the background." |
| 9 | +//! |
| 10 | +//! Cause: `spawn_server_detached` spawned the server as a plain child, so it |
| 11 | +//! stayed in the launching terminal's session and process group. Windows already |
| 12 | +//! passed `DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP`; the Unix side had no |
| 13 | +//! equivalent, and the `daemonize()` helper that would have provided one was |
| 14 | +//! never called from any path. |
| 15 | +//! |
| 16 | +//! Asserted here at the process level, because that is where the bug lives: the |
| 17 | +//! daemon must lead its own session, and must survive a `SIGKILL` delivered to |
| 18 | +//! the launching client's entire process group. `SIGKILL` to a process group is |
| 19 | +//! delivered to every member before `killpg` returns, so "still alive |
| 20 | +//! afterwards" is a deterministic verdict rather than a race. |
| 21 | +#![cfg(unix)] |
| 22 | + |
| 23 | +use std::path::{Path, PathBuf}; |
| 24 | +use std::time::Duration; |
| 25 | + |
| 26 | +fn pty_available() -> bool { |
| 27 | + use portable_pty::{native_pty_system, PtySize}; |
| 28 | + native_pty_system() |
| 29 | + .openpty(PtySize { |
| 30 | + rows: 24, |
| 31 | + cols: 80, |
| 32 | + pixel_width: 0, |
| 33 | + pixel_height: 0, |
| 34 | + }) |
| 35 | + .is_ok() |
| 36 | +} |
| 37 | + |
| 38 | +fn session_id_of(pid: u32) -> Option<i32> { |
| 39 | + let sid = unsafe { libc::getsid(pid as i32) }; |
| 40 | + if sid == -1 { |
| 41 | + None |
| 42 | + } else { |
| 43 | + Some(sid) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn process_alive(pid: u32) -> bool { |
| 48 | + unsafe { libc::kill(pid as i32, 0) == 0 } |
| 49 | +} |
| 50 | + |
| 51 | +/// Block until `pid_file` names a live process, which is how the client itself |
| 52 | +/// waits for daemon readiness. Unbounded on purpose (see CONTRIBUTING: no |
| 53 | +/// in-test timeouts); `cargo nextest` bounds the whole test externally. |
| 54 | +fn wait_for_daemon_pid(pid_file: &Path) -> u32 { |
| 55 | + loop { |
| 56 | + if let Ok(text) = std::fs::read_to_string(pid_file) { |
| 57 | + if let Ok(pid) = text.trim().parse::<u32>() { |
| 58 | + if process_alive(pid) { |
| 59 | + return pid; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + std::thread::sleep(Duration::from_millis(20)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn detached_daemon_outlives_its_launching_process_group() { |
| 69 | + if !pty_available() { |
| 70 | + eprintln!("SKIP: no PTY available"); |
| 71 | + return; |
| 72 | + } |
| 73 | + use portable_pty::{native_pty_system, CommandBuilder, PtySize}; |
| 74 | + |
| 75 | + let sandbox = tempfile::tempdir().unwrap(); |
| 76 | + let mk = |n: &str| -> PathBuf { |
| 77 | + let p = sandbox.path().join(n); |
| 78 | + std::fs::create_dir_all(&p).unwrap(); |
| 79 | + p |
| 80 | + }; |
| 81 | + // Isolate the daemon's sockets/pid files and all persistence into the |
| 82 | + // sandbox so this test cannot see or disturb a real daemon. |
| 83 | + let runtime_dir = mk("run"); |
| 84 | + let data_home = mk("data"); |
| 85 | + let home = mk("home"); |
| 86 | + let project = mk("project"); |
| 87 | + std::fs::write(project.join("file.txt"), "hello\n").unwrap(); |
| 88 | + |
| 89 | + // A daemon name unique to this test run, so the pid file path is known and |
| 90 | + // cannot collide with anything else. |
| 91 | + let session_name = format!("t{}", std::process::id()); |
| 92 | + let pid_file = runtime_dir |
| 93 | + .join("fresh") |
| 94 | + .join(format!("{session_name}.pid")); |
| 95 | + |
| 96 | + // Launch the client on a PTY, exactly as a terminal would. The PTY child |
| 97 | + // becomes its own session leader, standing in for "the launching terminal"; |
| 98 | + // the daemon it spawns must not join that session. |
| 99 | + let pair = native_pty_system() |
| 100 | + .openpty(PtySize { |
| 101 | + rows: 30, |
| 102 | + cols: 100, |
| 103 | + pixel_width: 0, |
| 104 | + pixel_height: 0, |
| 105 | + }) |
| 106 | + .unwrap(); |
| 107 | + let mut cmd = CommandBuilder::new(env!("CARGO_BIN_EXE_fresh")); |
| 108 | + cmd.args(["--no-upgrade-check", "-a", &session_name]); |
| 109 | + cmd.cwd(&project); |
| 110 | + cmd.env("XDG_RUNTIME_DIR", &runtime_dir); |
| 111 | + cmd.env("XDG_DATA_HOME", &data_home); |
| 112 | + cmd.env("HOME", &home); |
| 113 | + cmd.env("TERM", "xterm-256color"); |
| 114 | + let mut client = pair.slave.spawn_command(cmd).unwrap(); |
| 115 | + drop(pair.slave); |
| 116 | + |
| 117 | + let daemon_pid = wait_for_daemon_pid(&pid_file); |
| 118 | + let client_pid = client.process_id().expect("client pid"); |
| 119 | + |
| 120 | + let client_sid = session_id_of(client_pid).expect("client session id"); |
| 121 | + let daemon_sid = session_id_of(daemon_pid).expect("daemon session id"); |
| 122 | + |
| 123 | + // The daemon leads its own session. Sharing the client's session is what let |
| 124 | + // the terminal's teardown reach it. |
| 125 | + assert_ne!( |
| 126 | + daemon_sid, client_sid, |
| 127 | + "daemon (pid {daemon_pid}) must not share the launching client's session \ |
| 128 | + (pid {client_pid}, sid {client_sid})" |
| 129 | + ); |
| 130 | + |
| 131 | + // The user-visible promise: tearing the launching terminal down — its whole |
| 132 | + // process group, the way a closing terminal does — leaves the daemon running. |
| 133 | + let client_pgid = unsafe { libc::getpgid(client_pid as i32) }; |
| 134 | + assert!(client_pgid > 0, "could not read the client's process group"); |
| 135 | + assert_eq!( |
| 136 | + unsafe { libc::killpg(client_pgid, libc::SIGKILL) }, |
| 137 | + 0, |
| 138 | + "failed to signal the launching process group" |
| 139 | + ); |
| 140 | + let _ = client.wait(); |
| 141 | + |
| 142 | + assert!( |
| 143 | + process_alive(daemon_pid), |
| 144 | + "daemon (pid {daemon_pid}) died with the process group that launched it" |
| 145 | + ); |
| 146 | + |
| 147 | + // Leave nothing behind: the sandbox only isolates files, not processes. |
| 148 | + unsafe { libc::kill(daemon_pid as i32, libc::SIGKILL) }; |
| 149 | +} |
0 commit comments