Skip to content

fix(dvm): keep a worker killable after it closes stdout - #1258

Merged
cameri merged 2 commits into
rewrite/mainfrom
contrib/1102-kill-after-stdout
Nov 27, 2026
Merged

cameri merged 2 commits into
rewrite/mainfrom
contrib/1102-kill-after-stdout

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

Description

drive_worker in crates/nostream-services/src/workers/dvm_orchestrator.rs polled the kill request only while it was reading the child's stdout. Once that stream ended — a worker that finished its output and hung, or one whose output went elsewhere — it blocked in process.wait() with nothing polling the receiver, so any kill raised afterwards (job timeout, stalled stdin write, shutdown) reached no one. The child lived until it exited on its own or the orchestrator itself was dropped.

The wait now runs in a select! alongside the kill request. A receiver that already fired is not polled again, so the already-killed path keeps its plain wait.

Opened on @AgrimTawani's behalf from their fork commit 53d92d07 (branch fix/1102-kill-after-stdout-closes), cherry-picked with -x so authorship and origin are preserved. They reported they cannot open the PR themselves.

Related Issue

Closes #1102

Motivation and Context

The issue's reproduction — a DVM worker that finishes its output and then hangs — stayed alive through its job timeout: the kill reached nothing, so the slot was never reclaimed and the hung child outlived the job that owned it.

How Has This Been Tested?

New test a_worker_that_closed_stdout_is_still_killed_on_timeout in crates/nostream-services/tests/dvm_worker.rs. The child closes stdout, writes its pid by rename (so the file is never read half-written), then execs sleep 60; the test asserts the pid is alive before the 500 ms timeout and gone within 5 s of the kill.

  • cargo fmt --all --check — clean
  • cargo clippy --workspace --all-targets --all-features -- -D warnings — clean
  • cargo test -p nostream-services --test dvm_worker — 12 passed, 0 failed
  • The new test alone, 10 consecutive runs — 10 passed
  • Negative control: with only the dvm_orchestrator.rs hunk reverted and the test kept, the new test fails (0 passed, 1 failed: the child is still alive 5 s after its kill request); restoring the hunk makes it pass. The test therefore detects the defect it names rather than passing either way.
  • cargo test --workspace --all-features — exit 0; every test binary reports ok, no failures

No hidden Unicode in the three changed files; the changeset is Rust-only.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation — no, the behaviour matches what the issue describes
  • I have read the CONTRIBUTING document
  • I have added tests to cover my changes
  • All new and existing tests passed

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 <noreply@anthropic.com>
(cherry picked from commit 53d92d0)
@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f201324

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f0b7d1f1-3e65-4a8e-aed1-55398930b3a7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; the remaining test timing race is non-blocking.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the remaining test timing race is non-blocking.

Findings

  1. P2 Test races the job timeout ▶

Summary

drive_worker keeps listening for kill requests after the child's stdout closes.

  • DVM workers can still be killed after their stdout closes.

Reviews (2) · Last reviewed commit: "test(dvm): give the child room to outliv..." · Reviewed by Greptile

Comment on lines +256 to +271
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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test races the job timeout

The test starts the 500 ms job timeout without waiting for the child to write its PID. On a busy runner, the timeout can kill the child before the file appears or before alive(&pid) runs. The test then fails even when the fix works. Gate job dispatch on the child's readiness, then check that the timeout kills it.

…eout

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.
@chappie-daemon

Copy link
Copy Markdown
Collaborator Author

Confirmed, and fixed in f2013247. The 500 ms job timeout was doing double duty: it was also the window in which the child had to spawn, close stdout and write its pid. Measured before changing anything — with the child delayed by sleep 0.6 before it writes the pid, the test fails with the fix in place (panicked ... the child closed stdout and wrote its pid), so a runner slow enough to add 100 ms of spawn latency could flake it.

The fix is the one you describe, done on the timeout rather than on extra synchronisation: the job timeout is now 3 s, so the observation window is the spawn latency's rather than the kill's, and await_updates already allowed 10 s for the timeout to fire.

Re-verified with the widened window:

  • the new test 10 consecutive runs — 10 passed
  • cargo test -p nostream-services --test dvm_worker — 12 passed, 0 failed (3.13 s)
  • negative control re-run: with only the dvm_orchestrator.rs hunk reverted, the test now fails at the assertion that matters — the timed-out worker (pid …) is still running after its kill request — rather than at the pid wait, which is what the widened window was for.

@cameri
cameri merged commit 54a5a39 into rewrite/main Nov 27, 2026
18 checks passed
@cameri
cameri deleted the contrib/1102-kill-after-stdout branch November 27, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants