Skip to content

Commit 44e34f2

Browse files
committed
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.
1 parent e767e6c commit 44e34f2

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src-tauri/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ 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-
] }
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+
# ] }
7171

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

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::core::state::AppState;
22
use tauri::State;
3-
use tokio::time::{timeout, Duration};
43

54
pub async fn cleanup_processes(state: State<'_, AppState>) {
65
let mut map = state.llama_server_process.lock().await;
@@ -12,6 +11,7 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
1211
{
1312
use nix::sys::signal::{kill, Signal};
1413
use nix::unistd::Pid;
14+
use tokio::time::{timeout, Duration};
1515

1616
if let Some(raw_pid) = child.id() {
1717
let raw_pid = raw_pid as i32;
@@ -36,18 +36,28 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
3636
#[cfg(all(windows, target_arch = "x86_64"))]
3737
{
3838
if let Some(raw_pid) = child.id() {
39-
log::info!("Terminating llama-server PID {}", raw_pid);
39+
log::warn!(
40+
"Gracefully terminating is unsupported on Windows, force-killing PID {}",
41+
raw_pid
42+
);
4043

41-
// Brief wait for natural shutdown
42-
match timeout(Duration::from_secs(2), child.wait()).await {
43-
Ok(Ok(status)) => {
44-
log::info!("llama-server exited gracefully: {}", status);
45-
}
46-
_ => {
47-
log::warn!("Force-killing llama-server PID {}", raw_pid);
48-
let _ = child.kill().await;
49-
let _ = child.wait().await;
50-
}
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+
),
5161
}
5262
}
5363
}

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,30 @@ pub async fn unload_llama_model(
331331
#[cfg(all(windows, target_arch = "x86_64"))]
332332
{
333333
if let Some(raw_pid) = child.id() {
334-
log::info!("Terminating llama-server 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+
);
345+
}
335346

336-
// Brief wait for natural shutdown
337-
match timeout(Duration::from_secs(2), child.wait()).await {
338-
Ok(Ok(status)) => {
339-
log::info!("llama-server exited gracefully: {}", status);
340-
}
341-
_ => {
342-
log::warn!("Force-killing llama-server PID {}", raw_pid);
343-
let _ = child.kill().await;
344-
let _ = child.wait().await;
345-
}
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+
),
346358
}
347359
}
348360
}

0 commit comments

Comments
 (0)