mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Print: Printer Setup no longer leaves a zombie process behind on Linux and macOS
Printer Setup started system-config-printer, xdg-open or open and dropped the child, so the ended process stayed a zombie until the app quit. It now starts them through host_services::spawn_detached, which waits for the process on a thread. On Windows nothing changes. Builds on #11007, which adds spawn_detached.
This commit is contained in:
1 parent
a728d46e39
commit
a397bb5265
2 files changed
+73
-12
No files matched your search
@@ -84,13 +84,13 @@ fn reveal(path: &str) -> Result<(), String> {
|
||||
}
|
||||
|
||||
/// The thread that waits for a process [`spawn_detached`] started; it returns the exit status.
|
||||
type Reaper = std::thread::JoinHandle<std::io::Result<std::process::ExitStatus>>;
|
||||
pub(crate) type Reaper = std::thread::JoinHandle<std::io::Result<std::process::ExitStatus>>;
|
||||
|
||||
/// Starts `c` without the caller waiting for it. On Windows dropping the `Child` closes its handle,
|
||||
/// so there is no reaper. Elsewhere a thread waits for the process, so it is reaped when it ends
|
||||
/// instead of staying a zombie until the app exits. The thread starts first, so a process is never
|
||||
/// started without a waiter.
|
||||
fn spawn_detached(c: &mut std::process::Command) -> std::io::Result<Option<Reaper>> {
|
||||
pub(crate) fn spawn_detached(c: &mut std::process::Command) -> std::io::Result<Option<Reaper>> {
|
||||
#[cfg(windows)]
|
||||
let started = c.spawn().map(|_| None);
|
||||
#[cfg(not(windows))]
|
||||
|
||||
@@ -459,21 +459,82 @@ mod system {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let _ = printer;
|
||||
Command::new("open")
|
||||
.arg("x-apple.systempreferences:com.apple.Print-Scan-Settings.extension")
|
||||
.spawn()
|
||||
.map(|_| ())
|
||||
.map_err(|e| e.to_string())
|
||||
let mut open = Command::new("open");
|
||||
open.arg("x-apple.systempreferences:com.apple.Print-Scan-Settings.extension");
|
||||
start_first([open]).map(drop).map_err(|e| e.to_string())
|
||||
}
|
||||
#[cfg(not(any(windows, target_os = "macos")))]
|
||||
{
|
||||
if Command::new("system-config-printer").spawn().is_ok() {
|
||||
return Ok(());
|
||||
}
|
||||
let url = printer.map_or_else(|| "http://localhost:631/printers/".to_string(), |p| format!("http://localhost:631/printers/{p}"));
|
||||
Command::new("xdg-open").arg(url).spawn().map(|_| ()).map_err(|e| e.to_string())
|
||||
start_first(setup_commands(printer)).map(drop).map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// The commands [`setup`] tries in order on Linux and the BSDs: the printer settings app, then the
|
||||
/// CUPS web page for `printer` (or the printer list) in the browser.
|
||||
#[cfg(not(any(windows, target_os = "macos")))]
|
||||
pub(super) fn setup_commands(printer: Option<&str>) -> [Command; 2] {
|
||||
let url = printer.map_or_else(|| "http://localhost:631/printers/".to_string(), |p| format!("http://localhost:631/printers/{p}"));
|
||||
let mut open = Command::new("xdg-open");
|
||||
open.arg(url);
|
||||
[Command::new("system-config-printer"), open]
|
||||
}
|
||||
|
||||
/// Starts the first of `commands` that starts, through [`crate::host_services::spawn_detached`], so
|
||||
/// it is reaped when it ends. Returns the last error when none starts.
|
||||
#[cfg(not(windows))]
|
||||
pub(super) fn start_first(commands: impl IntoIterator<Item = Command>) -> std::io::Result<Option<crate::host_services::Reaper>> {
|
||||
let mut last = std::io::Error::other("no command to start");
|
||||
for mut c in commands {
|
||||
match crate::host_services::spawn_detached(&mut c) {
|
||||
Ok(reaper) => return Ok(reaper),
|
||||
Err(e) => last = e,
|
||||
}
|
||||
}
|
||||
Err(last)
|
||||
}
|
||||
}
|
||||
|
||||
/// Linux keeps `/proc/<pid>` in state `Z` for a process that ended until its parent waits for it.
|
||||
#[cfg(all(test, target_os = "linux"))]
|
||||
mod setup_tests {
|
||||
use super::system::{setup_commands, start_first};
|
||||
use std::process::Command;
|
||||
|
||||
#[test]
|
||||
fn printer_setup_tries_the_settings_app_then_the_cups_page() {
|
||||
let [app, page] = setup_commands(Some("Office Laser"));
|
||||
assert_eq!(app.get_program(), "system-config-printer");
|
||||
assert_eq!(app.get_args().count(), 0);
|
||||
assert_eq!(page.get_program(), "xdg-open");
|
||||
assert_eq!(page.get_args().collect::<Vec<_>>(), ["http://localhost:631/printers/Office Laser"]);
|
||||
let [_, list] = setup_commands(None);
|
||||
assert_eq!(list.get_args().collect::<Vec<_>>(), ["http://localhost:631/printers/"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_settings_fallback_is_reaped_once_it_ends() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let pid_file = dir.path().join("pid");
|
||||
let mut fallback = Command::new("sh");
|
||||
fallback.arg("-c").arg("echo $$ > \"$1\"; exit 3").arg("sh").arg(&pid_file);
|
||||
let reaper = start_first([Command::new("vectorcraft-no-such-program"), fallback]).unwrap().expect("a reaper on Linux");
|
||||
let status = reaper.join().unwrap().unwrap();
|
||||
assert_eq!(status.code(), Some(3), "the fallback ran after the missing program");
|
||||
let pid = std::fs::read_to_string(&pid_file).unwrap();
|
||||
let pid = pid.trim();
|
||||
assert!(!pid.is_empty(), "the process wrote its pid");
|
||||
let state = std::fs::read_to_string(format!("/proc/{pid}/stat"))
|
||||
.ok()
|
||||
.and_then(|s| s.rsplit_once(") ").and_then(|(_, rest)| rest.split(' ').next().map(str::to_string)));
|
||||
assert_ne!(state.as_deref(), Some("Z"), "process {pid} is still a zombie");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_no_command_starting_the_last_error_is_returned() {
|
||||
// A missing program fails with NotFound; a directory fails with PermissionDenied.
|
||||
let err = start_first([Command::new("vectorcraft-no-such-program"), Command::new("/")]).unwrap_err();
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, windows))]
|
||||
|
||||
Reference in new issue
Block a user