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(common): make SpawnedThread Send so musl release builds compile (#2376)

xerj-org14ff63161b23
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e7187a640..84516d17b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `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
+ `xerj share` moves the cloudflared log-reader handles onto another
+ thread, so the `x86_64-unknown-linux-musl` and
+ `aarch64-unknown-linux-musl` release builds of `xerj-server` failed
+ to compile (release.yml run 40133568141). POSIX treats `pthread_t` as
+ an opaque id that any thread may join or detach, and this handle still
+ has exactly one owner
+ ([#2376](https://github.com/xerj-org/xerj/pull/2376)).
+
- A graph-path `xerj autoindex` resume whose plan predates files now on disk
names those files in the summary. The human text under the `ok:` line, the
lines immediately before `xerj-done`, the run document, and the
diff --git a/engine/crates/xerj-common/src/thread_spawn.rs b/engine/crates/xerj-common/src/thread_spawn.rs
index 66ca4922e..d7fe2ab04 100644
--- a/engine/crates/xerj-common/src/thread_spawn.rs
+++ b/engine/crates/xerj-common/src/thread_spawn.rs
@@ -228,6 +228,30 @@ impl Drop for SpawnedThread {
}
}
+// `libc::pthread_t` is `*mut c_void` on musl and an integer on glibc
+// (`c_ulong`) and Apple (`uintptr_t`, libc 0.2.191). The integer forms
+// make `SpawnedThread` auto-`Send` + `Sync`, and a second impl would
+// conflict, so these impls exist only for musl.
+//
+// SAFETY: the stored value is an opaque POSIX thread id. This process
+// never dereferences it. `pthread_join` and `pthread_detach` are the
+// only uses, and POSIX allows either from any thread. `SpawnedThread`
+// is the single owner of that obligation: `join` takes `self`, `Drop`
+// detaches, and `joined` is written only through `&mut self`. A shared
+// `&SpawnedThread` cannot perform either call (`Debug` prints a
+// constant string), so `Sync` is sound and matches the automatic impl
+// glibc already provides and `std::thread::JoinHandle`.
+#[cfg(target_env = "musl")]
+unsafe impl Send for SpawnedThread {}
+// SAFETY: same as `Send` above. Shared references do not join or detach.
+#[cfg(target_env = "musl")]
+unsafe impl Sync for SpawnedThread {}
+
+const _: () = {
+ const fn assert_send_sync<T: Send + Sync>() {}
+ assert_send_sync::<SpawnedThread>()
+};
+
/// A spawn that did not happen. `job` is the closure that would have run on
/// the thread, returned to the caller so it can degrade (run it inline),
/// retry, or drop it on purpose. It is `None` only if the spawner reported a
@@ -1130,6 +1154,32 @@ mod tests {
assert!(rx.try_recv().is_err(), "ran exactly once");
}
+ #[test]
+ fn a_spawned_thread_is_send_and_can_be_joined_from_another_thread() {
+ fn assert_send_sync<T: Send + Sync>() {}
+ assert_send_sync::<SpawnedThread>();
+
+ let started = std::sync::Arc::new(AtomicUsize::new(0));
+ let flag = std::sync::Arc::clone(&started);
+ let handle = try_spawn("xerj-send", move || {
+ flag.fetch_add(1, Ordering::SeqCst);
+ })
+ .expect("spawn");
+ std::thread::spawn(move || handle.join())
+ .join()
+ .expect("joiner thread")
+ .expect("join");
+ assert_eq!(started.load(Ordering::SeqCst), 1);
+ }
+
+ #[test]
+ fn a_spawned_thread_can_be_detached_on_another_thread() {
+ let handle = try_spawn("xerj-detach", || {}).expect("spawn");
+ std::thread::spawn(move || drop(handle))
+ .join()
+ .expect("detacher thread");
+ }
+
#[cfg(unix)]
#[test]
fn a_spawned_helper_does_not_install_an_alternate_signal_stack() {