Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(engine): stop mlx-server on SIGTERM and reap one left by a previous Jan (#9355)
Thinh Lea5de19bb9ad7
diff --git a/src-tauri/plugins/tauri-plugin-mlx/src/commands.rs b/src-tauri/plugins/tauri-plugin-mlx/src/commands.rsindex 9f99b5dde..fc1fc3496 100644--- a/src-tauri/plugins/tauri-plugin-mlx/src/commands.rs+++ b/src-tauri/plugins/tauri-plugin-mlx/src/commands.rs@@ -105,6 +105,10 @@ pub async fn load_mlx_model_impl(let mut command = Command::new(&bin_path);command.args(&args);command.envs(envs);+ // Lets a later Jan tell this server from one someone runs themselves, so it+ // can stop it if this app dies without its exit cleanup. Set after `envs`:+ // nothing the caller passes may remove it.+ command.env(jan_utils::MLX_OWNER_ENV, "1");command.stdout(Stdio::piped());command.stderr(Stdio::piped());@@ -392,3 +396,68 @@ pub async fn get_mlx_all_sessions<R: Runtime>() -> Result<Vec<SessionInfo>, String> {get_all_active_sessions(app_handle).await}++#[cfg(all(test, unix))]+mod tests {+ use super::*;+ use std::os::unix::fs::PermissionsExt;++ /// The startup sweep stops an orphaned `mlx-server` only when it carries+ /// `MLX_OWNER_ENV`, so every server this plugin starts must have it, even+ /// when the caller's own environment names the variable.+ #[tokio::test]+ async fn the_server_it_starts_carries_jans_marker() {+ let dir = std::env::temp_dir().join(format!(+ "jan-mlx-marker-{}-{}",+ std::process::id(),+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos()+ ));+ std::fs::create_dir_all(&dir).unwrap();+ let env_dump = dir.join("env.txt");+ let server = dir.join("mlx-server");+ // Dump the environment first, then announce readiness, so the file is+ // complete by the time the plugin returns.+ std::fs::write(+ &server,+ format!(+ "#!/bin/sh\nenv > '{}'\necho 'server is listening'\nexec sleep 30\n",+ env_dump.display()+ ),+ )+ .unwrap();+ std::fs::set_permissions(&server, std::fs::Permissions::from_mode(0o755)).unwrap();++ let sessions = Arc::new(Mutex::new(HashMap::new()));+ let mut envs = HashMap::new();+ envs.insert(jan_utils::MLX_OWNER_ENV.to_string(), "0".to_string());+ let session = load_mlx_model_impl(+ sessions.clone(),+ &server,+ "m".to_string(),+ dir.display().to_string(),+ 1,+ MlxConfig { ctx_size: 0 },+ envs,+ false,+ 10,+ )+ .await+ .expect("fake server should load");++ let dumped = std::fs::read_to_string(&env_dump).unwrap();+ let marker = format!("{}=1", jan_utils::MLX_OWNER_ENV);+ assert!(dumped.lines().any(|l| l == marker), "{dumped}");+ assert!(+ !dumped.lines().any(|l| l == format!("{}=0", jan_utils::MLX_OWNER_ENV)),+ "{dumped}"+ );++ if let Some(mut s) = sessions.lock().await.remove(&session.pid) {+ let _ = s.child.kill().await;+ }+ let _ = std::fs::remove_dir_all(&dir);+ }+}diff --git a/src-tauri/src/core/system/orphans.rs b/src-tauri/src/core/system/orphans.rsindex 79a7e78be..e79497e66 100644--- a/src-tauri/src/core/system/orphans.rs+++ b/src-tauri/src/core/system/orphans.rs@@ -25,15 +25,26 @@//! parent that started after its child is not its owner. When a start time is//! unknown the order cannot be told: a router keeps its Jan, and a process is//! never swept as a child on that evidence.+//!+//! `mlx-server` (macOS) is swept too, with a stricter rule. It is a direct+//! child of the app and does not watch its parent, so an app that dies without+//! running its exit cleanup (a crash, `kill -9`) leaves it holding the model in+//! unified memory. Its name, path and flags can all belong to a server someone+//! runs themselves, so it is only taken when it carries the environment marker+//! Jan sets when it starts one, and nothing is left above it (macOS reparents+//! it to launchd). A server started by a version before the marker existed+//! cannot be told apart and is left alone.use std::collections::{HashMap, HashSet};use std::path::Path;+use jan_utils::MLX_OWNER_ENV;use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};const ROUTER_BINARY: &str = "llama-server";const ROUTER_PRESET_FILE: &str = "router.preset.ini";const PRESET_FLAG: &str = "--models-preset";+const MLX_BINARY: &str = "mlx-server";/// The slice of a process the matcher needs, so it can be tested without/// spawning anything.@@ -45,6 +56,9 @@ pub struct ProcInfo {pub cmd: Vec<String>,/// Seconds since the epoch; 0 when the OS would not say.pub start_time: u64,+ /// Carries the environment marker Jan sets on the `mlx-server` it starts.+ /// Only read for `mlx-server` processes; false for everything else.+ pub jan_launched: bool,}fn normalize(path: &str) -> String {@@ -98,6 +112,18 @@ fn is_jan_router(proc: &ProcInfo, llamacpp_dir: &str) -> bool {&& preset.starts_with(&format!("{llamacpp_dir}/"))}+fn is_mlx_binary(name: &str) -> bool {+ name.eq_ignore_ascii_case(MLX_BINARY)+}++/// Whether `proc` is an MLX server that a Jan started. There is no preset file+/// tying it to a data folder, and its name, path and flags are all things+/// someone running their own `mlx-server` can have, so the only proof is the+/// marker Jan puts in the server's environment when it starts one.+fn is_jan_mlx_server(proc: &ProcInfo) -> bool {+ is_mlx_binary(&proc.name) && proc.jan_launched+}+/// A Jan process, judged by name: `Jan`, `jan`, `Jan.exe`, `Jan-Desktop.exe`.fn is_jan(name: &str) -> bool {name.to_ascii_lowercase().contains("jan")@@ -133,6 +159,16 @@ fn is_orphaned(proc: &ProcInfo, by_pid: &HashMap<u32, &ProcInfo>) -> bool {}}+/// Whether nothing is left above `proc`: no parent, init, or a parent pid that+/// is not running. macOS hands the child of a dead process to launchd (pid 1)+/// at once, so this is exactly a server whose app died.+fn has_no_live_parent(proc: &ProcInfo, by_pid: &HashMap<u32, &ProcInfo>) -> bool {+ match proc.parent {+ None | Some(1) => true,+ Some(parent) => !by_pid.contains_key(&parent),+ }+}+/// `root` and everything below it, each process listed before its children.////// Each pid is listed once. Windows never reparents and reuses pids, so a dead@@ -187,6 +223,17 @@ pub fn orphaned_engine_pids(procs: &[ProcInfo], data_folder: &Path) -> Vec<u32>}}}+ // `mlx-server` has no router above it and no children, so it goes alone.+ // Unlike a router it is only taken when nothing is left above it: a live+ // parent of any kind, a shell included, means someone started it on purpose.+ for server in procs+ .iter()+ .filter(|p| is_jan_mlx_server(p) && has_no_live_parent(p, &by_pid))+ {+ if seen.insert(server.pid) {+ victims.push(server.pid);+ }+ }victims}@@ -194,8 +241,9 @@ pub fn orphaned_engine_pids(procs: &[ProcInfo], data_folder: &Path) -> Vec<u32>/// killed. Blocking: run it off the async runtime.pub fn sweep_orphaned_engines(data_folder: &Path) -> usize {// A fresh install, or a user who never ran the llama.cpp engine, has- // nothing to sweep and should not pay for a process-table scan.- if !data_folder.join("llamacpp").is_dir() {+ // nothing to sweep and should not pay for a process-table scan. macOS+ // always scans: an MLX model can be the only engine ever used there.+ if !cfg!(target_os = "macos") && !data_folder.join("llamacpp").is_dir() {return 0;}@@ -205,6 +253,22 @@ pub fn sweep_orphaned_engines(data_folder: &Path) -> usize {true,ProcessRefreshKind::nothing().with_cmd(UpdateKind::Always),);+ // Only an `mlx-server` can carry Jan's marker, and reading a process's+ // environment costs a call each, so ask for those alone.+ let mlx_pids: Vec<Pid> = system+ .processes()+ .iter()+ .filter(|(_, p)| is_mlx_binary(&p.name().to_string_lossy()))+ .map(|(pid, _)| *pid)+ .collect();+ if !mlx_pids.is_empty() {+ system.refresh_processes_specifics(+ ProcessesToUpdate::Some(&mlx_pids),+ true,+ ProcessRefreshKind::nothing().with_environ(UpdateKind::Always),+ );+ }+ let marker = format!("{MLX_OWNER_ENV}=");let procs: Vec<ProcInfo> = system.processes().values()@@ -213,6 +277,10 @@ pub fn sweep_orphaned_engines(data_folder: &Path) -> usize {parent: p.parent().map(Pid::as_u32),name: p.name().to_string_lossy().into_owned(),start_time: p.start_time(),+ jan_launched: p+ .environ()+ .iter()+ .any(|kv| kv.to_string_lossy().starts_with(&marker)),cmd: p.cmd().iter()@@ -227,14 +295,15 @@ pub fn sweep_orphaned_engines(data_folder: &Path) -> usize {let Some(process) = system.process(Pid::from_u32(pid)) else {continue;};+ let name = process.name().to_string_lossy();log::warn!(- "Killing llama-server left running by a previous Jan: pid {pid}, parent {:?}",+ "Killing {name} left running by a previous Jan: pid {pid}, parent {:?}",process.parent());if process.kill() {killed += 1;} else {- log::warn!("Could not kill orphaned llama-server pid {pid}");+ log::warn!("Could not kill orphaned {name} pid {pid}");}}killed@@ -254,6 +323,7 @@ mod tests {parent,name: name.to_string(),start_time: 0,+ jan_launched: false,cmd: cmd.iter().map(|s| s.to_string()).collect(),}}@@ -530,4 +600,84 @@ mod tests {];assert_eq!(sweep(&procs), vec![200]);}++ /// An `mlx-server` carrying the marker Jan sets when it starts one.+ fn mlx_server(pid: u32, parent: Option<u32>) -> ProcInfo {+ let mut server = proc(+ pid,+ parent,+ "mlx-server",+ &[+ "/Applications/Jan.app/Contents/Resources/resources/bin/mlx-server",+ "--model",+ "/Users/u/models/qwen",+ "--port",+ "52113",+ "--model-id",+ "qwen",+ ],+ );+ server.jan_launched = true;+ server+ }++ #[test]+ fn an_mlx_server_adopted_by_init_is_swept() {+ assert_eq!(sweep(&[mlx_server(200, Some(1))]), vec![200]);+ }++ #[test]+ fn an_mlx_server_whose_parent_is_gone_is_swept() {+ assert_eq!(sweep(&[mlx_server(200, Some(999))]), vec![200]);+ }++ #[test]+ fn an_mlx_server_whose_jan_is_still_running_is_left_alone() {+ let procs = [+ proc(100, Some(1), "Jan-Desktop", &["Jan"]),+ mlx_server(200, Some(100)),+ ];+ assert!(sweep(&procs).is_empty());+ }++ #[test]+ fn an_mlx_server_under_a_live_shell_is_left_alone() {+ // Run by hand from a terminal: its parent is alive and is not a Jan.+ for shell in ["zsh", "bash", "tmux: server"] {+ let procs = [+ proc(100, Some(1), shell, &[shell]),+ mlx_server(200, Some(100)),+ ];+ assert!(sweep(&procs).is_empty(), "{shell}");+ }+ }++ #[test]+ fn a_lookalike_mlx_server_without_jans_marker_is_left_alone_even_when_orphaned() {+ // Run by hand, e.g. `/Users/u/tools/resources/bin/mlx-server ...`, whose+ // shell has exited so launchd owns it. Path, flags and parent all fit;+ // only the marker Jan sets when it starts the server is missing.+ let theirs = proc(+ 200,+ Some(1),+ "mlx-server",+ &[+ "/Users/u/tools/resources/bin/mlx-server",+ "--model",+ "/path/to/model",+ "--port",+ "8080",+ "--model-id",+ "custom",+ ],+ );+ assert!(sweep(&[theirs]).is_empty());+ }++ #[test]+ fn a_process_with_jans_marker_but_another_name_is_left_alone() {+ let mut other = mlx_server(200, Some(1));+ other.name = "python3".to_string();+ assert!(sweep(&[other]).is_empty());+ }}diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex dd9efe0df..952b528d2 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -373,8 +373,9 @@ pub fn run() {app.handle().plugin(tauri_plugin_updater::Builder::new().build())?;- // A Jan up to 0.8.4 leaves its llama-server router running across an- // in-app update; that version cannot be fixed, so reap it here.+ // Reap engines a previous Jan left running: the llama-server router of+ // a 0.8.4 that was updated in place, or an mlx-server whose app was+ // killed. Neither can be stopped by the version that started it.#[cfg(not(any(target_os = "ios", target_os = "android")))]{let data_folder = get_jan_data_folder_path(app.handle().clone());@@ -386,6 +387,25 @@ pub fn run() {});}+ // A SIGTERM ends the process without RunEvent::Exit, so the exit+ // cleanup never ran and mlx-server, which does not watch its+ // parent, was left running. Turn the signal into a normal exit.+ #[cfg(target_os = "macos")]+ {+ let handle = app.handle().clone();+ tauri::async_runtime::spawn(async move {+ use tokio::signal::unix::{signal, SignalKind};+ let Ok(mut term) = signal(SignalKind::terminate()) else {+ log::warn!("Could not listen for SIGTERM");+ return;+ };+ term.recv().await;+ log::info!("Received SIGTERM; shutting down");+ SHUTTING_DOWN.store(true, std::sync::atomic::Ordering::SeqCst);+ handle.exit(0);+ });+ }+// Start migrationlet mut store_path = get_jan_data_folder_path(app.handle().clone());store_path.push("store.json");diff --git a/src-tauri/utils/src/system.rs b/src-tauri/utils/src/system.rsindex 974ad2da8..096559238 100644--- a/src-tauri/utils/src/system.rs+++ b/src-tauri/utils/src/system.rs@@ -1,5 +1,10 @@use std::path::Path;+/// Environment variable Jan sets on every `mlx-server` it starts. The startup+/// sweep stops an orphaned `mlx-server` only when it carries this, because the+/// server's name, path and flags can all belong to one someone runs themselves.+pub const MLX_OWNER_ENV: &str = "JAN_MLX_OWNER";+/// Returns true when the current process is running inside a Flatpak sandboxpub fn is_flatpak() -> bool {Path::new("/.flatpak-info").exists()