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): a failed thread spawn fails `xerj share`'s tunnel instead of aborting (#1122)

xerj-org8972961b7772
diff --git a/engine/crates/xerj-server/src/ingest_memory_trace.rs b/engine/crates/xerj-server/src/ingest_memory_trace.rs
index b779881c3..3c9f3d7e8 100644
--- a/engine/crates/xerj-server/src/ingest_memory_trace.rs
+++ b/engine/crates/xerj-server/src/ingest_memory_trace.rs
@@ -40,6 +40,18 @@ impl Drop for TraceRuntime {
}
}
+/// A trace thread that could not start (#1122). The trace is diagnostics
+/// only, so the node runs on without it — but say so, rather than leaving an
+/// operator who asked for a trace with an empty file and no reason.
+fn trace_spawn_failed(thread: &str, error: &std::io::Error) {
+ tracing::warn!(
+ target: "xerj::ingest_memory",
+ thread,
+ %error,
+ "ingest-memory trace disabled: could not start its {thread} thread"
+ );
+}
+
pub fn spawn(engine: &Arc<Engine>) -> Option<TraceRuntime> {
let config = ingest_memory::config();
if config.mode == TraceMode::Off {
@@ -53,6 +65,7 @@ pub fn spawn(engine: &Arc<Engine>) -> Option<TraceRuntime> {
let writer = std::thread::Builder::new()
.name("xerj-ingest-memory-writer".into())
.spawn(move || writer_loop(rx, writer_dropped))
+ .map_err(|error| trace_spawn_failed("writer", &error))
.ok()?;
let weak = Arc::downgrade(engine);
@@ -73,6 +86,9 @@ pub fn spawn(engine: &Arc<Engine>) -> Option<TraceRuntime> {
Duration::from_millis(config.sample_ms),
);
})
+ // The writer exits on its own: the sender it reads from was moved
+ // into the closure the failed spawn just dropped.
+ .map_err(|error| trace_spawn_failed("sampler", &error))
.ok()?;
Some(TraceRuntime {
diff --git a/engine/crates/xerj-server/src/share.rs b/engine/crates/xerj-server/src/share.rs
index 829f5ee21..c395a2ed3 100644
--- a/engine/crates/xerj-server/src/share.rs
+++ b/engine/crates/xerj-server/src/share.rs
@@ -1436,31 +1436,45 @@ impl Tunnel {
let (tx, rx) = mpsc::channel::<TunnelEvent>();
let log = TunnelLog::default();
- let mut readers = Vec::new();
// cloudflared logs to stderr; read stdout too so neither pipe can fill
// and stall it.
- if let Some(err) = child.stderr.take() {
- readers.push(spawn_reader(err, tx.clone(), log.clone()));
- }
- if let Some(out) = child.stdout.take() {
- readers.push(spawn_reader(out, tx.clone(), log.clone()));
- }
- // When both pipes close, the process is gone.
- std::thread::spawn(move || {
- for r in readers {
- let _ = r.join();
- }
- let _ = tx.send(TunnelEvent::Exited);
- });
+ let stderr = child.stderr.take();
+ let stdout = child.stdout.take();
// From here on `tunnel` owns the child: every early return below drops
- // it, and `Drop` closes the tunnel.
+ // it, and `Drop` closes the tunnel — including a failed thread spawn
+ // just below, which must not leave cloudflared running unread.
let mut tunnel = Tunnel {
child,
public_url: String::new(),
events: rx,
log,
};
+ // #1122: `std::thread::spawn` panics when a thread cannot be created,
+ // and the release profile is panic=abort; a failed spawn here fails
+ // the open (falling back to the local link) instead.
+ let spawn_failed =
+ |e: std::io::Error| failed(format!("start a cloudflared log reader thread: {e}"));
+ let mut readers = Vec::new();
+ for pipe in [
+ stderr.map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
+ stdout.map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ readers.push(spawn_reader(pipe, tx.clone(), tunnel.log.clone()).map_err(spawn_failed)?);
+ }
+ // When both pipes close, the process is gone.
+ std::thread::Builder::new()
+ .name("xerj-share-tunnel-wait".into())
+ .spawn(move || {
+ for r in readers {
+ let _ = r.join();
+ }
+ let _ = tx.send(TunnelEvent::Exited);
+ })
+ .map_err(spawn_failed)?;
let deadline = std::time::Instant::now() + TUNNEL_URL_TIMEOUT;
loop {
if let Some(why) = interrupts.fired() {
@@ -1561,24 +1575,26 @@ fn spawn_reader<R: std::io::Read + Send + 'static>(
pipe: R,
tx: mpsc::Sender<TunnelEvent>,
log: TunnelLog,
-) -> std::thread::JoinHandle<()> {
- std::thread::spawn(move || {
- let mut sent_url = false;
- for line in BufReader::new(pipe).lines() {
- let Ok(line) = line else { break };
- remember_tunnel_line(&log, &line);
- if !sent_url {
- if let Some(u) = parse_tunnel_url(&line) {
- sent_url = true;
- let _ = tx.send(TunnelEvent::Url(u));
- continue;
+) -> std::io::Result<std::thread::JoinHandle<()>> {
+ std::thread::Builder::new()
+ .name("xerj-share-tunnel-log".into())
+ .spawn(move || {
+ let mut sent_url = false;
+ for line in BufReader::new(pipe).lines() {
+ let Ok(line) = line else { break };
+ remember_tunnel_line(&log, &line);
+ if !sent_url {
+ if let Some(u) = parse_tunnel_url(&line) {
+ sent_url = true;
+ let _ = tx.send(TunnelEvent::Url(u));
+ continue;
+ }
+ }
+ if tunnel_line_means_connected(&line) {
+ let _ = tx.send(TunnelEvent::Connected);
}
}
- if tunnel_line_means_connected(&line) {
- let _ = tx.send(TunnelEvent::Connected);
- }
- }
- })
+ })
}
/// Keep `line` as one of the last [`TUNNEL_LOG_LINES`]; long lines are cut.