Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
a64b141
fix(git-remote): drive multi-round fetch as a v0 stateless-RPC client…
beardthelion Jul 12, 2026
89c0ad5
test(git-remote): real-git integration proof of the multi-round fetch…
beardthelion Jul 12, 2026
68ba492
fix(git-remote): skip content-free flush rounds in the fetch loop (#117)
beardthelion Jul 12, 2026
c4609d4
test(git-remote): close branch-coverage gaps in the fetch loop (#117)
beardthelion Jul 12, 2026
1cb930c
fix(git-remote): abort fetch on EOF without POSTing; harden the real-…
beardthelion Jul 14, 2026
4201ce4
test(git-remote): reap the whole fetch process group and make the wit…
beardthelion Jul 15, 2026
22ec66a
test(git-remote): make the run_bounded harness portable to the shippe…
beardthelion Jul 16, 2026
1c690c1
fix(node): count a completed fetch once per fetch, not per stateless-…
beardthelion Jul 18, 2026
bf4d8cd
test(git-remote): bound the fetch reader joins when the leader exits …
beardthelion Jul 18, 2026
8c04043
fix(git-remote): reject malformed pkt-line length headers instead of …
beardthelion Jul 19, 2026
c45ec96
fix(node): make metrics::init race-safe with Once
beardthelion Jul 19, 2026
5a5d4e1
fix(node): count fetch completion from the response outcome, not the …
beardthelion Jul 19, 2026
63cb52b
test(git-remote): bind the withheld-path test to the expected ACK/NAK…
beardthelion Jul 19, 2026
659ee00
test(git-remote): tear down the fetch tree with a Job Object on Windo…
beardthelion Jul 21, 2026
01971f7
test(git-remote): make the Windows Job Object mandatory in run_bounded
beardthelion Jul 22, 2026
a53c151
chore: sync Cargo.lock after rebase onto main
beardthelion Aug 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(git-remote): abort fetch on EOF without POSTing; harden the real-…
…git test harness (#192)

F1: a nonempty have-batch terminated by EOF means git aborted mid-negotiation, but
the fetch loop folded RoundEnd::Eof into the Done arm, appended a synthetic
0009done, and POSTed it — so a cancelled private fetch still issued an authorized,
signed upload-pack request and made the node generate and stream a full pack to a
client that had gone away. Split the arm: only a real done pkt finalizes; a
nonempty EOF terminates without a POST. The nonempty_eof_batch_posts_as_final_round
test that locked in the wrong behavior is flipped to assert no POST.

F2: fetch_with_helper piped stdout+stderr but only polled try_wait, so a fetch
emitting more than an OS pipe buffer (~64 KiB) blocked git before it exited and the
harness reported a false 30s deadlock. Drain both streams on reader threads while
enforcing the deadline, so the deadline measures a real protocol hang only.

F3: the integration repos were removed only on the success path (predictable
pid/counter names), so a timeout/panic leaked real git repos and could poison a
later run. Root them in a RAII tempfile::TempDir that drops on unwind.

RED->GREEN: nonempty_eof_batch_aborts_without_posting (pre-fix POSTs a synthetic
done → the aborted-negotiation POST fails against the single-response server; fixed
makes one request, no POST). repos_are_cleaned_up_on_unwind (temp dir gone after a
panic unwinds past the guard). Full git-remote-gitlawb suite 42 unit + 3 integration
green, fmt + clippy clean.
  • Loading branch information
beardthelion committed Aug 10, 2026
commit 1cb930cbd57ea5d232edddc8e50dc94fab4362a5
1 change: 1 addition & 0 deletions crates/git-remote-gitlawb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tracing-subscriber = { workspace = true }

[dev-dependencies]
mockito = "1"
tempfile = "3"
58 changes: 31 additions & 27 deletions crates/git-remote-gitlawb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,17 @@ fn negotiate_upload_pack<R: Read>(
body.extend_from_slice(b"0000");
false
}
// A `done` round is final. A nonempty EOF-terminated batch is treated
// as final too (a test-shim convenience; a real pipe ends a fetch with
// `done`, and a genuine mid-negotiation EOF means git aborted).
RoundEnd::Done | RoundEnd::Eof => {
// Only a real `done` pkt finalizes the stateless request.
RoundEnd::Done => {
body.extend_from_slice(b"0009done\n");
true
}
// A nonempty batch terminated by EOF (no flush, no done) means git
// ABORTED mid-negotiation (#192, F1). Terminate WITHOUT POSTing:
// synthesizing a `done` here would make the node generate and stream a
// full pack — signed, for a private fetch — to a client that has gone
// away. The accumulated haves are discarded; there is no one to serve.
RoundEnd::Eof => return Ok(()),
};

post_pack_round(client, post_url, service, signing_key, body, stdout)?;
Expand Down Expand Up @@ -2095,40 +2099,40 @@ mod tests {
);
}

/// G3: a nonempty have-batch terminated by EOF (no flush, no done) is POSTed as
/// the final round, with a `done` terminator appended.
/// G3 (#192, F1): a nonempty have-batch terminated by EOF (no flush, no done)
/// means git ABORTED mid-negotiation. The helper must terminate WITHOUT POSTing
/// — synthesizing a `done` and sending it would make the node generate and
/// stream a full pack (signed, for a private fetch) to a client that has gone
/// away. Only a real `done` pkt finalizes the stateless request. RED before the
/// fix (the folded `Done | Eof` arm POSTs a synthetic done → 2 requests), GREEN
/// after (only the info/refs GET, no upload-pack POST).
#[test]
fn nonempty_eof_batch_posts_as_final_round() {
let (base_url, server) = serve_http(vec![
TestResponse {
request_line: "GET /zOwner/myrepo/info/refs?service=git-upload-pack",
status: "200 OK",
body: "0000",
},
TestResponse {
request_line: "POST /zOwner/myrepo/git-upload-pack",
status: "200 OK",
body: "0008NAK\nPACKfake",
},
]);
fn nonempty_eof_batch_aborts_without_posting() {
// Only the info/refs GET is served (serve_http accepts exactly N conns). The
// fixed helper makes exactly that one request and stops. The pre-fix code
// additionally POSTs the synthetic done; that POST hits the now-closed
// listener and errors, so handle_connect returns Err and the `.expect` below
// fires — the RED. The fixed code returns Ok with one recorded request.
let (base_url, server) = serve_http(vec![TestResponse {
request_line: "GET /zOwner/myrepo/info/refs?service=git-upload-pack",
status: "200 OK",
body: "0000",
}]);
let repo_base = format!("{base_url}/zOwner/myrepo");
let mut stdin_bytes = Vec::new();
stdin_bytes.extend_from_slice(&want_line(&"a".repeat(40)));
stdin_bytes.extend_from_slice(b"0000");
stdin_bytes.extend_from_slice(&pkt(&format!("have {}\n", "1".repeat(40))));
// No trailing flush or done: the stream just ends (EOF) after the have.
// No trailing flush or done: the stream just ends (EOF) after the have —
// git aborted the negotiation.
let mut stdin = io::Cursor::new(stdin_bytes);
handle_connect(&repo_base, "git-upload-pack", None, &mut stdin)
.expect("nonempty EOF batch should POST as a final round");
.expect("an aborted (EOF) negotiation is a clean no-op, not an error");
let requests = server.join().unwrap();
assert_eq!(
requests.len(),
2,
"one POST for the EOF-terminated final round"
);
assert!(
request_body(&requests[1]).ends_with(b"0009done\n"),
"an EOF-terminated final round is sent with a done terminator"
1,
"an aborted EOF must NOT trigger an upload-pack POST (only the info/refs GET)"
);
}

Expand Down
168 changes: 135 additions & 33 deletions crates/git-remote-gitlawb/tests/real_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ fn pkt(data: &[u8]) -> Vec<u8> {
out
}

fn unique_dir(tag: &str) -> PathBuf {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("gitlawb-u7-{}-{}-{}", tag, std::process::id(), n));
std::fs::create_dir_all(&dir).unwrap();
dir
/// A server+clone fixture rooted in a single RAII temp dir (#192, F3). The
/// `TempDir` removes the whole tree on drop — including while unwinding from a
/// failed assertion, timeout, or panic — so a failing test never leaves real git
/// repos behind, and the randomized root name cannot collide with or poison a
/// later run the way the old pid/counter names could.
struct Repos {
server: PathBuf,
clone: PathBuf,
_tmp: tempfile::TempDir,
}

/// Run git in `dir` with deterministic identity/config, asserting success.
Expand Down Expand Up @@ -255,39 +258,79 @@ fn fetch_with_helper(clone: &Path, node_url: &str) -> (bool, std::process::Outpu
None => helper_dir.clone().into_os_string(),
};

let mut child = Command::new("git")
.args(["-c", "protocol.version=2"])
let mut cmd = Command::new("git");
cmd.args(["-c", "protocol.version=2"])
.arg("-C")
.arg(clone)
.args(["fetch", "origin", "main"])
.env("PATH", path_env)
.env("GITLAWB_NODE", node_url)
.env("GITLAWB_KEY", "/nonexistent-key-for-anon-fetch")
.env("GITLAWB_KEY", "/nonexistent-key-for-anon-fetch");
run_bounded(cmd, Duration::from_secs(30))
}

/// Spawn `cmd`, draining stdout and stderr CONCURRENTLY on reader threads while
/// enforcing `timeout`. Returns `(completed_before_deadline, Output)` (#192, F2).
///
/// The concurrent drain is the whole point: polling `try_wait` without reading lets
/// a child that writes more than an OS pipe buffer (~64 KiB) block on the full pipe
/// before it exits, so the deadline would trip on pipe backpressure and report a
/// false "deadlock signature" even while the child is making progress. Draining
/// continuously makes the deadline measure a real hang only.
fn run_bounded(mut cmd: Command, timeout: Duration) -> (bool, std::process::Output) {
let mut child = cmd
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn git fetch");
.expect("spawn child");

let mut out_pipe = child.stdout.take().expect("stdout piped");
let mut err_pipe = child.stderr.take().expect("stderr piped");
let out_reader = std::thread::spawn(move || {
let mut b = Vec::new();
let _ = out_pipe.read_to_end(&mut b);
b
});
let err_reader = std::thread::spawn(move || {
let mut b = Vec::new();
let _ = err_pipe.read_to_end(&mut b);
b
});

let deadline = Instant::now() + Duration::from_secs(30);
loop {
if let Some(_status) = child.try_wait().unwrap() {
let out = child.wait_with_output().unwrap();
return (true, out);
let deadline = Instant::now() + timeout;
let completed = loop {
if child.try_wait().unwrap().is_some() {
break true;
}
if Instant::now() >= deadline {
let _ = child.kill();
let out = child.wait_with_output().unwrap();
return (false, out); // timed out: the deadlock signature
let _ = child.kill(); // closes the pipes so the readers finish
break false; // timed out: the real deadlock signature
}
std::thread::sleep(Duration::from_millis(50));
}
};

// The child has exited (or been killed), so the pipes are closed and the reader
// threads run to completion. Reap the child and collect the drained output.
let status = child.wait().expect("reap child");
let stdout = out_reader.join().expect("stdout reader");
let stderr = err_reader.join().expect("stderr reader");
(
completed,
std::process::Output {
status,
stdout,
stderr,
},
)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// Build a server repo with a shared history deep enough to force multi-round
/// negotiation (>~32 haves), plus a clone of it, then advance the server so the
/// fetch has something to negotiate. Returns (server, clone).
fn build_divergent_repos(shared_commits: usize) -> (PathBuf, PathBuf) {
let server = unique_dir("server");
fn build_divergent_repos(shared_commits: usize) -> Repos {
let tmp = tempfile::TempDir::new().unwrap();
let server = tmp.path().join("server");
std::fs::create_dir_all(&server).unwrap();
git(&server, &["init", "-q"]);
std::fs::write(server.join("base.txt"), b"base").unwrap();
git(&server, &["add", "."]);
Expand All @@ -306,7 +349,7 @@ fn build_divergent_repos(shared_commits: usize) -> (PathBuf, PathBuf) {
);
}

let clone = unique_dir("clone");
let clone = tmp.path().join("clone");
// Clone over file:// so the clone shares the full history.
let status = Command::new("git")
.args(["clone", "-q"])
Expand Down Expand Up @@ -344,7 +387,11 @@ fn build_divergent_repos(shared_commits: usize) -> (PathBuf, PathBuf) {
"gitlawb://did:key:zTESTOWNER/myrepo",
],
);
(server, clone)
Repos {
server,
clone,
_tmp: tmp,
}
}

/// Matrix item 7, bridging half: a real multi-round `git fetch` through the
Expand All @@ -353,7 +400,10 @@ fn build_divergent_repos(shared_commits: usize) -> (PathBuf, PathBuf) {
/// one round would fail this), and produces a correct object graph.
#[test]
fn real_git_multi_round_fetch_completes() {
let (server, clone) = build_divergent_repos(50);
// `repos` owns the RAII temp dir; keep it in scope so cleanup runs on drop,
// including while unwinding from any assertion below (#192, F3).
let repos = build_divergent_repos(50);
let (server, clone) = (repos.server.clone(), repos.clone.clone());
let shim = start_shim(server.clone(), ShimMode::Normal);

let (completed, out) = fetch_with_helper(&clone, &shim.base_url);
Expand Down Expand Up @@ -383,8 +433,63 @@ fn real_git_multi_round_fetch_completes() {
"FETCH_HEAD must match the server tip"
);
git(&clone, &["fsck", "--full"]);
// No manual cleanup: `repos` drops here (or on unwind) and removes the tree.
}

cleanup(&[server, clone]);
/// #192 (F3): a panic (any unwind) mid-test still removes the repos. The old
/// success-only `cleanup()` ran after the assertions, so a failing test leaked real
/// git repos; the RAII `TempDir` drops during unwind instead. Load-bearing: the
/// path exists inside the closure and must be gone once the panic has unwound past
/// the guard — a non-RAII cleanup would leave it behind.
#[test]
fn repos_are_cleaned_up_on_unwind() {
let captured = Arc::new(std::sync::Mutex::new(None::<PathBuf>));
let c = captured.clone();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let repos = build_divergent_repos(1);
assert!(
repos._tmp.path().exists(),
"temp dir exists during the test"
);
*c.lock().unwrap() = Some(repos._tmp.path().to_path_buf());
panic!("simulated mid-test failure");
}));
assert!(result.is_err(), "the closure panicked as designed");
let path = captured
.lock()
.unwrap()
.take()
.expect("captured the temp path");
assert!(
!path.exists(),
"the RAII temp dir must be removed on unwind, not leaked: {}",
path.display()
);
}

/// #192 (F2): `run_bounded` must DRAIN a child that emits more than an OS pipe
/// buffer, not deadlock. A child writing ~140 KiB to BOTH stdout and stderr and
/// then exiting must complete within the deadline. Under a `try_wait`-only harness
/// (no concurrent drain) the child blocks on the full pipe, the deadline trips, and
/// `completed` is false — the exact false "deadlock signature" F2 fixes. Reverting
/// the concurrent drain to poll-then-read turns this test RED (completed=false).
#[test]
fn run_bounded_drains_large_output_without_deadlock() {
// seq 1..=25000 is ~140 KiB on each stream — well past a ~64 KiB pipe buffer.
let mut cmd = Command::new("sh");
cmd.arg("-c").arg("seq 1 25000; seq 1 25000 1>&2");
let (completed, out) = run_bounded(cmd, Duration::from_secs(10));
assert!(
completed,
"a child emitting >64 KiB must be drained and complete, not deadlock (F2)"
);
assert!(out.status.success(), "the child exited cleanly");
assert!(
out.stdout.len() > 64 * 1024 && out.stderr.len() > 64 * 1024,
"both streams exceeded a pipe buffer (stdout={}, stderr={})",
out.stdout.len(),
out.stderr.len()
);
}

/// Matrix item 7, withheld half (the Withheld-Path Decision gate): the node's
Expand All @@ -394,7 +499,10 @@ fn real_git_multi_round_fetch_completes() {
/// this test captures the break mode that the decision's remedy addresses.
#[test]
fn real_git_withheld_shaped_first_post() {
let (server, clone) = build_divergent_repos(50);
// `repos` owns the RAII temp dir; keep it in scope so cleanup runs on drop,
// including while unwinding from any assertion below (#192, F3).
let repos = build_divergent_repos(50);
let (server, clone) = (repos.server.clone(), repos.clone.clone());
let shim = start_shim(server.clone(), ShimMode::WithheldFirstPost);

let (completed, out) = fetch_with_helper(&clone, &shim.base_url);
Expand Down Expand Up @@ -426,11 +534,5 @@ fn real_git_withheld_shaped_first_post() {
);
}

cleanup(&[server, clone]);
}

fn cleanup(dirs: &[PathBuf]) {
for d in dirs {
let _ = std::fs::remove_dir_all(d);
}
// No manual cleanup: `repos` drops here (or on unwind) and removes the tree.
}