Skip to content

--pre-stop does not reliably flush before TTL-triggered terminate → data loss on ephemeral storage #184

Closed as not planned

Description

@scttfrdmn

Summary

A --pre-stop hook did not run (or did not complete effectively) when an instance was
terminated by TTL expiry, causing total loss of the job's output. --pre-stop exists
precisely to flush results before a lifecycle-triggered stop/terminate — but on the TTL path
it didn't save the data, which is the exact scenario it's meant to protect.

What happened

  • spawn launch ... --ttl 48h --pre-stop 'aws s3 sync ~/output s3://bucket/...'
  • Long-running GPU job (SAM inference) was still in progress at 48h.
  • At exactly T+48h, spawn terminated the instance ("User initiated" per StateTransitionReason).
  • The pre-stop sync did not land any files in S3 — output was lost.
  • Output lived on the instance-store NVMe (ephemeral, wiped on terminate), so there was no
    second chance to recover.

Why this is a data-loss bug, not just user error

The whole point of --pre-stop is "run this before any lifecycle-triggered stop/terminate so
results are flushed." A TTL expiry IS a lifecycle-triggered terminate. If pre-stop doesn't
reliably run-to-completion on the TTL path, the hook gives a false sense of safety.

Questions for maintainers:

  1. Does the TTL reaper invoke --pre-stop at all, and does it WAIT for it to complete before
    issuing the terminate? (The spawn#70/Idle-stopped instances have no daemon → TTL can never fire locally; they bill EBS indefinitely #71 out-of-band reaper may terminate without going
    through spored's pre-stop path.)
  2. If pre-stop ran, did it race the instance-store teardown / lose network before finishing a
    multi-GB sync?
  3. --pre-stop-timeout default is 5m — a large sync can exceed that. Is the timeout enforced
    as "kill the sync and terminate anyway"? If so, partial/zero sync on big outputs is expected
    and should be documented loudly.

Suggested

  • Guarantee --pre-stop runs to completion (up to --pre-stop-timeout) on the TTL-terminate
    path, same as on idle/completion paths.
  • If pre-stop can't be guaranteed on TTL-terminate (e.g. the out-of-band reaper bypasses spored),
    document that explicitly — pre-stop is then NOT a safety net for TTL, only for idle/on-complete,
    and users must not rely on it for TTL.
  • Consider a T-minus warning event (like the idle/ttl warnings spored already emits) that gives
    pre-stop a head start before the hard terminate deadline.

Impact

Lost 48h of g5.12xlarge compute ($270) and ~110k detections on a science workload, because
the data was on ephemeral storage and pre-stop didn't flush it before TTL terminate. Mitigations
on our side (persistent EBS, periodic incremental S3 upload, longer TTL) are real and we'll apply
them — but pre-stop silently not protecting the TTL case is the spawn-side gap.

Version

spawn v0.45.1

Activity

  1. added
    bug
    on Jun 16, 2026
  2. scttfrdmn commented on Jun 16, 2026

    @scttfrdmn
    ContributorAuthor

    Correction — this is a dup of #63 (pre-stop runs as root), not a 'pre-stop didn't run on TTL' bug

    Dug further. The pre-stop hook did run on the TTL-terminate path — my original framing was wrong. The actual failure is #63: pre-stop runs as root, so ~ resolved to /root, and our hook aws s3 sync ~/output s3://... synced the empty /root/output instead of the real output at /home/ec2-user/output (symlinked to the instance-store /scratch/output). It 'succeeded' syncing nothing.

    So:

    This is exactly the least-surprise failure #63 predicts ('Wrong $HOME / environment vs. how the workload ran'). Closing this as a duplicate of #63 — but flagging that #63 is higher-severity than 'consistency/ownership': it causes silent data loss for the canonical pre-stop use case (aws s3 sync ~/results ...), which is the flag's own documented example. Recommend bumping #63's priority accordingly.

    Workaround until #63 lands: use absolute paths in --pre-stop (aws s3 sync /home/ec2-user/output ...), never ~ or relative paths.

  3. scttfrdmn commented on Jun 16, 2026

    @scttfrdmn
    ContributorAuthor

    Duplicate of #63 (pre-stop runs as root). See analysis above.

  4. scttfrdmn commented on Jun 16, 2026

    @scttfrdmn
    ContributorAuthor

    Dug into the code. There are two TTL-terminate paths with different pre-stop guarantees, and your three questions land exactly on the seam:

    Path 1 — spored (in-instance): pre-stop does run to completion

    TTL expiry in the monitor loop calls a.terminate(ctx, "TTL expired") (pkg/agent/agent.go:337), and terminate() runs runPreStop() synchronously before provider.Terminate() (agent.go:1224-1243). runPreStop blocks on cmd.Run() up to the timeout, then proceeds. So when spored is healthy, pre-stop is honored on the TTL path — same as idle/on-complete.

    Path 2 — ttl-reaper (out-of-band Lambda): pre-stop is bypassed entirely

    The #70/#71/#72 backstop reaper calls TerminateInstances directly (lambda/ttl-reaper/main.go terminate()) — no pre-stop, no SSM RunCommand, no grace (git log -S SendCommand/PreStop on that dir is empty — it never did). By design it's a dumb "kill it no matter what" backstop for when spored is dead. So your Q1 is the real gap: if the reaper does the terminating, pre-stop never runs. Confirmed.

    Which path hit you — and why I suspect it's actually the timeout, not the reaper

    Two things point away from the reaper for your specific run on v0.45.1:

    • The reaper was deployed in dry-run (logs "WOULD reap", doesn't terminate) at that vintage — so it likely wasn't the one issuing your terminate.
    • That leaves spored's own terminate() → which does run pre-stop. Which makes Q3 the likely culprit: --pre-stop-timeout defaults to 5 minutes, and runPreStop enforces it as "kill the sync and terminate anyway" (agent.go:1212-1219: on DeadlineExceeded it logs "timed out — proceeding with shutdown" and returns). A multi-GB aws s3 sync of a 48h SAM run almost certainly blew past 5 min → spored killed the sync mid-flight and terminated → partial/zero objects in S3, and the NVMe was wiped. That matches "pre-stop did not land any files" far better than a silent bypass would.

    StateTransitionReason: "User initiated" is consistent with either an API terminate (reaper) or spored's own provider.Terminate call — it doesn't disambiguate.

    The one diagnostic that settles it

    The instance is gone, but spored's log is the tell if you captured it (it streams to the console/serial log, and if your --pre-stop itself didn't redirect, CloudWatch agent/console). Look for one of:

    • Running pre-stop hook (timeout: 5m0s): aws s3 sync … followed by Pre-stop hook timed out after 5m0s — proceeding with shutdown → timeout (Q3), or
    • no Running pre-stop hook line at all near the end → reaper bypass (Q1).

    If you don't have the log, the timeout is the safer bet to fix first.

    Suggested fixes (I'll implement once you confirm direction)

    1. Timeout (almost certainly your case): the 5-min default is dangerous for "flush a large output" hooks. Options: raise the default, or — better — document loudly that pre-stop is bounded and a multi-GB sync needs an explicit --pre-stop-timeout (and ideally periodic incremental upload, which you noted). I'd also add a pre_stop_timeout warning notification when it fires so the truncation isn't silent.
    2. Reaper bypass (the genuine architectural gap): give the reaper a graceful tier — before TerminateInstances, fire pre-stop via SSM RunCommand with a bounded wait, then terminate regardless. Keeps the hard backstop but tries the flush first. More work; only matters when spored is actually dead.
    3. Either way: document that pre-stop on TTL is best-effort within the timeout and is NOT a substitute for incremental persistence on ephemeral storage.

    Want me to start with the timeout hardening + docs (covers your most-likely case and is low-risk), and file the reaper-graceful-tier as a separate tracked enhancement? Also: do you have the spored log line from that instance? It'd confirm which path before I touch code.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions