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): compose the cluster listen address from the parsed bind IP too

xerj-org430254c77240
diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rs
index f06b51fd0..4a960b026 100644
--- a/engine/crates/xerj-server/src/main.rs
+++ b/engine/crates/xerj-server/src/main.rs
@@ -1811,6 +1811,12 @@ async fn async_main() -> Result<()> {
// 8. Engine (opens existing indices from disk)
let engine = Engine::new(cfg.clone()).context("initialise engine")?;
+ // This node's own cluster address, composed from the parsed bind IP.
+ // Needed twice — by the transport (8b) and by the console's node identity
+ // (9) — so it is derived once and the two cannot disagree. `None` only
+ // when `bind_address` is not an IP literal, which 8b refuses.
+ let cluster_listen_addr = cfg.socket_addr(cfg.cluster.port);
+
// 8b. Cluster runner (if cluster mode is enabled)
let _cluster_shutdown = if cfg.cluster.enabled {
// Fail closed (issue #75). The cluster port carries Raft control
@@ -1846,12 +1852,18 @@ async fn async_main() -> Result<()> {
}
}
- // Derive this node's listen address from the server bind address + cluster port.
- let node_id = format!("{}:{}", cfg.server.bind_address, cfg.cluster.port);
- let listen_addr: std::net::SocketAddr =
- format!("{}:{}", cfg.server.bind_address, cfg.cluster.port)
- .parse()
- .context("parse cluster listen address")?;
+ // Derive this node's listen address from the server bind address +
+ // cluster port — from the *parsed* IP, never by formatting
+ // `"{bind}:{port}"`. That construction is the one that made
+ // `bind_address = "::1"` unbootable for the data listeners (see 11);
+ // it survived here because cluster mode is off by default, and it
+ // failed later and worse — `Error: parse cluster listen address` after
+ // the data directory and the first-run admin key already existed.
+ let listen_addr = cluster_listen_addr.with_context(|| bad_bind_address(&cfg))?;
+ // The node identifies itself by that address, so an IPv6 node is
+ // `[::1]:9300` — the bracketed form peer entries are parsed from
+ // (`SocketAddr::parse`, just above), not the unparseable `::1:9300`.
+ let node_id = listen_addr.to_string();
let tick = std::time::Duration::from_millis(cfg.cluster.tick_ms);
@@ -1906,10 +1918,13 @@ async fn async_main() -> Result<()> {
.await
.context("xerj-console bootstrap")?;
- let xerj_console_node_id: String = if cfg.cluster.enabled {
- format!("{}:{}", cfg.server.bind_address, cfg.cluster.port)
- } else {
- "local".to_string()
+ let xerj_console_node_id: String = match (cfg.cluster.enabled, cluster_listen_addr) {
+ // The same string the cluster transport registered as this node's id.
+ (true, Some(addr)) => addr.to_string(),
+ // Unreachable: 8b refuses a cluster bind that has no socket address.
+ // Report the setting as written rather than invent an identity.
+ (true, None) => format!("{}:{}", cfg.server.bind_address, cfg.cluster.port),
+ (false, _) => "local".to_string(),
};
let xerj_console_cluster_mode = if cfg.cluster.enabled {
ClusterMode::Raft
diff --git a/engine/crates/xerj-server/tests/cleartext_bind_fail_closed.rs b/engine/crates/xerj-server/tests/cleartext_bind_fail_closed.rs
index fa9e1d70a..02d7aef14 100644
--- a/engine/crates/xerj-server/tests/cleartext_bind_fail_closed.rs
+++ b/engine/crates/xerj-server/tests/cleartext_bind_fail_closed.rs
@@ -26,14 +26,20 @@ use std::time::{Duration, Instant};
/// cleanly and keeps running instead of dying on an unrelated bind error,
/// which would masquerade as a pass.
fn three_free_ports() -> (u16, u16, u16) {
- let l1 = TcpListener::bind("127.0.0.1:0").unwrap();
- let l2 = TcpListener::bind("127.0.0.1:0").unwrap();
- let l3 = TcpListener::bind("127.0.0.1:0").unwrap();
- (
- l1.local_addr().unwrap().port(),
- l2.local_addr().unwrap().port(),
- l3.local_addr().unwrap().port(),
- )
+ let (a, b, c, _) = four_free_ports();
+ (a, b, c)
+}
+
+/// As above plus a fourth for the cluster transport.
+fn four_free_ports() -> (u16, u16, u16, u16) {
+ let held: Vec<TcpListener> = (0..4)
+ .map(|_| TcpListener::bind("127.0.0.1:0").unwrap())
+ .collect();
+ let p: Vec<u16> = held
+ .iter()
+ .map(|l| l.local_addr().unwrap().port())
+ .collect();
+ (p[0], p[1], p[2], p[3])
}
/// TOML-safe rendering of a temp path (Windows backslashes would be escape
@@ -411,3 +417,88 @@ data_dir = "{data}"
reached the config; stderr:\n{stderr}"
);
}
+
+/// The cluster transport must be reachable by the same IPv6 loopback spelling
+/// the data listeners accept.
+///
+/// `bind_address = "::1"` is a spelling operators reach for now that the
+/// default is loopback (#228), and the cluster listen address was the last
+/// place composing one by `format!("{bind}:{port}")` — `"::1:9300"` is not a
+/// socket address. The failure was worse than the data listeners': it lands at
+/// step 8b, *after* the data directory exists and a first-run admin key has
+/// been minted and printed, so the node leaves state behind and still cannot
+/// start.
+///
+/// Alive is the assertion. A regressed binary exits with
+/// `Error: parse cluster listen address`; the fixed one keeps running. If the
+/// environment has no usable IPv6 transport the binary logs the degraded
+/// single-node fallback and stays up — which still proves the address parsed,
+/// the only thing under test here.
+#[test]
+fn ipv6_loopback_bind_starts_the_cluster_transport() {
+ let dir = tempfile::tempdir().unwrap();
+ let (rest, grpc, es, cluster) = four_free_ports();
+ let config_path = dir.path().join("xerj.toml");
+ std::fs::write(
+ &config_path,
+ format!(
+ r#"
+[server]
+bind_address = "::1"
+rest_port = {rest}
+grpc_port = {grpc}
+es_compat_port = {es}
+data_dir = "{data}"
+
+[cluster]
+enabled = true
+port = {cluster}
+auth_secret = "0123456789abcdef0123456789abcdef"
+"#,
+ data = toml_path(&dir.path().join("data")),
+ ),
+ )
+ .unwrap();
+
+ let mut child = Command::new(env!("CARGO_BIN_EXE_xerj"))
+ .arg("--config")
+ .arg(&config_path)
+ .env("XERJ_LOG", "info")
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .spawn()
+ .expect("spawn xerj");
+
+ // Long enough for engine init plus step 8b on a slow runner; the regressed
+ // binary dies well before this and the loop breaks out early.
+ let alive_until = Instant::now() + Duration::from_secs(20);
+ let mut exited = None;
+ while Instant::now() < alive_until {
+ if let Some(status) = child.try_wait().expect("poll child") {
+ exited = Some(status);
+ break;
+ }
+ std::thread::sleep(Duration::from_millis(200));
+ }
+ child.kill().ok();
+ child.wait().ok();
+
+ let mut stderr = String::new();
+ child
+ .stderr
+ .take()
+ .expect("stderr piped")
+ .read_to_string(&mut stderr)
+ .expect("read child stderr");
+
+ assert!(
+ !stderr.contains("parse cluster listen address"),
+ "the cluster listen address must be composed from the parsed bind IP, \
+ not from \"{{bind}}:{{port}}\"; stderr:\n{stderr}"
+ );
+ assert!(
+ exited.is_none(),
+ "bind_address = \"::1\" with cluster mode on must start; it exited \
+ with {exited:?}. stderr:\n{stderr}"
+ );
+}