Skip to content

resolveInstance cannot see shutting-down or recently-terminated instances, so status/terminate report "not found" for a spore that is right there #736

Closed

Description

@scttfrdmn

Reported from the 2026-10-07 demo: spawn status chem-arm, run immediately after
that instance's job failed and it self-terminated, answered

no instance found with name: chem-arm

Accurate as worded, and unhelpful — the presenter was asking why it vanished.
Confirmed, and it has a second consequence that is worse than the message.

The mechanism

resolveInstance (cmd/utils.go:162) lists with an empty state filter:

instances, err := client.ListInstances(ctx, "", "")

and the empty filter means running and stopped only
(pkg/aws/client.go:1177-1182):

case "":
    // Default: show running and stopped instances (not terminated)
    filters = append(filters, types.Filter{
        Name:   aws.String("instance-state-name"),
        Values: []string{"pending", "running", "stopping", "stopped"},
    })

terminated and shutting-down are both excluded. EC2 keeps terminated
instances visible in DescribeInstances for roughly an hour, so the instance,
its state transition reason and its termination time were all available — the
lookup simply declined to ask for them.

The second consequence: unreachable code in terminate

cmd/terminate.go:100 has:

if instance.State == "shutting-down" {
    fmt.Fprintf(os.Stderr, "Instance %s is already shutting down — nothing to do.\n", ...)
    return nil
}

terminate resolves via resolveInstance (cmd/terminate.go:87), whose filter
excludes shutting-down. That branch can never execute. What actually happens
when you terminate an instance that is already terminating is the not-found path —
which, per #648, terminate deliberately treats as success and exit 0. So
re-terminating a shutting-down instance prints "no instance found" and reports
success, and the dead branch is what makes that look intentional in the source.

Suggested fix

  1. On a name miss, look again with "all" before reporting not-found, and use
    what comes back:
    • shutting-down → "chem-arm (i-…) is shutting down" and let terminate's
      existing branch finally work.
    • terminated → "chem-arm (i-…) terminated at 01:54" plus
      StateTransitionReason, which EC2 populates ("User initiated", "Client.
      InstanceInitiatedShutdown", a spot reclaim, …). That is the answer the
      presenter wanted.
    • still nothing → the current message, now meaning "no such instance,
      recently or otherwise".
  2. Keep retryIAM doesn't retry ConcurrentModification, so concurrent task run launches fail on CreateInstanceProfile #648's exit-code contract unchanged: a name that resolves to a terminated
    instance is still success for terminate. This is about what is said, not
    what is returned.

Related, and the harder half

Also reported, and I agree it is the more valuable gap: once the instance is gone,
its console output and /var/log/spawn-command.log are gone with it, so there is
no way to find out why an --on-complete terminate job failed. The exit code
survives in tags; the diagnosis does not.

pkg/taskproto/flush.go already does the right thing for the task path — it
uploads /var/log/spawn-command.log and a completion.json to the results
prefix in S3 (flush.go:125, :156). The gap is that a plain
spawn launch --command … --on-complete terminate has no results bucket and so
no flush. Options, roughly in order of cost:

  • Reuse the task flush for the launch path when a results bucket exists, and
    auto-create/derive one when --on-complete terminate is set — the destructive
    combination is exactly where the log is needed.
  • Failing that, tail the last N lines of spawn-command.log to the serial
    console
    on non-zero exit. GetConsoleOutput survives termination for a while,
    so this costs nothing and needs no bucket.

Both are bigger than the lookup fix above and probably want their own issue once
the direction is chosen; recording them here so the connection to "why did it
vanish" is not lost.

Environment: spawn v0.123.0, spored v0.124.0.

Activity

  1. added
    bug
    on Oct 8, 2026
  2. scttfrdmn commented on Oct 8, 2026

    @scttfrdmn
    ContributorAuthor

    Direction, with the console-retention caveat now measured instead of assumed

    I recommended tailing the command log to the serial console (option B) but flagged
    that there is no GetConsoleOutput call anywhere in this codebase, so the
    retention window was from AWS docs rather than measured here. I measured it
    against instances terminated earlier today.

    What survives

    $ aws ec2 describe-instances --instance-ids i-0155158cdece418bc
      state = None                      ← already aged out of DescribeInstances
    
    $ aws ec2 get-console-output --instance-id i-0155158cdece418bc
      12,848 bytes                      ← still returns real content
      "cloud-init" ×70,  "systemd" ×28
      ends: systemd-shutdown[1]: Powering off. / reboot: Power down
    

    So console output outlives the instance's visibility in
    DescribeInstances
    — a stronger result than I expected, and it covers the exact
    window where "why did it vanish?" gets asked.

    The part that changes the recommendation

    "spored" ×0    "spawn" ×0    "SPAWN" ×0
    

    The command log is not on the console. --command runs under su with its
    output teed to /var/log/spawn-command.log, which never touches the console
    device. So option B is not "read what is already there" — it needs a write:
    the instance must push the tail to /dev/console before terminating.

    That is a change to the generated bootstrap, not a read-only CLI addition. Still
    small, still needs no bucket and no IAM, but I described it as cheaper than it is.

    Revised recommendation — still B, now with a size budget

    spored's pre-terminate path writes the last ~50 lines of
    /var/log/spawn-command.log to /dev/console, framed so it is findable:

    echo "=== spawn command log (tail) ===" > /dev/console
    tail -50 /var/log/spawn-command.log > /dev/console 2>/dev/null
    echo "=== end spawn command log ===" > /dev/console
    

    A 50-line tail is a deliberate budget, not a guess: console output is capped
    (~64 KB), the measured baseline boot already consumes ~13 KB of it, and the
    failure reason is almost always in the last few lines. Writing the whole log
    would risk evicting the cloud-init messages that are also diagnostic.

    spored already has a pre-terminate hook — the same place pkg/taskproto/flush.go
    uploads the log on the task path — so this is one more step there, gated on a
    non-zero exit so a successful job writes nothing.

    Ordering against A (always upload to S3) is unchanged and the measurement
    reinforces it: A is the only option that worsens the un-reaped spawn-results-*
    problem, and it does so on every launch rather than on the failures you care
    about. If A is ever wanted it should ship with the S3 lifecycle policy #653 lists.

    C (upload only on non-zero exit) remains the right second step if the 50-line
    tail proves too thin in practice — better to learn that from real failures than
    to assume it now.

    D (refuse --on-complete terminate without a results bucket) stays wrong: it
    converts a missing diagnostic into a launch failure.

  3. scttfrdmn commented on Oct 8, 2026

    @scttfrdmn
    ContributorAuthor

    Probe result: console writes DO surface — option B is viable

    Measured on a real instance: wrote a nonce to /dev/console over SSM, terminated,
    then polled get-console-output.

    t+45s   size=0      nonce_hits=0
    t+90s   size=0      nonce_hits=0
    t+135s  size=0      nonce_hits=0
    t+180s  size=0      nonce_hits=0
    t+225s  size=0      nonce_hits=0
    t+270s  size=16842  nonce_hits=1   ← the nonce is there
    

    A userspace write to /dev/console does appear in GetConsoleOutput, so
    option B works. Combined with the earlier measurement — console output outlives
    the instance's visibility in describe-instances by hours — the whole path is
    sound.

    I nearly called this a failure

    My first read was at ~3 minutes and returned 0 bytes, and I reported the probe
    as negative. That was wrong, and the mistake is worth recording because it is the
    same one I made earlier in this backlog: treating an empty result as a negative
    result. A 0-byte console is not "the nonce is missing", it is "nothing has been
    captured yet".

    Two facts that only emerged from waiting:

    • The post-termination capture takes ~4–5 minutes to populate. Before that,
      get-console-output returns nothing at all for a just-terminated instance.
    • The variation I saw earlier between instances (12,848 bytes vs 0) was entirely
      this lag — the 12 KB reads were on instances terminated hours before.

    What that means for the implementation

    Option B as planned, with one documented caveat: the log is not readable for
    roughly five minutes after termination.
    That is fine for "why did it vanish?",
    which is asked minutes-to-hours later, but it means the CLI must not promise an
    immediately-readable log. Any message pointing a user at
    aws ec2 get-console-output should say to allow a few minutes.

    Still a 50-line tail, for the reason already established: console output is capped
    around 64 KB and a baseline boot consumes 13–17 KB, so dumping the whole log risks
    evicting the cloud-init messages that are also diagnostic.

    Option C is no longer needed. It stays the fallback if the console path turns out
    to be unreliable in a way one instance cannot show.

  4. added 6 commits that reference this issue on Oct 8, 2026
    fcac400
    98773ba
    d7c0448
    faff742
    0704463
    c91b1d8
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