fix: clean up cortex processes (#5215)

* fix: clean up cortex processes

* chore: clean up

* chore: does not need output shell
This commit is contained in:
Louis 2025-06-09 22:23:07 +07:00 committed by GitHub
parent ecfbdf8256
commit 6d6790d5e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 10 deletions

View File

@ -50,8 +50,6 @@ export default class JanEngineManagementExtension extends EngineManagementExtens
* Called when the extension is loaded.
*/
async onLoad() {
// Symlink Engines Directory
await executeOnMain(NODE, 'symlinkEngines')
// Update default local engine
this.updateDefaultEngine()

View File

@ -174,7 +174,6 @@ export default class JanInferenceCortexExtension extends LocalOAIEngine {
console.log('Clean up cortex.cpp services')
this.shouldReconnect = false
this.clean()
await executeOnMain(NODE, 'dispose')
super.onUnload()
}

View File

@ -10,9 +10,9 @@ use tauri::{App, Emitter, Listener, Manager};
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
use tauri_plugin_store::StoreExt;
use tokio::sync::Mutex; // Using tokio::sync::Mutex
use tokio::time::{sleep, Duration};
// MCP
use tokio::{process::Command, sync::Mutex}; // Using tokio::sync::Mutex
// MCP
use super::{
cmd::{get_jan_data_folder_path, get_jan_extensions_path},
mcp::run_mcp_commands,
@ -211,6 +211,7 @@ pub fn setup_mcp(app: &App) {
}
pub fn setup_sidecar(app: &App) -> Result<(), String> {
clean_up();
let app_handle = app.handle().clone();
let app_handle_for_spawn = app_handle.clone();
tauri::async_runtime::spawn(async move {
@ -225,6 +226,7 @@ pub fn setup_sidecar(app: &App) -> Result<(), String> {
let mut cmd = app_handle_for_spawn
.shell()
.sidecar("cortex-server")
.expect("Failed to get sidecar command")
.args([
"--start-server",
@ -410,6 +412,34 @@ pub fn setup_sidecar(app: &App) -> Result<(), String> {
Ok(())
}
//
// Clean up function to kill the sidecar process
//
pub fn clean_up() {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
let _ = std::process::Command::new("taskkill")
.args(["-f", "-im", "llama-server.exe"])
.creation_flags(0x08000000)
.spawn();
let _ = std::process::Command::new("taskkill")
.args(["-f", "-im", "cortex-server.exe"])
.creation_flags(0x08000000)
.spawn();
}
#[cfg(unix)]
{
let _ = std::process::Command::new("pkill")
.args(["-f", "llama-server"])
.spawn();
let _ = std::process::Command::new("pkill")
.args(["-f", "cortex-server"])
.spawn();
}
log::info!("Clean up function executed, sidecar processes killed.");
}
fn copy_dir_all(src: PathBuf, dst: PathBuf) -> Result<(), String> {
fs::create_dir_all(&dst).map_err(|e| e.to_string())?;
log::info!("Copying from {:?} to {:?}", src, dst);

View File

@ -10,7 +10,7 @@ use std::{collections::HashMap, sync::Arc};
use tauri::Emitter;
use tokio::sync::Mutex;
use reqwest::blocking::Client;
use crate::core::setup::clean_up;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
@ -120,11 +120,8 @@ pub fn run() {
.on_window_event(|window, event| match event {
tauri::WindowEvent::CloseRequested { .. } => {
if window.label() == "main" {
let client = Client::new();
let url = "http://127.0.0.1:39291/processManager/destroy";
let _ = client.delete(url).send();
window.emit("kill-sidecar", ()).unwrap();
clean_up();
}
}
_ => {}