Skip to content

dvm: a worker that closes stdout can no longer be killed #1102

Done

Description

@chappie-daemon

Found by the static review of the 026-nwc-req-before-event branch, run after its round 1: the finding is in a file that branch does not touch, and it is pre-existing on rewrite/main at 4adaa42.

The mechanism

crates/nostream-services/src/workers/dvm_orchestrator.rs:581-612 — drive_worker reads the child's stdout and watches the kill channel in one select!:

loop {
    tokio::select! {
        line = lines.next_line() => {
            match line {
                Ok(Some(line)) => { ... }
                Ok(None) => break,      // stdout closed
                Err(_) => break,
            }
        }
        _ = &mut killed => {
            let _ = process.kill().await;
            break;
        }
    }
}

let (code, signal) = match process.wait().await { ... };

The killed arm is polled only inside the loop. Once stdout closes — a worker that finishes its output and then hangs, or one whose output is redirected — the function leaves the loop and blocks in process.wait(), where a later kill request has nothing polling its receiver. The sender in kill_worker completes, the child is never killed, and it survives until it exits on its own. Child is held right there, so the kill is still deliverable; nothing delivers it.

What a fix needs

Keep the killed arm active while waiting — a second select! over process.wait() and killed — or kill and reap the child when its output closes if that is the intended policy for a worker that ends its stream. Either way there needs to be a test with a child that closes stdout and then sleeps, asserting the kill reaches it; the current tests drive a child that exits, which is why this is invisible.

Activity

  1. added
    bug
    rewrite
    rust
    on Nov 10, 2026
  2. AgrimTawani commented on Nov 23, 2026

    @AgrimTawani
    Contributor

    Hey @cameri, is anyone working on this? If not, I'd like to give it a go.

    • Keep the killed arm live after stdout closes, with a second select! over process.wait() and killed
    • Add a test with a child that closes stdout and then sleeps, and check the kill reaches it
  3. cameri commented on Nov 24, 2026

    @cameri
    Owner

    @AgrimTawani Assigned to you

  4. AgrimTawani commented on Nov 25, 2026

    @AgrimTawani
    Contributor

    Hey @cameri, the fix is ready on my fork; I can't open the PR myself: rewrite/main...AgrimTawani:nostream:fix/1102-kill-after-stdout-closes

    • After stdout closes, the wait now runs in a select! with the kill request, so a timeout or shutdown kill still reaches the child and it's reaped (the capture's worker.kill() behaves the same)
    • New test: the child closes stdout, writes its pid and execs sleep 60; on rewrite/main it's still alive 5 s after the kill, with the fix it dies
    • All five CLAUDE.md checks pass (1818 tests); dvm_worker passed 100+ extra runs, including under load
  5. agrim-sarvam commented on Nov 26, 2026

    @agrim-sarvam

    Hey @cameri, just a nudge on this one. The fix is still ready on my fork, whenever you have time to open the PR: rewrite/main...AgrimTawani:nostream:fix/1102-kill-after-stdout-closes

    • Based on rewrite/main dcf0259; all five CLAUDE.md checks pass, and I can rebase it if you need
  6. chappie-daemon commented on Nov 27, 2026

    @chappie-daemon
    CollaboratorAuthor

    Opened the PR on your behalf: #1258, from contrib/1102-kill-after-stdout, with your commit 53d92d07 cherry-picked -x so your authorship and the fork origin are kept.

    Verification I ran before opening it, on a worktree based on rewrite/main a4312b69:

    • 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
    • your new test alone, 10 consecutive runs — 10 passed
    • negative control: with only the dvm_orchestrator.rs hunk reverted and your test kept, it fails — the child is still alive 5 s after its kill request; restoring the hunk makes it pass, so the test does detect the defect
    • cargo test --workspace --all-features — exit 0, every binary ok, no failures

    The select! shape is right: killed is a oneshot::Receiver created once at line 199 and polled only in drive_worker, so a completed receiver can only be observed by the arm that breaks the loop — the kill_requested guard keeps the plain wait from polling a spent receiver. No further changes were needed.

  7. chappie-daemon commented on Nov 27, 2026

    @chappie-daemon
    CollaboratorAuthor

    Landed on rewrite/main as 54a5a39f, squashed from #1258, and it carries your commit unchanged (7674833d) plus one test-only commit of mine (f2013247) that widened the test's job timeout from 500 ms to 3 s — the 500 ms was also the window in which the child had to spawn and write its pid, so a slow runner could fail the test with the fix in place.

    Leaving this open, as with #1103: the fix is on rewrite/main, which the release PR does not track yet, so closing would claim a shipped fix. It closes when that line ships.

  8. chappie-daemon commented on Nov 27, 2026

    @chappie-daemon
    CollaboratorAuthor

    Closing as done. The fix is in drive_worker on rewrite/main (landed as 54a5a39, squashed from #1258): when stdout closes the loop still breaks, but the kill request stays live afterwards — the code now races process.wait() against the kill channel, so a child that closes its output and hangs is still killed, and the comment next to it names this issue.

    main carries no crates/ tree, so there is nothing to port or follow up here: this code ships when rewrite/main becomes the release line, which is a release decision and not work left on this finding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugrewriterust

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions