Skip to content

launch: report --command's outcome, signal completion, and let it reach your own S3 - #630

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/614-command-outcome-and-s3
Oct 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/614-command-outcome-and-s3

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Refs #614. All four reported items verified in code. The first is worse than the issue states, and the second's suggested fix turned out not to be implementable as written.

1. --on-complete has never fired for a --command launch

The bootstrap never writes /tmp/SPAWN_COMPLETE — only pkg/taskproto/wrapper.go does. So nothing on the --command path ever signals completion, and --on-complete (whose help promises "Action when workload signals completion") was dead there. The instance ran to its full TTL whether the command succeeded or failed.

That's the expensive part: --on-complete terminate is the documented way to bound batch cost (cmd/launch_flags.go), and on this path it never worked. The reporter's 38 idle minutes weren't bad luck.

Now the command's completion is signalled on both paths, so --on-complete fires when the command exits. This is a behaviour change users will notice and it's called out in the changelog: boxes that used to sit until TTL now take their action. Anyone wanting to keep a failed instance uses --on-complete stop or omits the flag. No spored change was needed — Agent.checkCompletion triggers on file existence and reads the content only to log it.

2. A failed command reported success

su - "$USER" -c '/tmp/spawn-command.sh' 2>&1 | tee /var/log/spawn-command.log &
echo "✅ Command execution started (logs: ...)"

Backgrounded, exit code discarded, success printed unconditionally.

$? would not have fixed it. After cmd | tee log, $? is tee's status — 0 even when the command failed. Verified under bash:

bash, $? after pipe        = 0   (tee's status)
bash, ${PIPESTATUS[0]}     = 7   (the command's status)

So ${PIPESTATUS[0]} it is, written to /tmp/SPAWN_EXITCODE, with a pass/fail line printed once the answer is actually known.

The & is load-bearing — this is the important design constraint

spored is installed and started later in the bootstrap. De-backgrounding the command would block cloud-init, so spored would not start and TTL / idle / cost enforcement would all be unarmed for as long as the command runs — the spored#65 failure (never block the ticker startup path), and strictly worse than the bug being fixed.

So the exit code is captured in a backgrounded subshell. TestCommandStaysBackgroundedBeforeSpored asserts the block is both still before systemctl start spored and still backgrounded, so a future "simplification" of the & fails loudly. I verified it by de-backgrounding and watching it fail.

3. The instance couldn't read the caller's own bucket

cmd/task.go derives its policy from the spec's declared inputs/outputs (taskStagingPolicy). A --command is an opaque shell string — there is nothing to derive from, so the issue's "give the launch role the same bucket access the task path has" isn't directly implementable. Meanwhile a bare launch lands on SetupSporedIAMRole's static policy, whose S3 grants cover only spawn's own infrastructure buckets. Hence the 403.

New --s3-read / --s3-write (repeatable) give launch the same declarative capability, reusing taskStagingPolicy unchanged:

spawn launch job --command '...' --s3-read my-inputs --s3-write my-results
{"Effect":"Allow","Action":["s3:GetObject","s3:GetObjectVersion"],"Resource":["arn:aws:s3:::my-inputs/*"]},
{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation"],"Resource":["arn:aws:s3:::my-inputs"]},
{"Effect":"Allow","Action":["s3:PutObject"],"Resource":["arn:aws:s3:::my-results/*"]}

versus the previous options: a 403, --iam-policy s3:ReadOnly (Resource "*" — every bucket in the account), or the AmazonS3FullAccess the reporter resorted to. It attaches as its own spawn-scoped-policy document, so it is additive with --iam-policy/--iam-policy-file rather than replacing them. Wildcards, ARNs and s3:// URIs are refused — the name goes straight into a Resource ARN, so --s3-read '*' would quietly grant the account.

4 & 3 (docs)

--command's help now says it runs on the bare instance as the login user, not root (use sudo), that Docker and fuse are not installed unlike spawn task run, and that the instance reaches only spawn's own buckets without --s3-read/--s3-write/--iam-policy. All three cost the reporter a run.

Tests

  • TestCommandOutcomeIsCaptured — PIPESTATUS, SPAWN_EXITCODE, SPAWN_COMPLETE, and both outcome branches. Verified to fail when reverted to $?.
  • TestCommandSuccessClaimIsGone — gates the exact sentence that lied, like fix(fsx): stop promising synchronous ephemeral reaping; surface FSx in terminate and estimates (#613) #617's FSx promise gate. It caught my own explanatory comment quoting the old line, so I reworded the comment rather than loosening the gate.
  • TestCommandStaysBackgroundedBeforeSpored — the spored#65 guard above. Verified to fail when de-backgrounded.
  • TestBootstrapWithCommandIsValidBash — runs bash -n over the generated script for three command shapes including quotes and $. A syntax error here breaks every launch, and the new heredoc-inside-subshell is exactly that risk. (Worth noting: my first attempt put backticks in a comment inside the Go raw string and broke the Go build — the shell-level equivalent would have shipped silently.)
  • S3 flags — the scoped policy's exact ARNs, no wildcard resource, a read bucket not becoming writable, and refusal of wildcards/ARNs/URIs/invalid names with the error suggesting the bare bucket name.

Behaviour was also validated by running the subshell pattern directly: echo ok → 0/completed, exit 7 → 7/failed, false; echo unreachable → 1/failed with set -e stopping before the echo.

Gates

gofmt · go vet ./... · go build ./... · go test ./... all green · make check-docs clean with docs-gen/launch.md regenerated and committed · go mod tidy clean · staticcheck zero added, diffed by message (the line-number diff gave a false positive on the previous PR).

Not done

Real-AWS validation of the terminate-on-exit behaviour — a launch with --command 'exit 7', --on-complete terminate and a short TTL should terminate within the grace period instead of idling. Worth doing before release; it needs a billable instance so I'd rather you greenlight it, and it must come with an explicit terminate and a leak-check.

Suggested bump: MINOR — new flags, and --on-complete now firing on a path where it never did is a user-visible behaviour change.

…t reach your own S3

Three defects, all in the path a user takes for a one-shot headless run.

1. --on-complete never fired for a --command launch. Nothing on that path wrote
   /tmp/SPAWN_COMPLETE — only pkg/taskproto/wrapper.go did — so the instance ran to
   its full TTL whether the command passed or failed, even though --on-complete
   terminate is the documented way to bound batch cost. The reporter's box idled 38
   minutes after its command died in the first second.

2. A failed command reported success. It ran as a plain background job with its exit
   code discarded, so the success line printed unconditionally. The code now comes
   from PIPESTATUS[0]: after 'cmd | tee log', $? is TEE's status and is 0 even when
   the command failed, so using $? would have reported every failure as a success
   (verified under bash: $?=0 vs PIPESTATUS[0]=7).

   The capture happens in a BACKGROUNDED SUBSHELL, not by waiting. spored is
   installed and started later in the bootstrap, so blocking cloud-init here would
   leave TTL, idle and cost enforcement unarmed for the command's entire duration —
   the spored#65 failure, and worse than the bug being fixed. A test asserts the
   block stays both before spored's start and backgrounded.

3. The instance could not read the caller's own S3 bucket. spawn task run derives a
   least-privilege policy from its spec's DECLARED inputs/outputs; a --command is an
   opaque string with nothing to derive from, so 'give launch the same access' was
   not implementable as asked. New --s3-read/--s3-write give launch the same
   DECLARATIVE capability using the same policy builder (taskStagingPolicy), instead
   of the s3:ReadOnly (Resource '*') or AmazonS3FullAccess workarounds. Wildcards,
   ARNs and s3:// URIs are refused, since the name goes straight into a Resource ARN.

Also documents what --command's instance actually is: the login user, not root, with
no Docker or fuse — all three of which cost the reporter a run to discover.

Refs #614
@codecov

codecov Bot commented Oct 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.09091% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/launch_single.go 0.00% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit ab7ed32 into main Oct 2, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/614-command-outcome-and-s3 branch October 2, 2026 01:49
scttfrdmn added a commit that referenced this pull request Oct 2, 2026
Promote Unreleased to [0.113.0] and update compare links.

MINOR: new --s3-read/--s3-write flags, and --on-complete now fires on the --command
path where it previously never did — a user-visible behaviour change (#614, PR #630).
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.

1 participant