Skip to content

Commit 76da968

Browse files
committed
Make absolutely sure subprocess is not killed
1 parent 4c86e55 commit 76da968

File tree

3 files changed

+138
-10
lines changed

3 files changed

+138
-10
lines changed

Cargo.lock

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ edition = "2021"
77
serde = { version = "1.0.*", features = ["derive"] }
88
serde_json = { version = "1.0.*", features = ["preserve_order"] }
99

10+
[target.'cfg(windows)'.dependencies]
11+
windows = { version = "0.56", features = ["Win32_System_Threading"] }
12+
1013
[profile.dev-optimized]
1114
inherits = "dev"
1215
opt-level = 3

src/command.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,26 @@ impl Command {
9292
command.arg("--");
9393
command.arg(url);
9494

95-
// NOTE: On Windows, browser spawns process into a Job object.
96-
// NOTE: We need to detach player from the job, so it won't get killed after we're done,
97-
// NOTE: See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#closing_the_native_app
98-
#[cfg(windows)]
99-
{
100-
use std::os::windows::process::CommandExt;
101-
const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x01000000;
102-
103-
command.creation_flags(CREATE_BREAKAWAY_FROM_JOB);
104-
}
95+
Command::detach_mpv(&mut command);
10596

10697
command.spawn()?;
10798

10899
Ok(())
109100
}
101+
102+
// NOTE: Make sure the subprocess is not killed.
103+
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#closing_the_native_app
104+
105+
#[cfg(unix)]
106+
fn detach_mpv(command: &mut process::Command) {
107+
use std::os::unix::process::CommandExt;
108+
command.process_group(0);
109+
}
110+
111+
#[cfg(windows)]
112+
fn detach_mpv(command: &mut process::Command) {
113+
use std::os::windows::process::CommandExt;
114+
use windows::Win32::System::Threading::CREATE_BREAKAWAY_FROM_JOB;
115+
command.creation_flags(CREATE_BREAKAWAY_FROM_JOB.0);
116+
}
110117
}

0 commit comments

Comments
 (0)