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(mcp): give xerj_code_search a request deadline (#2366)

Ahmet Adaklı59197190ec8b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 84516d17b..e23a41daa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `xerj mcp`: `xerj_code_search` now gives up on a node that does not
+ answer. Its requests went through a fresh HTTP client without the 30 s
+ deadline every other tool uses, so a node that accepted the connection
+ and then stalled held the call forever, and with it every later request
+ on the stdio connection. The call now fails with an error after 30 s,
+ like the other tools.
+
- `SpawnedThread` is `Send` and `Sync` on musl. `libc::pthread_t` is
`*mut c_void` there and an integer on glibc, so the handle was `!Send`
only on musl. `MemoryShardRpc::call` holds it across `.await`, and
diff --git a/engine/crates/xerj-mcp/src/lib.rs b/engine/crates/xerj-mcp/src/lib.rs
index f7c2c979c..b41096283 100644
--- a/engine/crates/xerj-mcp/src/lib.rs
+++ b/engine/crates/xerj-mcp/src/lib.rs
@@ -125,6 +125,8 @@ const DEFAULT_PROTOCOL_VERSION: &str = "2025-06-18";
const DEFAULT_SEARCH_SIZE: usize = 10;
/// Default XERJ ES-compatible endpoint.
const DEFAULT_XERJ_URL: &str = "http://localhost:9200";
+/// Deadline for one proxied request to the node, connect to last body byte.
+const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
/// How to start the node `xerj mcp` proxies to. The flags are the local-dev
/// start in llms.txt ("Start here", step 1), the README, and the `xerj mcp`
@@ -369,7 +371,7 @@ pub async fn run(args: &[String]) -> anyhow::Result<()> {
resolve_auth(auth_override, std::env::var("XERJ_AUTH").ok(), &base_url);
let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
+ .timeout(REQUEST_TIMEOUT)
.build()?;
let ctx = Ctx {
@@ -2710,6 +2712,10 @@ fn build_brain_overview(args: &Value) -> Result<BuiltRequest, String> {
struct McpXc {
base_url: String,
auth: Option<String>,
+ /// `Ctx.client`'s deadline does not reach this fresh client; without one a
+ /// node that accepts and never answers holds the tool call (and the stdio
+ /// loop behind it) forever.
+ timeout: Duration,
}
impl McpXc {
@@ -2720,10 +2726,14 @@ impl McpXc {
.map_err(|e| format!("runtime: {e}"))?;
let url = format!("{}{}", self.base_url, path);
let auth = self.auth.clone();
+ let timeout = self.timeout;
let method = method.to_string();
let body = body.cloned();
rt.block_on(async move {
- let client = reqwest::Client::new();
+ let client = reqwest::Client::builder()
+ .timeout(timeout)
+ .build()
+ .map_err(|e| format!("http client: {e}"))?;
let m = reqwest::Method::from_bytes(method.as_bytes())
.map_err(|_| format!("not an HTTP method: {method}"))?;
let mut req = client.request(m, &url);
@@ -2983,7 +2993,11 @@ async fn run_code_search(ctx: &Ctx, args: &Value) -> Value {
let base_url = ctx.base_url.clone();
let auth = ctx.auth.clone();
let res = tokio::task::spawn_blocking(move || {
- let http = McpXc { base_url, auth };
+ let http = McpXc {
+ base_url,
+ auth,
+ timeout: REQUEST_TIMEOUT,
+ };
let root = code_root();
xerj_common::xccode::run_code_query(&root, &http, &http.base_url, &p, "stale_ok:true")
})
@@ -3311,6 +3325,36 @@ mod tests {
(m, p, b.expect("builder should carry a body"))
}
+ /// `xerj_code_search` talks to the node through `McpXc`, not `Ctx.client`,
+ /// so it needs a deadline of its own: a node that accepts the connection
+ /// and never answers must give an error, not hang the tool call (and with
+ /// it the stdio loop, which serves one request at a time).
+ #[test]
+ fn code_search_transport_gives_up_on_a_silent_node() {
+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ let base_url = format!("http://{}", listener.local_addr().unwrap());
+ // Accept, then hold the connection open without a byte of reply.
+ std::thread::spawn(move || {
+ let held = listener.accept();
+ std::thread::sleep(Duration::from_secs(10));
+ drop(held);
+ });
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let http = McpXc {
+ base_url,
+ auth: None,
+ timeout: Duration::from_millis(300),
+ };
+ let _ = tx.send(http.request("GET", "/_cat/indices/x?format=json", None));
+ });
+ let res = rx
+ .recv_timeout(Duration::from_secs(5))
+ .expect("McpXc::request still pending after 5 s against a silent node");
+ let err = res.expect_err("a silent node is not a reply");
+ assert!(err.contains("no response from"), "{err}");
+ }
+
/// #2079: the sentence itself, with no network. The connect-vs-HTTP split
/// is covered by the tests below.
#[test]