tests: every temporary path is removed by a guard; no leftovers in /tmp

About 10,000 test leftovers had built up in titan's /tmp. loopd's Home
and FakeServer, its fake broker's socket, bxctl's temp sockets, fake
loopd and command homes, inferproxy's socket, and a few brokerd and
toolkit tests created uniquely named directories or sockets and never
removed them, or removed them by hand at the end, which a failing test
skips. Each is now a guard removed on drop (bxctl gains
tests/support/temp.rs). Two races also left gatewayd homes behind: the
test Running only signalled its server to stop, and two Stage structs
declared the home first, so it was removed while the server could still
write into it. Running's drop now waits for the thread (with a 5-second
limit), and every home is declared last.

Checked: make gate twice, then the gate and three rounds of gatewayd and
loopd tests; no new entry in /tmp; 944 tests pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-26 01:29:32 -07:00
co-authored by Claude Opus 5.5
parent 53532e04ff
commit a15c428ea6
19 changed files with 230 additions and 51 deletions
+29 -4
View File
@@ -6,7 +6,7 @@
#![allow(dead_code)] // each test file uses a different part of this module
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
@@ -21,9 +21,31 @@ use proto::{
static NEXT: AtomicU32 = AtomicU32::new(0);
pub fn socket_path() -> PathBuf {
/// A fresh socket path, removed when the value is dropped (tip T44).
pub struct SocketPath(PathBuf);
impl std::ops::Deref for SocketPath {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl AsRef<Path> for SocketPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl Drop for SocketPath {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
pub fn socket_path() -> SocketPath {
let n = NEXT.fetch_add(1, Ordering::SeqCst);
std::env::temp_dir().join(format!("loopd-broker-{}-{n}.sock", std::process::id()))
SocketPath(std::env::temp_dir().join(format!("loopd-broker-{}-{n}.sock", std::process::id())))
}
/// Accepts one connection, reads the request frame and hands both to `script`. The connection
@@ -35,13 +57,16 @@ where
let path = socket_path();
let _ = std::fs::remove_file(&path);
let listener = UnixListener::bind(&path).unwrap();
let returned = path.to_path_buf();
// The thread owns the path, so the socket file goes once the exchange is over.
let handle = thread::spawn(move || {
let _path = path;
let (mut stream, _) = listener.accept().unwrap();
let request = read_frame(&mut stream).unwrap();
script(&mut stream, &request);
request
});
(path, handle)
(returned, handle)
}
pub fn frame(id: u64, r#final: bool, msg: Message) -> Envelope {