Skip to content

Reset spored's per-boot state when the instance resumes from hibernation (#987) - #1020

Merged
scttfrdmn merged 3 commits into
mainfrom
fix/987
Oct 17, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
fix/987

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #987. Stacked on #1009 (fix/979) → #1005 → #986 → #977 → #969 → #960 → #955 → #936. The base is fix/979, so the diff shows only #987.

What was wrong

A hibernated instance resumes the same spored process, and spored carried its pre-hibernate state into the new run:

  1. preStopDone and taskFlushDone stayed set, so the next stop, hibernate or TTL terminate skipped the user's pre-stop hook and the task record (the data-loss shape of --pre-stop does not reliably flush before TTL-triggered terminate → data loss on ephemeral storage #184/pre-stop: surface a loud signal when the hook transfers nothing / exits non-zero #186).
  2. Cleanup had deleted the DNS name. Only the hourly renewal put it back, and with identity.PublicIP from the first boot.
  3. lastActivityTime was from before the hibernation, so the first idle tick re-hibernated within about a minute.
  4. The plugins Cleanup stopped stayed stopped.

A fifth case the issue doesn't list: after --on-complete hibernate, the completion file is still on disk, so the first tick after the resume hibernated the instance again.

Detection (pkg/agent/resume.go)

checkResume runs at the start of every monitor tick (the first call in Monitor sets the baseline) and compares this tick's clocks with the last tick's. I chose this over a systemd sleep hook: a hook is another unit to install and only helps if it runs. Two independent signals:

resumedBetween is a pure function and is table-tested, including the near-misses (clock-read noise, a 30 s NTP step, a backwards step).

What a resume resets (onResume)

  • preStopDone and taskFlushDone: the next exit runs the hook and writes the record again.
  • lastActivityTime = now, and idleWarned = false.
  • The failed-stop backoff (A failed --on-complete stop/hibernate repeats the completion notification, webhook, grace period and stop on every tick #978).
  • The CPU, network and disk baselines, so the first delta does not span the hibernation. The first tick reads "unknown", which counts as active.
  • A completion file already acted on is marked spent (by mtime). checkCompletion ignores it, and a completion written after the resume is acted on as usual.
  • The public IP is re-read from IMDS (lookupPublicIP is a seam for tests). It is kept in a guarded currentIP, read through publicIP(). identity is not mutated, because the provider, the plugin runtime and off-loop goroutines share it unguarded. The DNS register and delete calls and the ready-url hosts now read publicIP().
  • restoreAfterCleanup (renamed from restoreAfterFailedStop) then re-registers DNS with the new IP and the job-array entry, and restores the plugins with ResumeStopped (A failed stop/hibernate leaves every plugin marked stopped on a running instance (Cleanup's StopAll is not undone) #979). When the hibernate was started outside spored, Cleanup didn't run and these are idempotent refreshes.

Tests (pkg/agent/resume_test.go)

The kernel reading is replaced by a fake, so a hibernation can be simulated.

  • TestResumedBetween: both signals and the near-misses.
  • TestResumeResetsPerBootState: hibernate on idle (so Cleanup runs), then an 8 h suspend. Every flag is re-armed, the idle timer and baselines are reset, there is one UPSERT with the new IP, and the plugin is running again.
  • TestResumedInstanceIsNotReHibernatedAndRunsPreStopAgain: the issue's scenario through checkAndAct, with counters faked to read a quiet machine. The tick after the resume does not hibernate. The next real idle hibernate runs the pre-stop hook a second time and deletes the record at the new IP.
  • TestResumeAfterOnCompleteHibernate: the stale file does not re-hibernate, and a rewritten file (new mtime) is announced and acted on.
  • TestNoResumeWithoutASuspend.
  • sys_linux_suspend_test.go: the real kernel read. It passed in an AL2023 container under podman, along with the rest of these tests built for linux/arm64.

Revert checks. Each revert compiled, and the test ran and failed:

revert failing tests
onResume never called ResetsPerBootState, NotReHibernated... (re-hibernated on the first tick), AfterOnCompleteHibernate
idle timer and baselines not reset ResetsPerBootState, NotReHibernated... (re-hibernated on the first tick)
public IP not re-read ResetsPerBootState (UPSERT with 1.2.3.4), NotReHibernated... (DELETE at 1.2.3.4)
completion-spent check disabled AfterOnCompleteHibernate
preStopDone not re-armed ResetsPerBootState, NotReHibernated... (hook ran once over two hibernates)
wall-clock signal removed ResumedBetween (both wall cases)

Checks

Caveats

…ion (#987)

A hibernated instance resumes the same spored process, which carried its
pre-hibernate state into the new run. The pre-stop hook and task record
were spent, so the next stop or TTL terminate skipped them. The DNS name
Cleanup deleted came back only at the hourly renewal, with the IP from
the first boot. lastActivityTime was hours old, so the first idle tick
hibernated again within a minute. The plugins Cleanup stopped stayed
stopped.

Detection runs on each monitor tick (checkResume) and needs no sleep
hook:
- The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from
  #965) growing by 1s or more. Only a suspend moves it.
  sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it
  does not collide with #965's kernelclock.go.
- The backup signal is the wall clock running at least 5 minutes ahead
  of Go's monotonic clock between ticks.
- resumedBetween is pure and table-tested.

onResume re-arms preStopDone and taskFlushDone, restarts the idle timer
and warning, clears the stop backoff, resets the CPU, network and disk
baselines, and marks a completion file already acted on as spent.
checkCompletion ignores it until its mtime changes. onResume then
re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed
from restoreAfterFailedStop), which re-registers DNS, the registry entry
and the plugins.

The new IP is held in a guarded currentIP field behind publicIP().
identity is shared unguarded with the provider, the plugin runtime and
off-loop goroutines, so it is not mutated. The DNS register and delete
calls and the ready-url hosts read publicIP().

(#987)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.84466% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/resume.go 75.34% 15 Missing and 3 partials ⚠️
pkg/agent/suspend_linux.go 40.00% 3 Missing and 3 partials ⚠️
pkg/agent/dns_lease.go 57.14% 3 Missing ⚠️
pkg/agent/agent.go 91.66% 1 Missing ⚠️
pkg/agent/webready.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 3711989 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/987 branch October 17, 2026 10:43
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