Skip to content

fix(taskproto): never upload an empty completion record when /tmp is full (#1179) - #1279

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1179-empty-record
Oct 23, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1179-empty-record

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1179

Problem

The task wrapper (pkg/taskproto/wrapper.go) and the terminal-flush hook (pkg/taskproto/flush.go) wrote completion.json and .exitcode to /tmp with cat > file <<JSON / printf > file, then ran aws s3 cp file ... without checking the result. On a container task /tmp is also the staging tmpfs. When the task filled it, both writes left 0-byte files, and those empty files were uploaded. spawn task status then failed with parse completion record: unexpected end of JSON input, even though the failure was recorded in command.log.

Change

  • Writers (wrapper, pooled job script, flush hook). The record is now built in memory with printf -v REC <format> <args>. That is a bash builtin, so it needs no fork, no heredoc temp file and no disk. The Go-quoted task_id/run_id JSON is passed as printf arguments and never as part of the format. A new helper, writeRecordFile, emits the same three steps for each of the four record files: write the local copy, read it back and compare it with the value in memory, then upload the local copy if it matches. If it doesn't match, it prints a warning and streams the value with printf '%s' "$REC" | aws s3 cp - <dest>. The fallback uploads the full record (the same bytes), not a cut-down one, so state/exit_code/retry_class (and terminal_reason from the hook) stay accurate. The local file is still written, so the flush hook's [ -f completion.json ] interlock works as before. The JSON fields have the same names and order as the old heredoc. The only differences are that the record has no trailing newline, and a backslash in a task id is no longer eaten by heredoc expansion.
  • Reader. tasklaunch.ReadCompletionAt, the one reader that task status, task run --wait and the MCP server all use, now returns a new tasklaunch.ErrEmptyCompletionRecord for an empty or whitespace-only object. The error reads completion record is empty at <uri> (the instance's disk was probably full when the task ended); see the task's log at s3://.../command.log. It is not ErrNoCompletionRecord, so --wait returns at once and does not keep polling.
  • api/public-surface.txt: one addition, var ErrEmptyCompletionRecord (make api-snapshot). Nothing was removed or changed.
  • CHANGELOG: new ### Fixed entry under [Unreleased].

User-data budget

Measured with gzip on the test fixtures: the wrapper grew from 2139 to 2268 bytes (+129) and the flush hook from 975 to 1084 bytes (+109). TestBareBootstrapFitsItsBudget and the tasklaunch user-data limit tests pass.

Gates, each shown failing against a compiling revert

  • TestWrapperUploadsAValidRecordWhenTheLocalCopyCannotBeWritten and TestFlushHookUploadsAValidRecordWhenTheLocalCopyCannotBeWritten (pkg/taskproto/fulldisk_test.go). Each test leaves the local record files present, empty and read-only, which is the state a full disk leaves them in. It then runs the generated script under bash with a stub aws that stores every s3 cp upload, reading stdin when the source is -. Both tests check the uploaded objects against the documented record: wrapper failed/7/app_error with task_id and run_id, .exitcode = 7; hook failed/-1/ttl_expired/terminal_reason=ttl_expired, .exitcode = -1. They skip on Windows like their neighbours, and also skip as root, because root ignores the file mode.
    • With wrapper.go and flush.go reverted to origin/main (stashed; tests compile and run): both fail with uploaded completion.json is not a record (parse completion record: unexpected end of JSON input): "", the exact error from the issue.
    • With only the .exitcode fallback disabled (the condition replaced by true): both fail with uploaded .exitcode = "", want "7" / want "-1".
  • TestReadCompletion_EmptyRecord (pkg/tasklaunch) checks that "" and "\n" return errors.Is(ErrEmptyCompletionRecord), are not ErrNoCompletionRecord, return no record, and that the error names .../command.log. With the check disabled (if false && ...): it fails with ... parse completion record: unexpected end of JSON input, want errors.Is ErrEmptyCompletionRecord.

Two existing structural tests matched the old heredoc text and were updated to the new form while keeping their intent. TestGenerateWrapper_StampsRunID now checks that the run id is the printf -v REC argument matching the format's "run_id": %s; TestGenerateWrapper_RunIDKeepsCompletionJSONValid, which runs the script, still parses the run id from the written record. TestFlushHookStagingFileIsNotTheInterlockFile now matches the write to the staging path.

Checks run

  • GOTOOLCHAIN=go1.26.10 go test -short ./...: all pass. This includes the shellcheck test on the generated scripts (shellcheck is installed locally).
  • make lint: clean in all 10 modules.
  • make api-snapshot: diff reviewed, one addition.
  • go test -run Changelog .: pass.
  • make gen-docs: not needed, no help text changed.

Not in this PR

  • An empty record still makes spawn task status --check-complete exit 3 (error), not 1 (failed). An empty record proves the task ended, but not whether it succeeded, so it is reported as an error with a pointer to the log.
  • The plain --command bootstrap in pkg/launcher, which writes /tmp/SPAWN_COMPLETE with cat > <<JSONEOF, has the same shape but is not part of the task record contract, so it is left alone.
  • Moving the local record off /tmp (the issue's first suggestion) is deliberately not done, because spored shares /tmp (spawn#66).

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…full (#1179)

The task wrapper and terminal-flush hook wrote completion.json and .exitcode
to /tmp and uploaded the files unconditionally. A task that filled /tmp left
both empty, and the empty objects were uploaded, so `spawn task status` failed
with "unexpected end of JSON input".

Both scripts now build the record in memory (printf -v), write the local copy,
read it back, and when it does not match stream the record to S3 with
`aws s3 cp -`, which needs no local disk. tasklaunch.ReadCompletionAt reports
an empty object as ErrEmptyCompletionRecord, naming the task's command.log.
@scttfrdmn
scttfrdmn force-pushed the fix/1179-empty-record branch from e82afe7 to 2119f0b Compare October 23, 2026 21:58
@scttfrdmn
scttfrdmn merged commit d293b47 into main Oct 23, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1179-empty-record branch October 23, 2026 22:19
@scttfrdmn scttfrdmn mentioned this pull request Oct 23, 2026
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.

A task that fills /tmp uploads a 0-byte completion.json and .exitcode; status then fails 'unexpected end of JSON input' and --wait never returns

1 participant