Skip to content

fix(spored,reaper): make the task terminal flush visible, retried and covered by the reaper (#1186) - #1294

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1186-task-flush-reliability
Oct 24, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1186-task-flush-reliability

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1186

The TTL-killed task in #1186 left an empty results prefix and no trace of why: the flush hook's output and status went only to spored's journal, which goes with the instance. This makes every flush outcome survive the instance, makes the hook retry, closes an interlock hole, bounds the hook's pipes, and has the reaper wait for a live spored and run the flush itself when it does terminate a task.

spored (pkg/agent)

  • Every outcome is logged in full (output plus status) and written to the spawn:flush-status tag: ok, failed:{hook-refused,timeout,upload-failed,exit-N,start-failed,killed}, or skipped:hook-missing (only reported on a task instance, meaning spawn:task-id is set; a plain launch stays silent).
  • On a failure or skip, a framed block (=== task flush report: … ===) with the reason and the hook's output goes to /dev/console, followed by the command-log tail (writeCommandLogToConsole). A notification is also sent. It goes out as pre_stop_failed with detail task flush <status> — <output tail>, not as a new event: TestSporedNotifyEventsHandledBySporeBot only allows events spore-bot formats, and a new one would need a spore-host change first.
  • exec.Cmd.WaitDelay (5 s) on the hook, so an aws child it leaves holding the pipe can't stall termination past the 45 s cap.
  • The tag write has a 5 s timeout, the console write is a local write, and the notification is fire-and-forget.

Flush hook (pkg/taskproto/flush.go)

  • spawn_up retries each upload: 3 attempts, 1 s/2 s backoff, no new attempt after 25 s, plus --cli-connect-timeout 5 --cli-read-timeout 15. On the last failure it prints the aws error text. A stdin upload is buffered first, so a retry doesn't send an empty body.
  • completion.json and .exitcode still upload when command.log fails. The hook exits FlushExitUploadFailed (3) if any upload didn't land.
  • Interlock: if the local wrapper record exists but head-object can't find it in S3, the hook now uploads the wrapper's own record (plus the log and .exitcode) instead of exiting 0. It never writes its own record in that case, and it won't upload a local copy that doesn't end in } (the 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 #1179 full-/tmp case). Note: task roles have only s3:PutObject on the results bucket, so head-object normally gets a 403, and the hook re-uploads the wrapper's identical bytes.
  • writeRecordFile gains an onFail argument (the wrapper passes true, which is unchanged) because a piped upload runs in a subshell.

Reaper (lambda/ttl-reaper)

  • REAPER_TTL_GRACE (default 10m, 0–1h, 0 = old behaviour): a running instance with a fresh spawn:last-heartbeat isn't reaped until the grace after its deadline. This covers both spawn:ttl-deadline and the launch-time+ttl fallback. A stale or missing heartbeat, or a stopped instance, is reaped at the deadline, and max-age still applies. It's capped because the heartbeat tag is instance-writable.
    • Wired through all three deploy paths, the same way as ElasticIps: template TtlGrace (default ''), Makefile PARAM_MAP / echo list (no ?= default, so live values win), spawn reaper deploy --ttl-grace via reaperconfig.Settable, and the census tests.
  • Before terminating a running spawn:task-id instance, the reaper runs taskproto.FlushCommand(ExitTTLExpired) over SSM, bounded at 60 s. This is the command spawn terminate already sent, now moved to taskproto and shared. It runs whatever REAPER_GRACEFUL is set to. ssmFor now returns an ssmAPI interface so this can be tested.
  • IAM: nothing added. ssm:SendCommand (instance on the ssm:resourceTag/spawn:managed rail, plus document/AWS-RunShellScript) and ssm:GetCommandInvocation are already in the SAM template's ScanSelf branch, the cross-account role and pkg/reaperiam.

Sizes

For a trivial spec, the flush hook (embedded in task user data) grows from 1,070 to 1,538 bytes gzipped. The wrapper is unchanged (1,760 to 1,762, which is just temp-path noise). The user-data budget tests in pkg/launcher and pkg/tasklaunch pass.

Gates, each watched failing against a revert that compiles

  • Hook failure/skip reaches console + tag + notification: reportFlushOutcome returning early fails TestFlushOutcomeIsVisibleAfterTermination (all 4 cases), TestFlushSuccessIsTaggedQuietly and the Monitor test.
  • Upload retry: flushUploadAttempts = 1 fails TestFlushHookRetriesAFailedUpload.
  • Persistent failure: making the final exit 0 fails TestFlushHookReportsAPersistentUploadFailure. Making a failed upload exit 3 immediately fails it too (the record wasn't uploaded after the log failed).
  • Interlock: restoring the exit 0 fails TestFlushHookUploadsALocalOnlyRecord. Removing the } guard fails TestFlushHookDoesNotUploadATruncatedLocalRecord.
  • WaitDelay: with _ = a.flushWaitDelay() in place of cmd.WaitDelay = …, TestFlushHookOutputPipeIsBounded fails. It passes when flushTaskRecord returns while the hook's child is still alive; there's no wall-clock bound on the pass path.
  • TTL to hook, end to end: removing a.flushTaskRecord(why) from runPreStop fails TestMonitorTTLExpiryRunsTheFlushHookBeforeTerminate. The provider's Terminate records what the hook had done by then.
  • Reaper grace: ignoring the heartbeat, never granting the grace, or granting it to stopped instances each fails TestTTLGraceOnlyForARunningInstanceWhoseSporedIsAlive.
  • Reaper SSM flush: not calling it, flushing all instances, or flushing stopped ones each fails TestReaperFlushesOnlyTaskInstancesBeforeTerminating.
  • Preservation/census: dropping TtlGrace:TTL_GRACE from PARAM_MAP fails TestEveryTemplateParameterIsInThePARAMMAP and TestTtlGraceSurvivesAMakeDeploy. A typo in the env read fails TestTemplateEnvironmentIsReadByTheReaper. Dropping it from reaperconfig.settable fails TestReaperDeployFlagsBecomeSettings and TestEveryReaperSettingSurvivesARedeploy.

Checks

  • make check passes every step except check-build-tags, which fails the same way on unmodified main (the /private/tmp paths). golangci-lint reports 0 issues in all 10 modules.
  • make check-docs and semgrep scan --config=auto --error . find nothing (0 findings).
  • make api-snapshot: additions only (FlushCommand, FlushHookSeconds, FlushExitUploadFailed).

No deploy, no AWS calls. The reaper change needs a redeploy; the SSM grants are already in place but should be IAM-simulated at deploy.

… covered by the reaper (#1186)

spored: every flush outcome is logged in full and recorded in a
spawn:flush-status tag; a failure or skip (missing hook on a task
instance, refused hook, timeout, failed uploads, other non-zero exit)
also goes to the serial console with the hook output and command-log
tail, and is sent as a pre_stop_failed notification. The hook's output
pipes are bounded with exec.Cmd.WaitDelay.

Flush hook: each upload is retried (3 attempts, backoff, 25 s budget,
connect/read timeouts), the aws error is printed on final failure, the
record still uploads when command.log fails, and the hook exits 3 when
an upload did not land. A wrapper record that exists locally but not in
S3 is uploaded instead of ending the hook (an incomplete local copy is
not).

Reaper: REAPER_TTL_GRACE (default 10m, 0..1h) leaves a running instance
with a fresh heartbeat for spored past its deadline; threaded through
the template, Makefile PARAM_MAP and spawn reaper deploy --ttl-grace
with deployed-value preservation. Task instances get the flush hook run
over SSM (taskproto.FlushCommand, shared with spawn terminate) before
the terminate.

Fixes #1186
@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.23129% with 32 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/taskflush.go 76.31% 12 Missing and 6 partials ⚠️
pkg/taskproto/flush.go 86.04% 6 Missing ⚠️
pkg/reaperconfig/reaperconfig.go 63.63% 4 Missing ⚠️
pkg/agent/consolediag.go 60.00% 2 Missing ⚠️
pkg/provider/ec2.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 76ebc33 into main Oct 24, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1186-task-flush-reliability branch October 24, 2026 06:38
@scttfrdmn scttfrdmn mentioned this pull request Oct 25, 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.

TTL-killed task on 0.135.1 left nothing under its results prefix: no command.log, no completion.json (terminal-flush hook didn't fire?)

1 participant