From 7674833d566d63d07a9ba4cb32e13a3a26523e2c Mon Sep 17 00:00:00 2001 From: AgrimTawani Date: Wed, 25 Nov 2026 15:08:37 +0530 Subject: [PATCH 1/2] fix(dvm): keep a worker killable after it closes stdout drive_worker polled the kill request only inside the stdout-reading loop. Once stdout closed, it blocked in process.wait() with nothing polling the receiver, so a later kill (a job timeout, a stalled stdin write, shutdown) never reached a child that finished its output and hung. The wait now runs in a select! with the kill request; a receiver that already fired is not polled again. Closes #1102 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 53d92d07440737e9bb5ce352282fac0069782b92) --- .changeset/dvm-kill-after-stdout-closes.md | 6 ++ .../src/workers/dvm_orchestrator.rs | 19 +++++- crates/nostream-services/tests/dvm_worker.rs | 58 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 .changeset/dvm-kill-after-stdout-closes.md diff --git a/.changeset/dvm-kill-after-stdout-closes.md b/.changeset/dvm-kill-after-stdout-closes.md new file mode 100644 index 000000000..ef8568064 --- /dev/null +++ b/.changeset/dvm-kill-after-stdout-closes.md @@ -0,0 +1,6 @@ +--- +--- + +fix(dvm): keep a worker killable after it closes stdout (#1102) + +`drive_worker` watched the kill request only while it was reading the child's stdout. Once stdout closed — a worker that finished its output and hung, or one whose output went elsewhere — it blocked in `process.wait()`, and a later kill (a job timeout, a stalled stdin write, shutdown) reached nothing, so the child lived until it exited on its own or the orchestrator itself was dropped. The wait now runs alongside the kill request, so the kill is delivered and the child reaped either way. Rust-only; no published package changes. diff --git a/crates/nostream-services/src/workers/dvm_orchestrator.rs b/crates/nostream-services/src/workers/dvm_orchestrator.rs index 7e2ddc13a..7d27d205f 100644 --- a/crates/nostream-services/src/workers/dvm_orchestrator.rs +++ b/crates/nostream-services/src/workers/dvm_orchestrator.rs @@ -583,6 +583,7 @@ async fn drive_worker( events: mpsc::UnboundedSender, ) { let mut lines = BufReader::new(stdout).lines(); + let mut kill_requested = false; loop { tokio::select! { @@ -598,13 +599,29 @@ async fn drive_worker( } } _ = &mut killed => { + kill_requested = true; let _ = process.kill().await; break; } } } - let (code, signal) = match process.wait().await { + // Once stdout has closed the child may still be running — it finished its output and + // hung, or its output went elsewhere — so the kill request stays live while it is awaited + // (#1102). A receiver that already fired is not polled again. + let status = if kill_requested { + process.wait().await + } else { + tokio::select! { + status = process.wait() => status, + _ = &mut killed => { + let _ = process.kill().await; + process.wait().await + } + } + }; + + let (code, signal) = match status { Ok(status) => exit_parts(status), Err(_) => (None, None), }; diff --git a/crates/nostream-services/tests/dvm_worker.rs b/crates/nostream-services/tests/dvm_worker.rs index 94f705f89..24c29e416 100644 --- a/crates/nostream-services/tests/dvm_worker.rs +++ b/crates/nostream-services/tests/dvm_worker.rs @@ -231,6 +231,64 @@ async fn a_job_that_outlives_its_timeout_is_timed_out_and_the_worker_killed() { let _ = tokio::time::timeout(Duration::from_secs(5), running).await; } +/// Whether a process with this pid is still there (a reaped child no longer is). +fn alive(pid: &str) -> bool { + std::process::Command::new("kill") + .args(["-0", pid]) + .stderr(std::process::Stdio::null()) + .status() + .expect("kill runs") + .success() +} + +#[tokio::test] +async fn a_worker_that_closed_stdout_is_still_killed_on_timeout() { + let directory = tempfile::tempdir().expect("a temporary directory"); + let pid_file = directory.path().join("pid"); + // Close stdout, then record the pid, then sleep in the same process: the orchestrator sees + // the stream end while the child lives on, and the job's timeout must still kill it. The + // pid is written after stdout closes, so a pid on disk means the kill below can only reach + // the child through the post-stdout path. + let script = format!( + "exec >&-; echo $$ > '{}.tmp' && mv '{0}.tmp' '{0}'; exec sleep 60", + pid_file.display() + ); + let fixture = build(worker(vec!["-c", &script], 500), true); + let shutdown = ShutdownState::new(); + let running = tokio::spawn(Arc::clone(&fixture.orchestrator).run(shutdown.clone())); + + // Written by rename, so it is never read half-written; awaited before the 500 ms timeout. + let pid = tokio::time::timeout(Duration::from_secs(5), async { + loop { + if let Ok(pid) = std::fs::read_to_string(&pid_file) { + return pid.trim().to_owned(); + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("the child closed stdout and wrote its pid"); + assert!(alive(&pid), "the child is running before its timeout"); + + let updates = await_updates(&fixture, 1).await; + assert_eq!(updates[0].status, DvmJobStatus::TimedOut); + + let killed = tokio::time::timeout(Duration::from_secs(5), async { + while alive(&pid) { + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await; + + assert!( + killed.is_ok(), + "the timed-out worker (pid {pid}) is still running after its kill request" + ); + + shutdown.begin_draining(); + let _ = tokio::time::timeout(Duration::from_secs(5), running).await; +} + #[tokio::test] async fn a_result_that_cannot_be_signed_fails_the_job() { let fixture = build(worker(vec!["-c", "cat"], 5_000), false); From f20132473bbeb3903291fef3812ec46de0b13a3c Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Thu, 26 Nov 2026 23:34:00 -0500 Subject: [PATCH 2/2] test(dvm): give the child room to outlive its spawn, not only its timeout The 500 ms job timeout was also the window in which the child had to spawn, close stdout and write its pid. A child delayed 600 ms failed the test with the fix in place (measured), so a loaded runner could flake it. At 3 s the observation window is the spawn latency's; the assertion that matters still runs, and with the fix reverted the test fails where it should - the child still alive after its kill request. --- crates/nostream-services/tests/dvm_worker.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/nostream-services/tests/dvm_worker.rs b/crates/nostream-services/tests/dvm_worker.rs index 24c29e416..5712946e6 100644 --- a/crates/nostream-services/tests/dvm_worker.rs +++ b/crates/nostream-services/tests/dvm_worker.rs @@ -253,11 +253,15 @@ async fn a_worker_that_closed_stdout_is_still_killed_on_timeout() { "exec >&-; echo $$ > '{}.tmp' && mv '{0}.tmp' '{0}'; exec sleep 60", pid_file.display() ); - let fixture = build(worker(vec!["-c", &script], 500), true); + // The job timeout is generous on purpose: the pid must be observed *before* it fires, and + // writing it is subject to process spawn on a loaded runner. At 500 ms a child that took + // 600 ms to start failed the test with the fix in place (measured), so the window here is + // the spawn latency's, not the kill's — the kill is still what the assertions below test. + let fixture = build(worker(vec!["-c", &script], 3_000), true); let shutdown = ShutdownState::new(); let running = tokio::spawn(Arc::clone(&fixture.orchestrator).run(shutdown.clone())); - // Written by rename, so it is never read half-written; awaited before the 500 ms timeout. + // Written by rename, so it is never read half-written; awaited inside the job timeout. let pid = tokio::time::timeout(Duration::from_secs(5), async { loop { if let Ok(pid) = std::fs::read_to_string(&pid_file) {