Skip to content

Commit 0093562

Browse files
committed
fix: avoid leaving zombie processes on unix
In `spawn_detached()`, the spawned process isn't waited on, which makes zombie processes. This is fixed by calling `wait()` on the `Child`. While this isn't done in the original function taken from the `open` crate, it is done in `Alacritty`'s implementation, which is what the author of `open` based the function on anyway.
1 parent 3503fa5 commit 0093562

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/spawn_detached.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,32 @@ pub fn spawn_detached(command: &mut process::Command) -> io::Result<()> {
1111
unsafe {
1212
use std::os::unix::process::CommandExt as _;
1313

14-
command.pre_exec(move || {
15-
match libc::fork() {
16-
-1 => return Err(io::Error::last_os_error()),
17-
0 => (),
18-
_ => libc::_exit(0),
19-
}
14+
command
15+
.pre_exec(move || {
16+
match libc::fork() {
17+
-1 => return Err(io::Error::last_os_error()),
18+
0 => (),
19+
_ => libc::_exit(0),
20+
}
2021

21-
if libc::setsid() == -1 {
22-
return Err(io::Error::last_os_error());
23-
}
22+
if libc::setsid() == -1 {
23+
return Err(io::Error::last_os_error());
24+
}
2425

25-
Ok(())
26-
});
26+
Ok(())
27+
})
28+
.spawn()?
29+
.wait()
30+
.map(|_| ())
2731
}
2832
#[cfg(windows)]
2933
{
3034
use std::os::windows::process::CommandExt;
3135
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
3236
const CREATE_NO_WINDOW: u32 = 0x08000000;
33-
command.creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW);
37+
command
38+
.creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW)
39+
.spawn()
40+
.map(|_| ())
3441
}
35-
36-
command.spawn().map(|_| ())
3742
}

0 commit comments

Comments
 (0)