Skip to content

Status for stopped instances from EC2; compute kept across stop/start; stop's TTL message (#928) - #947

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/928
Oct 17, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/928

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #928
Stacked on #916 (fix/896). Review and merge that first; this PR's base is fix/896.

1. spawn status on a stopped instance SSHed to localhost

Cause. status always took the SSH path. A stopped instance has no public IP, so the destination was ec2-user@. ssh resolves an empty host to the local machine, so it reached the operator's own sshd (::1) and offered it their keys. Eleven call sites built a destination from PublicIP with no check.

Fix.

  • Only a running instance is asked. stopped, stopping, pending and other states are rendered from EC2 data and tags, with no SSH and no SSM:
    • state, when it stopped and why (StateTransitionReason, StateReason)
    • the TTL deadline in Cost spot instances at their spot price in spawn status (#896) #916's format (local time with UTC), or "past due"
    • cumulative compute across all boots and its cost
    • EBS since launch, and the EBS accruing while stopped (per hour, day and month, plus this stop so far)
    • --output json gives the same facts with "source": "ec2". --check-complete exits 3 (unknown) without connecting.
  • A running instance with no public IP now goes to SSM.
  • New pkg/sshtarget: every ssh/scp client starts through sshtarget.Command. It parses the argv the way ssh(1) and scp(1) do and refuses an empty, localhost/*.localhost, loopback or unspecified host. That covers the destination, every -J jump host and every scp remote operand, plus a host that would be read as an option. All 13 exec sites were moved to it (connect, status, config, extend, queue, plugin ×2, arraygroup, service upload ×2, pkg/service exec and tunnel).
  • spawn extend on a stopped instance no longer tries to reach spored. Its failure hint used to suggest ssh user@ with no host.
  • aws.InstanceInfo.StateReason was added (additive; make api-snapshot committed).

2. Compute from earlier boots was dropped after a restart

Cause. The carry-over existed: spawn:compute-seconds is read at boot and flushed in Cleanup on SIGTERM. But it had three weaknesses:

  • The only persistence was one network write during the shutdown a stop triggers. It was attempted once, and its error was discarded.
  • Every boot counted from spored's start, so the OS and cloud-init time EC2 bills was never counted.
  • Cost spot instances at their spot price in spawn status (#896) #916's status computed the boot gap from the original launch to this boot's spored start. On a restarted instance that billed the stopped interval as boot time.

Fix.

  • Durable checkpoint. It lives on the root EBS volume (/var/lib/spored/compute-checkpoint.json). The record holds instance id, boot_id, base, the boot's count start and total. It is written at daemon start, every tick, and first thing in Cleanup, before any network call. The write is atomic and fsynced.
  • Tag flush. It now goes through the agent's tag writer and is retried 3x within Cleanup's 10s context. Failures are logged.
  • On boot (resolveComputeStart):
    • A checkpoint from an earlier boot carries the larger of its total and the tag, and the new boot counts from kernel boot (/proc/uptime, clamped to launch).
    • A checkpoint from this same boot (an upgrade or Restart=on-failure) resumes the boot's count, so nothing is counted twice.
    • With no checkpoint and a non-zero tag (upgrading from an older spored), it counts from spored's start, as before. That can undercount but never counts twice.
  • Status. The boot gap applies to the first boot only. A restarted instance's stopped interval is reported as stopped (EBS-only), not billed as compute, so the effective rate reflects stop/idle/restart.
  • Windows / non-Linux. There is no boot id, so there is no checkpoint. The tag path is unchanged and still flushed on service stop.

3. spawn stop said the TTL countdown pauses

It does not: the deadline is absolute (docs/safety: "It never moves across stop/wake cycles").

  • stop and hibernate now print The TTL deadline still applies: 2026-10-15 11:42 MDT (18:42 UTC), in 59m. It keeps counting while the instance is stopped. start prints the same and says the stop did not extend it. If the deadline has already passed, the message says starting terminates the instance.
  • spawn start printed "Restoring TTL: … (was paused during stop)" and overwrote spawn:ttl with the remaining time. pkg/ttl defines that tag as the total from launch, so the overwrite changed its meaning (and shortened the lifetime of pre-deadline-tag instances). stop no longer saves spawn:ttl-remaining, and start no longer writes spawn:ttl.
  • The stop/hibernate/start help (and docs-gen/) carry one shared statement. A gate scans every cmd/ string literal, plus docs-gen/, docs/ and README, for a TTL pause/resume claim, and requires the statement on the three reference pages. I checked the spore-host docs site by hand (safety.md, tools/spawn.md, go-library.md): they already agree that the deadline doesn't move.

Tests

All tests are unit tests with fakes. No AWS resources were created and nothing was launched.

Each new test was watched failing against a revert that compiles (rule 1). The assertions come from the issue and the docs:

Part Revert Test that fails
1 statusNeedsInstance returns true for any state TestStatus_OnlyARunningInstanceIsAsked
1 drop the empty-host check TestCommand_RefusesTheStoppedInstanceArgvFromTheIssue (the issue's exact argv)
1 drop the loopback check TestCheckHost
1 exec.Command("ssh") back in status.go (aliased os/exec import too), and service.go's runQuietly back on raw exec TestEverySSHExecGoesThroughTheGuard (AST scan of every non-test Go file, wrappers included; it fails if discovery finds nothing)
2 ignore an earlier-boot checkpoint TestResolveComputeStart_CarriesEarlierBootsAcrossAStop (issue timeline: first run + 22s, not 22s)
2 no checkpoint write in Cleanup TestCleanup_PersistsComputeWhenTheTagWriteFails
2 1 tag attempt TestCleanup_PersistsComputeWhenTheTagWriteFails
2 #916's launch-anchored boot gap TestStatusReport_RestartedInstanceDoesNotBillTheStop
2 first boot counted from spored start TestResolveComputeStart_FirstBoot
3 old notice text TestStopNotice_SaysTheDeadlineStillApplies
3 old "TTL countdown will pause" line re-added TestStopStartAgreeWithTheDocsThatTheDeadlineDoesNotPause
3 startTags writes spawn:ttl TestStopStartDoNotTouchTheTTL
3 statement removed from docs-gen/stop.md TestStopStartAgreeWithTheDocsThatTheDeadlineDoesNotPause

Also covered: same-boot restart doesn't count twice, tag-only upgrade path, foreign or unidentified checkpoint ignored, larger of tag and checkpoint wins, stopped JSON, past-due deadline, unknown compute shown as —.

make check passes except TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker, which needs a Docker daemon (not running locally). make check-nested passes (9 modules). GOOS=linux and GOOS=windows vet are clean. make gen-docs leaves no diff.

Caveats

Follow-ups filed: #945 (hibernation), #946 (TestSporedUnit_NoPrivateTmp lists files and misses two units in cmd/app.go), spore-host/spore-host#554 (safety.md says --cost-limit is compute-only; spored enforces compute + EBS).

@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

@scttfrdmn
scttfrdmn force-pushed the fix/928 branch 4 times, most recently from aad10a0 to f064d2c Compare October 17, 2026 02:17
…p/start, fix the TTL-pause message (#928)

- status: a non-running instance is rendered from EC2 data and tags (state,
  stopped since and why, TTL deadline, compute and cost so far, EBS accruing
  while stopped). No SSH or SSM. A running instance with no public IP uses SSM.
- pkg/sshtarget: every ssh/scp client starts through one guard that refuses
  empty, loopback and unspecified hosts (destination, -J, scp operands). A
  gate fails the build on any ssh/scp exec elsewhere, wrappers included.
- spored: durable compute checkpoint on the root volume, written every tick
  and first on shutdown. The compute-seconds tag flush is retried and
  logged. Each boot counts from kernel boot without double-counting a
  same-boot restart. A restarted instance's billed time excludes the stop.
- stop/hibernate/start: say the TTL deadline still applies (local time with
  UTC). start no longer overwrites spawn:ttl. Help text and docs-gen agree,
  and that agreement is gated.
- pkg/aws: InstanceInfo.StateReason (additive; api snapshot updated).

(#928)
… instant

Sampling in buildStatusReport takes seconds on Windows, so the 91-94s bound
flaked there (95s). Same window pattern as TestStatusReport_QueryCountsTheDaemonsRunningTime (#973). (#928)
@scttfrdmn
scttfrdmn merged commit 7f31144 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/928 branch October 17, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant