Skip to content

Commit 94d78ef

Browse files
authored
fix(cli.rs): terminate the beforeDevCommand, closes #2794 (#2883)
1 parent 34a402f commit 94d78ef

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

.changes/fix-before-dev-command.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Properly terminate the `beforeDevCommand` process.

tooling/cli/Cargo.lock

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

tooling/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ dialoguer = "0.10"
6262
url = { version = "2.2", features = [ "serde" ] }
6363
os_pipe = "1"
6464
ignore = "0.4"
65+
ctrlc = "3.2"
6566

6667
[target."cfg(windows)".dependencies]
6768
encode_unicode = "0.3"

tooling/cli/src/dev.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ use std::{
2424
ffi::OsStr,
2525
process::{exit, Command},
2626
sync::{
27+
atomic::{AtomicBool, Ordering},
2728
mpsc::{channel, Receiver},
2829
Arc, Mutex,
2930
},
3031
time::Duration,
3132
};
3233

3334
static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new();
35+
static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();
3436

3537
#[derive(Debug, Parser)]
3638
#[clap(about = "Tauri dev", trailing_var_arg(true))]
@@ -116,13 +118,19 @@ pub fn command(options: Options) -> Result<()> {
116118
let status = child_
117119
.wait()
118120
.expect("failed to wait on \"beforeDevCommand\"");
119-
if !status.success() {
121+
if !(status.success() || KILL_BEFORE_DEV_FLAG.get().unwrap().load(Ordering::Relaxed)) {
120122
logger_.error("The \"beforeDevCommand\" terminated with a non-zero status code.");
121123
exit(status.code().unwrap_or(1));
122124
}
123125
});
124126

125127
BEFORE_DEV.set(Mutex::new(child)).unwrap();
128+
KILL_BEFORE_DEV_FLAG.set(AtomicBool::default()).unwrap();
129+
130+
let _ = ctrlc::set_handler(move || {
131+
kill_before_dev_process();
132+
exit(130);
133+
});
126134
}
127135
}
128136

@@ -280,11 +288,15 @@ pub fn command(options: Options) -> Result<()> {
280288
fn kill_before_dev_process() {
281289
if let Some(child) = BEFORE_DEV.get() {
282290
let child = child.lock().unwrap();
291+
KILL_BEFORE_DEV_FLAG
292+
.get()
293+
.unwrap()
294+
.store(true, Ordering::Relaxed);
283295
#[cfg(windows)]
284296
let _ = Command::new("powershell")
285297
.arg("-NoProfile")
286298
.arg("-Command")
287-
.arg(format!("function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid }}; Kill-Tree {}", child.id()))
299+
.arg(format!("function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid -ErrorAction SilentlyContinue }}; Kill-Tree {}", child.id()))
288300
.status();
289301
#[cfg(not(windows))]
290302
let _ = Command::new("pkill")

0 commit comments

Comments
 (0)