Skip to content

armgit / xerj

Public Imported from GitHub
0

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.

fix(server): say how to move when the listen port is taken (#1687)

xerj-org10195d5af346
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rs
index 6571e1a57..71d1636c0 100644
--- a/engine/crates/xerj-server/src/main.rs
+++ b/engine/crates/xerj-server/src/main.rs
@@ -1277,9 +1277,27 @@ async fn serve(
/// can never advertise a port another process owns — the failure mode that
/// sent one user's contracts, invoices and bank export into a stranger's data
/// directory under `ok=true exit=0` (issue #465).
+/// What a bare `xerj` prints when the default port is taken.
+///
+/// The old context was `ES-compat: bind 127.0.0.1:9200` plus the OS error.
+/// A bare `xerj` starts the server. When that port is taken, the error did
+/// not mention `--port`, `--auto-port`, or that `--help` lists subcommands.
+fn addr_in_use_message(name: &str, addr: SocketAddr) -> String {
+ format!(
+ "{name}: {addr} is already in use. Pass --port <PORT> to pick a different one, or \
+ --auto-port to take the next free port. If you did not mean to start a server, \
+ `xerj --help` lists the subcommands."
+ )
+}
+
fn bind_listener(addr: SocketAddr, name: &str) -> Result<std::net::TcpListener> {
- let listener =
- std::net::TcpListener::bind(addr).with_context(|| format!("{name}: bind {addr}"))?;
+ let listener = match std::net::TcpListener::bind(addr) {
+ Ok(listener) => listener,
+ Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {
+ anyhow::bail!("{}", addr_in_use_message(name, addr));
+ }
+ Err(e) => return Err(e).with_context(|| format!("{name}: bind {addr}")),
+ };
// Both axum's accept loop and axum-server's rustls acceptor drive this
// listener from the reactor; a blocking fd would stall the runtime thread
// on the first accept.
@@ -1289,6 +1307,24 @@ fn bind_listener(addr: SocketAddr, name: &str) -> Result<std::net::TcpListener>
Ok(listener)
}
+#[cfg(test)]
+mod bind_listener_tests {
+ use super::{addr_in_use_message, bind_listener};
+
+ #[test]
+ fn a_taken_port_names_port_and_auto_port() {
+ let held = std::net::TcpListener::bind("127.0.0.1:0").expect("bind a free port");
+ let addr = held.local_addr().expect("read the held address");
+ let err = bind_listener(addr, "ES-compat").expect_err("the port is held");
+ let msg = format!("{err:#}");
+ assert!(msg.contains("--port"), "{msg}");
+ assert!(msg.contains("--auto-port"), "{msg}");
+ assert!(msg.contains("xerj --help"), "{msg}");
+ assert!(msg.contains(&addr.to_string()), "{msg}");
+ assert_eq!(msg, addr_in_use_message("ES-compat", addr));
+ }
+}
+
/// If `data_dir` is currently locked by another live process, find the first
/// sibling directory (`<dir>-2`, `<dir>-3`, ...) that is not, so multiple
/// opted-in (`--auto-port`) instances can run side by side.