fix: ensure server process is properly terminated and reaped

This commit is contained in:
Akarshan 2025-06-11 19:05:31 +05:30 committed by Louis
parent f463008362
commit 2eeabf8ae6
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2

View File

@ -164,35 +164,39 @@ pub async fn unload_llama_model(
state: State<'_, AppState>, state: State<'_, AppState>,
) -> ServerResult<UnloadResult> { ) -> ServerResult<UnloadResult> {
let mut process_map = state.llama_server_process.lock().await; let mut process_map = state.llama_server_process.lock().await;
match process_map.remove(&pid) { match process_map.remove(&pid) {
Some(mut child) => { Some(mut child) => {
log::info!("Attempting to terminate server process with PID: {}", pid); log::info!("Terminating server process with PID: {}", pid);
match child.start_kill() { // 1. Send the kill signal
Ok(_) => { if let Err(e) = child.kill().await {
log::info!("Server process termination signal sent successfully"); log::error!("Failed to send kill to server process: {}", e);
return Ok(UnloadResult {
success: false,
error: Some(format!("kill failed: {}", e)),
});
}
// 2. Await its exit so the OS can reap it
match child.wait().await {
Ok(exit_status) => {
log::info!("Server exited with: {}", exit_status);
Ok(UnloadResult { Ok(UnloadResult {
success: true, success: true,
error: None, error: None,
}) })
} }
Err(e) => { Err(e) => {
log::error!("Failed to kill server process: {}", e); log::error!("Error waiting for server process: {}", e);
Ok(UnloadResult { Ok(UnloadResult {
success: false, success: false,
error: Some(format!("Failed to kill server process: {}", e)), error: Some(format!("wait failed: {}", e)),
}) })
} }
} }
} }
None => { None => {
log::warn!( log::warn!("No server with PID '{}' found to unload", pid);
"Attempted to unload server with PID '{}', but no such process exists",
pid
);
Ok(UnloadResult { Ok(UnloadResult {
success: true, success: true,
error: None, error: None,