H: make the SIGTERM tests immune to a stolen port Both helpers verify the socket answers as our server and retry on a fresh port when the child lost the bind race; the SSE exit bound is 10 s

This commit is contained in:
ari committed 2026-10-20 16:16:13 -04:00
1 parent 3f017725f8
commit fea6a52bb0
2 files changed
+136 -29

No files matched your search

+76 -8
View File
@@ -390,12 +390,33 @@ fn nonloopback_without_auth_exits_nonzero() {
#[test]
fn sigterm_with_open_sse_client_exits_promptly() {
use std::io::{Read, Write};
use std::net::TcpStream as StdStream;
// The port is probed before the child binds it. If something else takes
// it in between, our child exits and the socket belongs to a stranger, so
// a fresh port is tried instead of signalling the wrong process.
let mut started = false;
for _ in 0..3 {
if sse_stream_then_sigterm() {
started = true;
break;
}
}
assert!(started, "server did not stay up on any of three ports");
}
/// One attempt on a fresh port. `false` means the child exited before it
/// served, which only happens when the probed port was taken by someone else.
fn sse_stream_then_sigterm() -> bool {
let port = {
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
l.local_addr().unwrap().port()
};
sse_stream_then_sigterm_on(port)
}
fn sse_stream_then_sigterm_on(port: u16) -> bool {
use std::io::{Read, Write};
use std::net::TcpStream as StdStream;
let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_slinuxm"))
.args(["serve", "--auth-enabled", "false", "--bind", "127.0.0.1"])
.args(["--port", &port.to_string()])
@@ -403,15 +424,29 @@ fn sigterm_with_open_sse_client_exits_promptly() {
.stderr(std::process::Stdio::null())
.spawn()
.unwrap();
let mut conn = None;
// Only a server that answers proves the socket is ours; a listener that
// answers nothing is a squatter, and the child that lost the race exits.
let mut ready = false;
for _ in 0..50 {
if let Ok(c) = StdStream::connect(("127.0.0.1", port)) {
conn = Some(c);
if child.try_wait().unwrap().is_some() {
return false;
}
if healthz_answers(port) {
ready = true;
break;
}
std::thread::sleep(Duration::from_millis(100));
}
let mut conn = conn.expect("server did not start");
if !ready || child.try_wait().unwrap().is_some() {
// Alive but never answered: a real startup failure, not a lost race.
let hung = !ready && child.try_wait().unwrap().is_none();
let _ = child.kill();
let _ = child.wait();
assert!(!hung, "server did not start");
return false;
}
let mut conn = StdStream::connect(("127.0.0.1", port)).unwrap();
conn.set_read_timeout(Some(Duration::from_millis(500)))
.unwrap();
write!(
@@ -433,12 +468,45 @@ fn sigterm_with_open_sse_client_exits_promptly() {
assert!(st.success(), "exit status {st}");
break;
}
if start.elapsed() > Duration::from_secs(5) {
if start.elapsed() > Duration::from_secs(10) {
let _ = child.kill();
panic!("process did not exit within 5s of SIGTERM");
panic!("process did not exit within 10s of SIGTERM");
}
std::thread::sleep(Duration::from_millis(50));
}
true
}
/// A port already held by a listener that never answers is not ours: the
/// helper reports "retry" instead of signalling the wrong server.
#[test]
fn a_probed_port_taken_by_someone_else_is_retried() {
let squatter = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = squatter.local_addr().unwrap().port();
assert!(!sse_stream_then_sigterm_on(port));
drop(squatter);
}
fn healthz_answers(port: u16) -> bool {
use std::io::{Read, Write};
let Ok(mut conn) = std::net::TcpStream::connect(("127.0.0.1", port)) else {
return false;
};
conn.set_read_timeout(Some(Duration::from_millis(500)))
.unwrap();
if write!(
conn,
"GET /healthz HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\n\r\n"
)
.is_err()
{
return false;
}
let mut buf = [0u8; 32];
match conn.read(&mut buf) {
Ok(n) => String::from_utf8_lossy(&buf[..n]).starts_with("HTTP/1.1 200"),
Err(_) => false,
}
}
/// Real binary: an open MCP session with an in-flight `tail_journal` call