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
fix(cli): `xerj … | head` dumped core instead of exiting on SIGPIPE
xerj-orga2a2e3f18304
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rsindex 26101e73a..fc316f1c2 100644--- a/engine/crates/xerj-server/src/main.rs+++ b/engine/crates/xerj-server/src/main.rs@@ -1305,6 +1305,32 @@ fn raise_nofile_limit() {}}+/// Restore the default SIGPIPE disposition, which the Rust runtime replaces+/// with `SIG_IGN` before `main` runs. Under `SIG_IGN` a write to a pipe whose+/// reader has gone returns `EPIPE`, `println!` turns that into a panic+/// ("failed printing to stdout: Broken pipe"), and the release profile's+/// `panic = "abort"` turns THAT into a core dump — so `xerj autoindex map |+/// head -80`, and every other `xerj … | head`, dumped core and lost the+/// command's real exit status. With `SIG_DFL` the kernel terminates the+/// process on the signal instead: no panic, no core, and the conventional+/// 128+13 status that shells report as 141.+///+/// Process-global, so it reaches the server too. Its client sockets are NOT+/// exposed: `std` writes those with `MSG_NOSIGNAL` (`SO_NOSIGPIPE` on macOS),+/// so a client that disconnects mid-response still surfaces as `EPIPE` and+/// cannot signal us. Its own stdout does change — the startup banner no longer+/// aborts when the pipe has no reader, but tracing output, which discards+/// write errors, no longer keeps a server alive past a vanished stdout either:+/// the next log line terminates it. Supervised deployments point stdout at a+/// file or `/dev/null` (`brain`'s boot path included), so in practice this+/// only reaches `xerj | reader-that-leaves`.+#[cfg(unix)]+fn restore_default_sigpipe() {+ unsafe {+ libc::signal(libc::SIGPIPE, libc::SIG_DFL);+ }+}+/// The ordered `(rlim_cur, rlim_max)` pairs to try for RLIMIT_NOFILE, highest/// first. Pure, so the macOS-only `RLIM_INFINITY` path is unit-testable without/// a Mac. Every pair uses `rlim_max == rlim_cur` — a CONCRETE value: macOS@@ -1370,6 +1396,11 @@ mod nofile_tests {}fn main() -> Result<()> {+ // Before anything can write to stdout, including the `--help`/`--version`+ // fast paths inside `parse_args`.+ #[cfg(unix)]+ restore_default_sigpipe();+#[cfg(unix)]raise_nofile_limit();diff --git a/engine/crates/xerj-server/tests/sigpipe.rs b/engine/crates/xerj-server/tests/sigpipe.rsnew file mode 100644index 000000000..f1aa4e064--- /dev/null+++ b/engine/crates/xerj-server/tests/sigpipe.rs@@ -0,0 +1,102 @@+//! `xerj … | head` must not dump core.+//!+//! The Rust runtime installs `SIG_IGN` for SIGPIPE before `main`, so a write to+//! a pipe whose reader has left returns `EPIPE`, `println!` panics on it+//! ("failed printing to stdout"), and the release profile's `panic = "abort"`+//! turns that panic into a core dump — the reported `xerj autoindex map |+//! head -80` failure, which also swallowed the command's real exit status.+//! `main` restores `SIG_DFL` so the kernel terminates us on the signal instead.+//!+//! Both tests drive `--help`: it is the only long stdout-only path that needs+//! neither a running server nor a fixture, and it prints through the same+//! `println!` that panicked.++#![cfg(unix)]++use std::io::Read;+use std::os::fd::AsRawFd;+use std::os::unix::process::ExitStatusExt;+use std::process::{Command, ExitStatus, Stdio};++fn assert_died_quietly_on_sigpipe(status: ExitStatus, stderr: &[u8]) {+ let stderr = String::from_utf8_lossy(stderr);+ assert!(+ !stderr.contains("panicked"),+ "a closed stdout must not panic; stderr was: {stderr}"+ );+ assert!(+ !status.core_dumped(),+ "a closed stdout must not dump core; status was {status:?}"+ );+ assert_eq!(+ status.signal(),+ Some(libc::SIGPIPE),+ "expected termination by SIGPIPE (shell status 141), got {status:?}; stderr: {stderr}"+ );+}++/// The reader is dropped *before* the child is spawned, so there is no window+/// in which the write could have succeeded and no timing assumption at all.+#[test]+fn stdout_pipe_without_a_reader_dies_on_sigpipe_instead_of_aborting() {+ let (reader, writer) = std::io::pipe().expect("create pipe");+ drop(reader);++ let child = Command::new(env!("CARGO_BIN_EXE_xerj"))+ .arg("--help")+ .stdout(Stdio::from(writer))+ .stderr(Stdio::piped())+ .spawn()+ .expect("spawn xerj --help");+ let out = child.wait_with_output().expect("wait for xerj --help");++ assert_died_quietly_on_sigpipe(out.status, &out.stderr);+}++/// The reported shape: a reader that consumes a prefix and then leaves, as+/// `head -80` does. Linux-only because it needs `F_SETPIPE_SZ` to shrink the+/// pipe below the help text — that is what guarantees the child is still+/// mid-write when the reader goes, rather than having already buffered+/// everything into a 64 KiB pipe and exited 0.+#[cfg(target_os = "linux")]+#[test]+fn reader_leaving_mid_stream_dies_on_sigpipe_instead_of_aborting() {+ const PEEK: usize = 8;++ let full = Command::new(env!("CARGO_BIN_EXE_xerj"))+ .arg("--help")+ .output()+ .expect("run xerj --help");+ let (mut reader, writer) = std::io::pipe().expect("create pipe");+ // Shrinking bottoms out at one page; the kernel reports what it did set.+ let capacity = unsafe { libc::fcntl(writer.as_raw_fd(), libc::F_SETPIPE_SZ, 4096) };+ assert!(+ capacity > 0,+ "F_SETPIPE_SZ failed: {}",+ std::io::Error::last_os_error()+ );+ // Not a flake when it trips: the child must still owe bytes after we have+ // read PEEK of them, or it finishes cleanly and proves nothing.+ assert!(+ full.stdout.len() > capacity as usize + PEEK,+ "`xerj --help` is {} bytes, which no longer exceeds the {capacity}-byte pipe — \+ drive this test with a longer-output subcommand",+ full.stdout.len()+ );++ let child = Command::new(env!("CARGO_BIN_EXE_xerj"))+ .arg("--help")+ .stdout(Stdio::from(writer))+ .stderr(Stdio::piped())+ .spawn()+ .expect("spawn xerj --help");+ let mut prefix = [0u8; PEEK];+ reader+ .read_exact(&mut prefix)+ .expect("read the head of the stream");+ drop(reader);++ let out = child.wait_with_output().expect("wait for xerj --help");++ assert_died_quietly_on_sigpipe(out.status, &out.stderr);+}