Skip to content

Commit 0aaaca0

Browse files
authored
fix: use direct process termination instead of console events on Windows (#5972)
* fix: remove CREATE_NEW_PROCESS_GROUP flag for proper Ctrl-C handling CREATE_NEW_PROCESS_GROUP prevented GenerateConsoleCtrlEvent from working, causing graceful shutdown failures. Removed to enable proper signal handling. * Revert "fix: remove CREATE_NEW_PROCESS_GROUP flag for proper Ctrl-C handling" This reverts commit 82ace3e. * fix: use direct process termination instead of console events Simplified Windows process cleanup by removing console attachment logic and using direct child.kill() method. More reliable for headless processes. * Fix missing imports * switch to tokio::time * Don't wait while forcefully terminate process using kill API on Windows Disabled use of windows-sys crate as graceful shutdown on Windows is unreliable in this context. Updated cleanup.rs and server.rs to directly call child.kill().await for terminating processes on Windows. Improved logging for process termination and error handling during kill and wait. Removed timeout-based graceful shutdown attempt on Windows since TerminateProcess is inherently forceful and immediate. This ensures more predictable process cleanup behavior on Windows platforms. * final cleanups
1 parent 0797599 commit 0aaaca0

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

src-tauri/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ nix = "=0.30.1"
6363

6464
[target.'cfg(windows)'.dependencies]
6565
libc = "0.2.172"
66-
windows-sys = { version = "0.60.2", features = [
67-
"Win32_Foundation",
68-
"Win32_System_Console",
69-
"Win32_System_Threading" # for using CreateProcess flags like CREATE_NEW_PROCESS_GROUP
70-
] }
7166

7267
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
7368
tauri-plugin-updater = "2"

src-tauri/src/core/utils/extensions/inference_llamacpp_extension/cleanup.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use tauri::State;
21
use crate::core::state::AppState;
2+
use tauri::State;
33

44
pub async fn cleanup_processes(state: State<'_, AppState>) {
55
let mut map = state.llama_server_process.lock().await;
@@ -19,8 +19,12 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
1919
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGTERM);
2020

2121
match timeout(Duration::from_secs(2), child.wait()).await {
22-
Ok(Ok(status)) => log::info!("Process {} exited gracefully: {}", raw_pid, status),
23-
Ok(Err(e)) => log::error!("Error waiting after SIGTERM for {}: {}", raw_pid, e),
22+
Ok(Ok(status)) => {
23+
log::info!("Process {} exited gracefully: {}", raw_pid, status)
24+
}
25+
Ok(Err(e)) => {
26+
log::error!("Error waiting after SIGTERM for {}: {}", raw_pid, e)
27+
}
2428
Err(_) => {
2529
log::warn!("SIGTERM timed out for PID {}; sending SIGKILL", raw_pid);
2630
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGKILL);
@@ -29,27 +33,31 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
2933
}
3034
}
3135
}
32-
3336
#[cfg(all(windows, target_arch = "x86_64"))]
3437
{
35-
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
36-
use tokio::time::{timeout, Duration};
37-
3838
if let Some(raw_pid) = child.id() {
39-
log::info!("Sending Ctrl-C to PID {} during shutdown", raw_pid);
40-
let ok: i32 = unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid) };
41-
if ok == 0 {
42-
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
43-
}
39+
log::warn!(
40+
"Gracefully terminating is unsupported on Windows, force-killing PID {}",
41+
raw_pid
42+
);
4443

45-
match timeout(Duration::from_secs(2), child.wait()).await {
46-
Ok(Ok(status)) => log::info!("Process {} exited after Ctrl-C: {}", raw_pid, status),
47-
Ok(Err(e)) => log::error!("Error waiting after Ctrl-C for {}: {}", raw_pid, e),
48-
Err(_) => {
49-
log::warn!("Timed out for PID {}; force-killing", raw_pid);
50-
let _ = child.kill().await;
51-
let _ = child.wait().await;
52-
}
44+
// Since we know a graceful shutdown doesn't work and there are no child processes
45+
// to worry about, we can use `child.kill()` directly. On Windows, this is
46+
// a forceful termination via the `TerminateProcess` API.
47+
if let Err(e) = child.kill().await {
48+
log::error!("Failed to send kill signal to PID {}: {}. It may have already terminated.", raw_pid, e);
49+
}
50+
match child.wait().await {
51+
Ok(status) => log::info!(
52+
"process {} has been terminated. Final exit status: {}",
53+
raw_pid,
54+
status
55+
),
56+
Err(e) => log::error!(
57+
"Error waiting on child process {} after kill: {}",
58+
raw_pid,
59+
e
60+
),
5361
}
5462
}
5563
}

src-tauri/src/core/utils/extensions/inference_llamacpp_extension/server.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -330,35 +330,34 @@ pub async fn unload_llama_model(
330330

331331
#[cfg(all(windows, target_arch = "x86_64"))]
332332
{
333-
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
334-
335333
if let Some(raw_pid) = child.id() {
336-
log::info!("Sending Ctrl-C to PID {}", raw_pid);
337-
let ok: i32 = unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid as u32) };
338-
if ok == 0 {
339-
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
334+
log::warn!("gracefully killing is unsupported on Windows, force-killing PID {}", raw_pid);
335+
336+
// Since we know a graceful shutdown doesn't work and there are no child processes
337+
// to worry about, we can use `child.kill()` directly. On Windows, this is
338+
// a forceful termination via the `TerminateProcess` API.
339+
if let Err(e) = child.kill().await {
340+
log::error!(
341+
"Failed to send kill signal to PID {}: {}. It may have already terminated.",
342+
raw_pid,
343+
e
344+
);
340345
}
341346

342-
match timeout(Duration::from_secs(5), child.wait()).await {
343-
Ok(Ok(status)) => log::info!("Process exited after Ctrl-C: {}", status),
344-
Ok(Err(e)) => log::error!("Error waiting after Ctrl-C: {}", e),
345-
Err(_) => {
346-
log::warn!("Timed out; force-killing PID {}", raw_pid);
347-
if let Err(e) = child.kill().await {
348-
log::error!("Failed to kill process {}: {}", raw_pid, e);
349-
return Ok(UnloadResult {
350-
success: false,
351-
error: Some(format!("kill failed: {}", e)),
352-
});
353-
}
354-
if let Ok(s) = child.wait().await {
355-
log::info!("Process finally exited: {}", s);
356-
}
357-
}
347+
match child.wait().await {
348+
Ok(status) => log::info!(
349+
"process {} has been terminated. Final exit status: {}",
350+
raw_pid,
351+
status
352+
),
353+
Err(e) => log::error!(
354+
"Error waiting on child process {} after kill: {}",
355+
raw_pid,
356+
e
357+
),
358358
}
359359
}
360360
}
361-
362361
Ok(UnloadResult {
363362
success: true,
364363
error: None,

0 commit comments

Comments
 (0)