Skip to content

Commit a950d47

Browse files
authored
fix(tauri): force Windows process tree shutdown (NeuralNomadsAI#240)
## Summary - force the Windows CLI process tree shutdown path during normal app close - avoid leaving child server processes alive when the direct wrapper process exits first - keep the change limited to the Windows shutdown path in cli_manager ## Testing - cargo check --manifest-path packages/tauri-app/src-tauri/Cargo.toml
1 parent 1c68f5d commit a950d47

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

packages/tauri-app/src-tauri/src/cli_manager.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ fn workspace_root() -> Option<PathBuf> {
5151
const SESSION_COOKIE_NAME: &str = "codenomad_session";
5252

5353
const CLI_STOP_GRACE_SECS: u64 = 30;
54+
#[cfg(windows)]
55+
const CLI_WINDOWS_FORCE_GRACE_MS: u64 = 2_000;
5456

5557
#[cfg(unix)]
5658
fn configure_posix_process_group(command: &mut Command) {
@@ -402,6 +404,8 @@ impl CliProcessManager {
402404
let mut child_opt = self.child.lock();
403405
if let Some(mut child) = child_opt.take() {
404406
log_line(&format!("stopping CLI pid={}", child.id()));
407+
#[cfg(windows)]
408+
let mut forced_tree_shutdown = false;
405409
#[cfg(unix)]
406410
unsafe {
407411
let pid = child.id() as i32;
@@ -414,16 +418,29 @@ impl CliProcessManager {
414418
}
415419
#[cfg(windows)]
416420
{
417-
if !kill_process_tree_windows(child.id(), false) {
418-
let _ = child.kill();
419-
}
421+
let _ = kill_process_tree_windows(child.id(), false);
420422
}
421423

422424
let start = Instant::now();
423425
loop {
424426
match child.try_wait() {
425427
Ok(Some(_)) => break,
426428
Ok(None) => {
429+
#[cfg(windows)]
430+
if !forced_tree_shutdown
431+
&& start.elapsed() > Duration::from_millis(CLI_WINDOWS_FORCE_GRACE_MS)
432+
{
433+
log_line(&format!(
434+
"regular Windows shutdown still running after {}ms; escalating pid={}",
435+
CLI_WINDOWS_FORCE_GRACE_MS,
436+
child.id()
437+
));
438+
forced_tree_shutdown = true;
439+
if !kill_process_tree_windows(child.id(), true) {
440+
let _ = child.kill();
441+
}
442+
}
443+
427444
if start.elapsed() > Duration::from_secs(CLI_STOP_GRACE_SECS) {
428445
log_line(&format!(
429446
"stop timed out after {}s; sending SIGKILL pid={}",
@@ -440,7 +457,11 @@ impl CliProcessManager {
440457
}
441458
#[cfg(windows)]
442459
{
443-
if !kill_process_tree_windows(child.id(), true) {
460+
if !forced_tree_shutdown
461+
&& !kill_process_tree_windows(child.id(), true)
462+
{
463+
let _ = child.kill();
464+
} else if forced_tree_shutdown {
444465
let _ = child.kill();
445466
}
446467
}

0 commit comments

Comments
 (0)