Skip to content

fix(launcher): complete --command on its own exit, with a bounded output drain (#1459) - #1474

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1459-command-pipe-drain
Nov 1, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1459-command-pipe-drain

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1459

What changed

The --command block in the bootstrap body ran the command as su ... 2>&1 | tee -a /var/log/spawn-command.log and read ${PIPESTATUS[0]}. A pipeline ends only when tee gets EOF. So any descendant still holding the command's stdout or stderr (an un-waited aws s3 sync ... &) held back /tmp/SPAWN_EXITCODE, the completion file and --on-complete. Only the TTL bounded that.

This implements the owner's decision on #1459: completion is the command's own exit.

  • tee reads a FIFO (/tmp/spawn-command.fifo) in the background, and the command writes to the FIFO. CMD_RC is su's own $? (|| CMD_RC=$?, because the subshell runs under set -e), so non-zero codes and 128+n signal codes are kept as before. I used a FIFO with $! instead of > >(tee ...): $! for a process substitution needs bash 4.4, and AL2 has 4.2.46.
  • After the command exits, the block polls kill -0 $tee for at most commandDrainPolls (100) polls of commandDrainPoll (0.1 s), which is 10 s. If nothing holds the output, tee exits at once and the loop ends.
  • Past the bound, it scans /proc/*/fd/* with -ef against the FIFO. That is a bash builtin, so there is no fork per fd, and the block's own tee is excluded. It logs one line to the command log and the console:
    ⚠️ spawn: not waiting for processes still holding --command's output after 10s; they run on: <pid> <args> ...
    Then it writes SPAWN_EXITCODE and the completion file. The stragglers keep running, and keep logging through tee.
  • If mkfifo fails, the output goes to /dev/null but completion is still recorded, because a missing record bills to TTL.
  • The --command help text (and docs-gen/launch.md) now says background jobs are not waited for.

On-complete and stragglers:

Published bootstrap body changes

Its sha256 changes, so deploy the sweep orchestrator from the next tag. The deploy checklist will show it.

User-data size (bare bootstrap, gzip)

before (main cc12692) after
inline bare bootstrap 7138 7393 (+255, of which the PID/command listing is ~94)
release stub 1804 1805
tasklaunch dev-build boundary command 8212 B 7802 B
tasklaunch docker-socket user data 11774 12028

The inline bootstrap had only 30 bytes of slack above the #1422 1 KiB margin. bootstrapGzipBudget is raised 8192 → 8448 (+256) in this PR, for review. Release launches carry the stub, whose size is unchanged, so the room lost is a dev build's. I tried de-duplicating the two MOTD heredocs as a reclaim, and it saved only 14 bytes, so I didn't include it. Dropping the comments inside the /etc/profile.d heredocs would reclaim more, but those comments are user-visible file content, so I left them alone.

Gates (pkg/launcher/bootstrap_command_drain_test.go)

These run the real --command block in the spot sandbox. A stub sleep on PATH counts the drain polls, so the bounds are counts, not wall-clock. A slow tee stub writes each line 0.2 s late, so a block that doesn't drain is caught every time.

  • TestCommandCompletesWhileAStragglerHoldsItsOutput (exit 7, and SIGTERM → 143) checks five things: the straggler is still alive when completion is recorded; there are exactly commandDrainPolls polls; SPAWN_EXITCODE and the record keep the code; the last line written before exit is in the log ahead of the errexit notice; and the notice names the straggler's PID (where /proc exists), not the block's own tee.
  • TestCommandWithoutStragglersAddsNoDelay: fewer than commandDrainPolls/4 polls, and no straggler notice.
  • TestCommandDrainMatchesConstant: the shell literals match the Go constants.

Each was watched failing against a compiling revert of bootstrap.go on Linux (ubuntu:24.04):

  • old | tee pipeline: "the straggler (pid 40) is gone, so the block waited for it before recording completion" (60 s run)
  • no drain loop: "the drain polled 0 times ... want exactly commandDrainPolls (100)", plus "the command's last line must be in the log ahead of the errexit notice (last=-1 notice=0)", plus the missing straggler notice
  • drain always runs to the bound: "the drain polled 100 times with no straggler", plus "a straggler notice with no straggler"
  • exit code dropped (|| true): "SPAWN_EXITCODE = "0", want the command's own 7" / "143", and TestCommandOutcomeIsCaptured fails

The gates pass on macOS, ubuntu:24.04 (bash 5.2), amazonlinux:2023 (5.2.15) and amazonlinux:2 (4.2.46). Testing on AL2 caught that procps 3.3.10 reads -o pid=,args= as a single header, so the block uses -o pid= -o args=.

Other runners checked

  • MPI (pkg/userdata/mpi.go:381) has the same shape and is NOT changed here: mpirun ... 2>&1 | tee -a ...; spawn_mpi_done "${PIPESTATUS[0]}". Ranks' stdout goes through mpirun's I/O forwarding, not the tee pipe, so only something that inherits mpirun's own stdout would hold it. I have not verified that empirically (no mpirun here).
  • Task wrapper (pkg/taskproto/wrapper.go): it runs inside --command, so this fix covers it. It runs the task directly, with no pipe.
  • Queue runner (pkg/agent/queue_runner.go): *os.File stdout/stderr, so cmd.Wait returns on process exit. Not affected.
  • Pool worker (pkg/taskpool/exec.go via cmd/spored/pool_worker.go): os.Stdout/os.Stderr. Not affected.
  • Pipeline stage runner: os.Stdout. Not affected.
  • Windows: --command is refused on Windows (Windows launches silently drop --command and --user-data #1022), and no Windows runner exists.

Also

bootstrap_stub_test.go's boot sandbox now links mkfifo. Without it the command's output went to the /dev/null fallback: completion was still recorded, but the log was empty.

@scttfrdmn scttfrdmn added this to the v0.154.0 milestone Oct 31, 2026
@codecov

codecov Bot commented Oct 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

bootstrap: a surviving descendant holding the --command pipe delays SPAWN_EXITCODE and on-complete indefinitely

1 participant