Skip to content

Give plugins back their status and health loop when a stop or hibernate fails (#979) - #1009

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/979
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/979

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

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

What was wrong

stop() and hibernate() call Cleanup before the EC2 call. Cleanup calls pluginRuntime.StopAll, which ends each running plugin's health loop and saves StatusStopped. If the EC2 call then fails, the instance keeps running, but:

  • spawn plugin status reports every plugin as stopped;
  • no health loop is running, so a plugin that fails later is never noticed.

#977 already restored DNS and the registry entry. It could not restore plugins, because the runtime kept no record of what StopAll had stopped.

What changed

  • pkg/pluginruntime
    • startHealthLoop records the health block each loop was started with (healthSpecs, under mu), and stopHealthLoop returns it.
    • StopAll records what it stopped: each plugin's prior status and its health block, if it had one.
    • New ResumeStopped(ctx) undoes the last StopAll, once. It saves the prior status back (running or degraded) and restarts the health loop.
    • A plugin that is no longer stopped when ResumeStopped runs (removed, or reinstalled in between) is left alone.
    • StopAll runs no stop steps, so a plugin's processes were never stopped and nothing else needs restarting.
    • New NewRuntimeWithStore, so tests can use a temp state store. pkg/pluginruntime is not part of the public surface.
  • pkg/agent: restoreAfterFailedStop calls ResumeStopped, next to the DNS and registry restore. That covers the idle path and the on-complete path, for both stop and hibernate.

I took the issue's second option rather than splitting Cleanup. With hibernate, the guest freezes as soon as EC2 accepts the request, so there is no safe point to stop plugins after the call. The runtime record also gives #987 (stacked next) a way to restart plugins after a resume.

Tests

  • pkg/pluginruntime/resume_test.go
    • TestResumeStoppedRestoresStatusAndHealthLoop sets up a running plugin with a real 20 ms health step that appends to a file, plus degraded, already-stopped, failed and removed plugins.
      • After StopAll, the plugins are stopped and the file stops growing.
      • After ResumeStopped, running and degraded are back, the others are unchanged, the removed plugin is not recreated, and the file grows again.
    • TestResumeStoppedIsOnceAndYieldsToNewerState: a reinstall in between is left alone, and a second resume is a no-op.
  • pkg/agent/idle_stop_retry_test.go TestFailedStopRestoresPlugins: after a failed stop or hibernate the plugin is running. After a successful stop it is stopped.

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

revert failing tests
restoreAfterFailedStop doesn't call ResumeStopped FailedStopRestoresPlugins/{stop,hibernate}_fails ("stopped", want "running")
ResumeStopped doesn't restart the health loop ResumeStoppedRestoresStatusAndHealthLoop (health loop did not restart)
ResumeStopped doesn't restore the status both resume tests and FailedStopRestoresPlugins

The health-loop test passed 30 runs in a row under -race.

Checks

…te fails (#979)

stop() and hibernate() run Cleanup before the EC2 call, and Cleanup's
pluginRuntime.StopAll ends every running plugin's health loop and
persists StatusStopped. When the EC2 call then failed, the instance kept
running with every plugin recorded as stopped and no health loop, so a
plugin that failed later was never noticed.

- pluginruntime.Runtime now remembers the health block each loop was
  started with (healthSpecs) and what the last StopAll stopped: each
  plugin's prior status and, if it had one, its health block.
- New Runtime.ResumeStopped undoes the last StopAll once: the prior
  status (running or degraded) is saved back and the health loop is
  restarted. A plugin no longer recorded as stopped (removed or
  reinstalled since) is left alone. StopAll runs no stop steps, so
  nothing else needs restarting.
- restoreAfterFailedStop calls it, beside the DNS and registry restore
  from #948.
- New NewRuntimeWithStore, so the agent's tests can use a temp store.

(#979)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.94737% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/pluginruntime/runtime.go 77.77% 6 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

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

A failed stop/hibernate leaves every plugin marked stopped on a running instance (Cleanup's StopAll is not undone)

1 participant