AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
node.lock: record the holder pid in an unlocked note (#2431) (#2820)
xerj-orge5d994b74d78
diff --git a/engine/crates/xerj-common/src/nodedir.rs b/engine/crates/xerj-common/src/nodedir.rsindex 0251d228d..508bdb8a0 100644--- a/engine/crates/xerj-common/src/nodedir.rs+++ b/engine/crates/xerj-common/src/nodedir.rs@@ -172,7 +172,16 @@ pub fn node_lock_hint(} else {format!(" --port {port}")};- let pid: u32 = std::fs::read_to_string(&lock).ok()?.trim().parse().ok()?;+ // A running node on Windows holds `node.lock` under `LockFileEx`, which+ // hides its bytes from other processes. The engine writes the same PID+ // to the unlocked `node.lock.pid` beside it (#2431); read that only when+ // the lock file exists but cannot be read.+ let text = match std::fs::read_to_string(&lock) {+ Ok(text) => text,+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return None,+ Err(_) => std::fs::read_to_string(data_dir.join("node.lock.pid")).ok()?,+ };+ let pid: u32 = text.trim().parse().ok()?;let dir = data_dir.display();match probe(pid) {PidState::Dead => Some(format!(@@ -196,6 +205,43 @@ pub fn node_lock_hint(mod tests {use super::*;+ /// #2431: on Windows the held `node.lock` cannot be read; the hint comes+ /// from the unlocked PID note instead. A directory named `node.lock`+ /// stands in for the unreadable file on every platform.+ #[test]+ fn an_unreadable_node_lock_falls_back_to_the_pid_note() {+ let data = tempfile::tempdir().unwrap();+ std::fs::create_dir(data.path().join("node.lock")).unwrap();+ assert!(node_lock_hint(data.path(), 9200, &|_| PidState::Alive).is_none());+ std::fs::write(data.path().join("node.lock.pid"), "0000004343\n").unwrap();+ let hint = node_lock_hint(data.path(), 9200, &|pid| {+ assert_eq!(pid, 4343);+ PidState::Alive+ })+ .expect("hint from the note");+ assert!(hint.contains("Process 4343 still holds"), "{hint}");+ }++ #[test]+ fn a_missing_node_lock_ignores_a_leftover_pid_note() {+ let data = tempfile::tempdir().unwrap();+ std::fs::write(data.path().join("node.lock.pid"), "4343").unwrap();+ assert!(node_lock_hint(data.path(), 9200, &|_| PidState::Dead).is_none());+ }++ #[test]+ fn a_readable_node_lock_wins_over_the_pid_note() {+ let data = tempfile::tempdir().unwrap();+ std::fs::write(data.path().join("node.lock"), "4242").unwrap();+ std::fs::write(data.path().join("node.lock.pid"), "9999").unwrap();+ let hint = node_lock_hint(data.path(), 9200, &|pid| {+ assert_eq!(pid, 4242);+ PidState::Dead+ })+ .expect("hint");+ assert!(hint.contains("pid 4242"), "{hint}");+ }+#[test]fn a_dead_pid_in_the_published_dir_names_the_dir_and_the_restart() {let home = tempfile::tempdir().unwrap();diff --git a/engine/crates/xerj-engine/src/node_lock.rs b/engine/crates/xerj-engine/src/node_lock.rsindex 39db280c1..d3324f6ee 100644--- a/engine/crates/xerj-engine/src/node_lock.rs+++ b/engine/crates/xerj-engine/src/node_lock.rs@@ -21,6 +21,16 @@//! A live PID, or a PID that cannot be checked, still fails the second//! start. The recorded PID is a diagnostic; a running holder is excluded by//! the OS lock, not by the bytes in the file.+//!+//! The holder also writes its PID to `<data_dir>/node.lock.pid`, a plain+//! file it never locks (#2431). `LockFileEx` locks the whole of `node.lock`,+//! so on Windows a second process cannot read the PID inside it: the refusal+//! said "pid unknown" and the dead-holder reclaim above could never run.+//! The note is read only when reading `node.lock` itself fails. Only a+//! process that has just taken the OS lock writes it, so it names the+//! current holder once that holder has stamped, or the last one. The window+//! between taking the lock and writing the note is the one+//! `STALE_CONFIRM` already covers for the PID inside the lock file.use std::fs::{self, File, OpenOptions, TryLockError};use std::io::{self, Read, Seek, SeekFrom, Write};@@ -33,6 +43,9 @@ use crate::{EngineError, Result};const LOCK_NAME: &str = "node.lock";+/// Unlocked copy of the holder's PID, readable while `node.lock` is locked.+const PID_NOTE_NAME: &str = "node.lock.pid";+/// How many times [`acquire`] will replace a stale lock file before giving up.const ACQUIRE_ATTEMPTS: u32 = 4;@@ -133,6 +146,12 @@ pub(crate) fn acquire(data_dir: &Path) -> Result<NodeLock> {drop(file);continue;}+ write_pid_note(&lock_path).map_err(|e| {+ config_err(format!(+ "failed to record the holder pid in '{}': {e}",+ lock_path.with_file_name(PID_NOTE_NAME).display()+ ))+ })?;if let Some(pid) = reclaimed_from.or_else(|| dead_other(previous)) {log_reclaim(&lock_path, pid);}@@ -334,9 +353,63 @@ fn reclaim_dest(path: &Path, pid: u32) -> PathBuf {))}+/// Write this process's PID to the unlocked note beside `lock_path`.+///+/// Staged through a per-process temp file and renamed over the note, so a+/// reader sees the previous PID or this one, never half a line.+fn write_pid_note(lock_path: &Path) -> io::Result<()> {+ let note = lock_path.with_file_name(PID_NOTE_NAME);+ let pid = std::process::id();+ let tmp = lock_path.with_file_name(format!("{PID_NOTE_NAME}.tmp-{pid}"));+ let record = format!("{pid}\n");+ if stage_and_rename(&tmp, ¬e, record.as_bytes()).is_err() {+ let _ = fs::remove_file(&tmp);+ // A rename can fail on Windows while a reader has the note open.+ // Writing in place is still correct: readers parse one line.+ fs::write(¬e, record.as_bytes())?;+ }+ Ok(())+}++fn stage_and_rename(tmp: &Path, dest: &Path, bytes: &[u8]) -> io::Result<()> {+ let mut out = File::create(tmp)?;+ out.write_all(bytes)?;+ out.sync_all()?;+ drop(out);+ fs::rename(tmp, dest)+}+fn read_holder(path: &Path) -> Option<u32> {- let file = File::open(path).ok()?;- read_pid_from_file(&file)+ holder_from(+ File::open(path).and_then(|file| read_lock_bytes(&file)),+ || read_pid_note(path),+ )+}++/// The PID in `node.lock` when its bytes can be read; the note otherwise.+///+/// A read that succeeds but holds no PID (an empty or foreign file) stays+/// unknown: the note is the answer only to a lock that hides its bytes.+fn holder_from(lock_bytes: io::Result<String>, note: impl FnOnce() -> Option<u32>) -> Option<u32> {+ match lock_bytes {+ Ok(text) => parse_pid(&text),+ // No lock file: there is no holder for the note to describe.+ Err(e) if e.kind() == io::ErrorKind::NotFound => None,+ Err(_) => note(),+ }+}++fn read_lock_bytes(file: &File) -> io::Result<String> {+ let mut cloned = file.try_clone()?;+ cloned.seek(SeekFrom::Start(0))?;+ let mut buf = [0_u8; 64];+ let n = cloned.read(&mut buf)?;+ Ok(String::from_utf8_lossy(&buf[..n]).into_owned())+}++fn read_pid_note(lock_path: &Path) -> Option<u32> {+ let text = fs::read_to_string(lock_path.with_file_name(PID_NOTE_NAME)).ok()?;+ parse_pid(&text)}fn read_pid_from_file(file: &File) -> Option<u32> {@@ -722,6 +795,86 @@ mod tests {assert!(err.contains("unknown"), "{err}");}+ /// #2431: the holder's PID must be readable while `node.lock` is locked.+ #[test]+ fn acquire_records_the_holder_pid_in_an_unlocked_note() {+ let _guard = serialize_tests();+ let dir = tempfile::tempdir().unwrap();+ let held = acquire(dir.path()).unwrap();+ let note = fs::read_to_string(dir.path().join(PID_NOTE_NAME)).unwrap();+ assert_eq!(parse_pid(¬e), Some(std::process::id()), "{note:?}");+ let names: Vec<_> = fs::read_dir(dir.path())+ .unwrap()+ .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())+ .collect();+ assert!(+ !names.iter().any(|n| n.contains(".tmp-")),+ "the staged note must be renamed into place: {names:?}"+ );+ drop(held);+ }++ #[test]+ fn the_note_is_read_only_when_the_lock_bytes_cannot_be_read() {+ let hidden = || Err(io::Error::other("lock violation"));+ assert_eq!(holder_from(hidden(), || Some(77)), Some(77));+ assert_eq!(holder_from(hidden(), || None), None);+ let missing = Err(io::Error::from(io::ErrorKind::NotFound));+ assert_eq!(holder_from(missing, || panic!("note read")), None);+ // Readable bytes win; the note is not consulted.+ assert_eq!(+ holder_from(Ok("0000000042\n".into()), || panic!("note read")),+ Some(42)+ );+ assert_eq!(holder_from(Ok("\n".into()), || panic!("note read")), None);+ }++ /// The Windows shape of #2431: a stuck lock left by a dead node. Unix+ /// reads the PID from `node.lock`; Windows cannot, and reads the note.+ /// Both must reclaim.+ #[test]+ fn a_busy_lock_left_by_a_dead_pid_is_reclaimed_with_the_note_present() {+ let _guard = serialize_tests();+ let dir = tempfile::tempdir().unwrap();+ let dead = reaped_pid();+ let path = dir.path().join(LOCK_NAME);+ fs::write(&path, format!("{dead}\n")).unwrap();+ fs::write(dir.path().join(PID_NOTE_NAME), format!("{dead}\n")).unwrap();+ let holder = hold(&path);+ assert_eq!(read_holder(&path), Some(dead));++ let logs = capture_warns(|| {+ let file = acquire(dir.path()).expect("a dead holder is reclaimed");+ assert_eq!(read_holder(&path), Some(std::process::id()));+ let note = fs::read_to_string(dir.path().join(PID_NOTE_NAME)).unwrap();+ assert_eq!(parse_pid(¬e), Some(std::process::id()));+ drop(file);+ });+ assert!(+ logs.contains(&format!("reclaimed stale node.lock left by pid {dead}")),+ "reclaim must be logged: {logs}"+ );+ drop(holder);+ }++ /// Windows only: the lock hides its bytes, and the refusal must name the+ /// live holder from the note instead of "pid unknown".+ #[cfg(windows)]+ #[test]+ fn a_hidden_busy_lock_names_the_live_holder_from_the_note() {+ let _guard = serialize_tests();+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join(LOCK_NAME);+ fs::write(&path, b"\n").unwrap();+ let live = std::process::id();+ fs::write(dir.path().join(PID_NOTE_NAME), format!("{live}\n")).unwrap();+ let _holder = hold(&path);+ let err = acquire(dir.path())+ .expect_err("a live holder blocks")+ .to_string();+ assert!(err.contains(&live.to_string()), "{err}");+ }+#[test]fn data_dir_lock_held_ignores_a_dead_pid_and_honors_a_live_one() {let _guard = serialize_tests();