Skip to content

Commit

Permalink
fix(cli.rs): terminate the beforeDevCommand, closes #2794 (#2883)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 28, 2022
1 parent 34a402f commit 94d78ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-before-dev-command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Properly terminate the `beforeDevCommand` process.
24 changes: 24 additions & 0 deletions tooling/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dialoguer = "0.10"
url = { version = "2.2", features = [ "serde" ] }
os_pipe = "1"
ignore = "0.4"
ctrlc = "3.2"

[target."cfg(windows)".dependencies]
encode_unicode = "0.3"
Expand Down
16 changes: 14 additions & 2 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ use std::{
ffi::OsStr,
process::{exit, Command},
sync::{
atomic::{AtomicBool, Ordering},
mpsc::{channel, Receiver},
Arc, Mutex,
},
time::Duration,
};

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

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

BEFORE_DEV.set(Mutex::new(child)).unwrap();
KILL_BEFORE_DEV_FLAG.set(AtomicBool::default()).unwrap();

let _ = ctrlc::set_handler(move || {
kill_before_dev_process();
exit(130);
});
}
}

Expand Down Expand Up @@ -280,11 +288,15 @@ pub fn command(options: Options) -> Result<()> {
fn kill_before_dev_process() {
if let Some(child) = BEFORE_DEV.get() {
let child = child.lock().unwrap();
KILL_BEFORE_DEV_FLAG
.get()
.unwrap()
.store(true, Ordering::Relaxed);
#[cfg(windows)]
let _ = Command::new("powershell")
.arg("-NoProfile")
.arg("-Command")
.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()))
.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()))
.status();
#[cfg(not(windows))]
let _ = Command::new("pkill")
Expand Down

0 comments on commit 94d78ef

Please sign in to comment.