Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

chore(release): merge release/v0.8.8 back into main

James Damf1c5d612e9ea
diff --cc src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs
index 857346be3,7bd3054cf..ac49e31b9
--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs
@@@ -596,29 -448,32 +596,53 @@@ impl WorkerHandle
pub fn exited(&mut self) -> Option<std::process::ExitStatus> {
self.child.try_wait().ok().flatten()
}
+
+ /// None while still running, otherwise how it ended and what it last said.
+ ///
+ /// Waits up to [`STDERR_DRAIN_WAIT`] for the rest of its stderr first, so
+ /// a crash reason written just before the exit is not missed.
+ pub async fn exit_report(&mut self) -> Option<ExitReport> {
+ let status = self.exited()?;
+ wait_for_drain(self.stderr_drain.take()).await;
+ Some(ExitReport {
+ status: describe_exit(&status),
+ last_lines: stderr_lines(&self.stderr_tail),
+ })
+ }
+}
+
+/// Gives a dead worker's stderr drain a bounded moment to read what is left.
+/// On timeout the task is detached, not cancelled, and keeps logging.
+async fn wait_for_drain(drain: Option<tokio::task::JoinHandle<()>>) {
+ if let Some(drain) = drain {
+ let _ = tokio::time::timeout(STDERR_DRAIN_WAIT, drain).await;
+ }
}
+ /// The working directory every worker spawn pins.
+ ///
+ /// ggml's backend scan puts `fs::current_path()` in its search paths and
+ /// calls dl_load_library on every `ggml-*` filename match *before* reading
+ /// its score, so a planted module in an inherited cwd runs its DllMain /
+ /// constructor even if it is never selected -- and `best_score` is one
+ /// accumulator across all search paths, so a higher-scoring plant wins
+ /// outright rather than losing to path order. Pinning the cwd to the
+ /// worker's own directory, which is where the bundled modules already live,
+ /// removes the attacker-writable entry from that list.
+ ///
+ /// Absolute only: `current_dir` also changes how the OS resolves a *relative*
+ /// program path, so doing this unconditionally would make a dev's
+ /// `--bin build/jan-llama-worker` be looked up inside `build/`. Every shipped
+ /// path is absolute (`resource_dir()`, or `sidecar_path` off
+ /// `current_exe`), so the hardening lands exactly where it matters.
+ pub(crate) fn worker_cwd(exe: &Path) -> Option<&Path> {
+ if exe.is_absolute() {
+ exe.parent()
+ } else {
+ None
+ }
+ }
+
/// Spawns the worker and waits for its handshake line.
#[allow(clippy::too_many_arguments)]
pub async fn spawn(
@@@ -817,9 -638,17 +825,17 @@@ pub fn sidecar_path() -> Option<PathBuf
}
#[cfg(test)]
-mod tests {
+pub(crate) mod tests {
use super::*;
+ #[test]
+ fn worker_cwd_is_the_exe_dir_only_for_absolute_paths() {
+ let exe = std::env::temp_dir().join("bin").join("jan-llama-worker");
+ assert_eq!(worker_cwd(&exe), exe.parent());
+ assert!(worker_cwd(Path::new("build/jan-llama-worker")).is_none());
+ assert!(worker_cwd(Path::new("jan-llama-worker")).is_none());
+ }
+
// Restored with the marker lists intact: each entry came from a real crash
// report, and a fault the classifier misses is a silent hang for the user.
#[test]