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 82ace3e72e4bf7338f422d5c79bdd6a0f8a2440e. * 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
This commit is contained in:
parent
079759939a
commit
0aaaca05a4
@ -63,11 +63,6 @@ nix = "=0.30.1"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
libc = "0.2.172"
|
||||
windows-sys = { version = "0.60.2", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_System_Console",
|
||||
"Win32_System_Threading" # for using CreateProcess flags like CREATE_NEW_PROCESS_GROUP
|
||||
] }
|
||||
|
||||
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
|
||||
tauri-plugin-updater = "2"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use tauri::State;
|
||||
use crate::core::state::AppState;
|
||||
use tauri::State;
|
||||
|
||||
pub async fn cleanup_processes(state: State<'_, AppState>) {
|
||||
let mut map = state.llama_server_process.lock().await;
|
||||
@ -19,8 +19,12 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
|
||||
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGTERM);
|
||||
|
||||
match timeout(Duration::from_secs(2), child.wait()).await {
|
||||
Ok(Ok(status)) => log::info!("Process {} exited gracefully: {}", raw_pid, status),
|
||||
Ok(Err(e)) => log::error!("Error waiting after SIGTERM for {}: {}", raw_pid, e),
|
||||
Ok(Ok(status)) => {
|
||||
log::info!("Process {} exited gracefully: {}", raw_pid, status)
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
log::error!("Error waiting after SIGTERM for {}: {}", raw_pid, e)
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("SIGTERM timed out for PID {}; sending SIGKILL", raw_pid);
|
||||
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGKILL);
|
||||
@ -29,27 +33,31 @@ pub async fn cleanup_processes(state: State<'_, AppState>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(windows, target_arch = "x86_64"))]
|
||||
{
|
||||
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
|
||||
use tokio::time::{timeout, Duration};
|
||||
|
||||
if let Some(raw_pid) = child.id() {
|
||||
log::info!("Sending Ctrl-C to PID {} during shutdown", raw_pid);
|
||||
let ok: i32 = unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid) };
|
||||
if ok == 0 {
|
||||
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
|
||||
}
|
||||
log::warn!(
|
||||
"Gracefully terminating is unsupported on Windows, force-killing PID {}",
|
||||
raw_pid
|
||||
);
|
||||
|
||||
match timeout(Duration::from_secs(2), child.wait()).await {
|
||||
Ok(Ok(status)) => log::info!("Process {} exited after Ctrl-C: {}", raw_pid, status),
|
||||
Ok(Err(e)) => log::error!("Error waiting after Ctrl-C for {}: {}", raw_pid, e),
|
||||
Err(_) => {
|
||||
log::warn!("Timed out for PID {}; force-killing", raw_pid);
|
||||
let _ = child.kill().await;
|
||||
let _ = child.wait().await;
|
||||
}
|
||||
// Since we know a graceful shutdown doesn't work and there are no child processes
|
||||
// to worry about, we can use `child.kill()` directly. On Windows, this is
|
||||
// a forceful termination via the `TerminateProcess` API.
|
||||
if let Err(e) = child.kill().await {
|
||||
log::error!("Failed to send kill signal to PID {}: {}. It may have already terminated.", raw_pid, e);
|
||||
}
|
||||
match child.wait().await {
|
||||
Ok(status) => log::info!(
|
||||
"process {} has been terminated. Final exit status: {}",
|
||||
raw_pid,
|
||||
status
|
||||
),
|
||||
Err(e) => log::error!(
|
||||
"Error waiting on child process {} after kill: {}",
|
||||
raw_pid,
|
||||
e
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -330,35 +330,34 @@ pub async fn unload_llama_model(
|
||||
|
||||
#[cfg(all(windows, target_arch = "x86_64"))]
|
||||
{
|
||||
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
|
||||
|
||||
if let Some(raw_pid) = child.id() {
|
||||
log::info!("Sending Ctrl-C to PID {}", raw_pid);
|
||||
let ok: i32 = unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid as u32) };
|
||||
if ok == 0 {
|
||||
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
|
||||
log::warn!("gracefully killing is unsupported on Windows, force-killing PID {}", raw_pid);
|
||||
|
||||
// Since we know a graceful shutdown doesn't work and there are no child processes
|
||||
// to worry about, we can use `child.kill()` directly. On Windows, this is
|
||||
// a forceful termination via the `TerminateProcess` API.
|
||||
if let Err(e) = child.kill().await {
|
||||
log::error!(
|
||||
"Failed to send kill signal to PID {}: {}. It may have already terminated.",
|
||||
raw_pid,
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
match timeout(Duration::from_secs(5), child.wait()).await {
|
||||
Ok(Ok(status)) => log::info!("Process exited after Ctrl-C: {}", status),
|
||||
Ok(Err(e)) => log::error!("Error waiting after Ctrl-C: {}", e),
|
||||
Err(_) => {
|
||||
log::warn!("Timed out; force-killing PID {}", raw_pid);
|
||||
if let Err(e) = child.kill().await {
|
||||
log::error!("Failed to kill process {}: {}", raw_pid, e);
|
||||
return Ok(UnloadResult {
|
||||
success: false,
|
||||
error: Some(format!("kill failed: {}", e)),
|
||||
});
|
||||
}
|
||||
if let Ok(s) = child.wait().await {
|
||||
log::info!("Process finally exited: {}", s);
|
||||
}
|
||||
}
|
||||
match child.wait().await {
|
||||
Ok(status) => log::info!(
|
||||
"process {} has been terminated. Final exit status: {}",
|
||||
raw_pid,
|
||||
status
|
||||
),
|
||||
Err(e) => log::error!(
|
||||
"Error waiting on child process {} after kill: {}",
|
||||
raw_pid,
|
||||
e
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(UnloadResult {
|
||||
success: true,
|
||||
error: None,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user