Once graceful shutdown begins (SIGINT/SIGTERM cancels the daemon's signal context), start an out-of-loop watchdog timer for the drain window plus a 60s grace period. If the shutdown chain has not completed by the time that leash expires, force-exit the process (os.Exit) as a backstop against a hung shutdown. The watchdog runs in its own goroutine outside the daemon's event loop and graceful-shutdown machinery, so it fires even when that machinery is itself the thing that is stuck. It is disarmed via the FIRST registered cleanup so it runs LAST in the LIFO shutdown chain: it stands down only after every subsystem has shut down cleanly, and a hung subsystem never reaches the disarm, letting the watchdog force-exit. A shutdown that completes within the leash does not trigger the watchdog; a shutdown that hangs past the leash results in a forced process exit. The leash uses a test-only short timeout in tests. Reference bead archie-core-1786637496842-267-8b2984f9.
96 lines
3.5 KiB
Go
96 lines
3.5 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// shutdownGracePeriod is the fixed grace added to the drain window before the
|
|
// shutdown watchdog force-exits the process.
|
|
const shutdownGracePeriod = 60 * time.Second
|
|
|
|
// defaultShutdownDrainTimeout is the graceful-shutdown drain window used to
|
|
// compute the watchdog leash when no drain timeout is configured. It bounds
|
|
// how long the daemon waits for in-flight work to finish before the
|
|
// watchdog's backstop applies. The drain-control marker/timeout is a sibling
|
|
// effort; when it lands it supplies this value and the leash is recomputed
|
|
// from it.
|
|
const defaultShutdownDrainTimeout = 60 * time.Second
|
|
|
|
// shutdownLeash is the total leash the shutdown watchdog allows: the drain
|
|
// window plus the grace period. Shutdown normally completes well within it;
|
|
// the leash only bites when the graceful-shutdown machinery hangs.
|
|
func shutdownLeash() time.Duration {
|
|
return defaultShutdownDrainTimeout + shutdownGracePeriod
|
|
}
|
|
|
|
// shutdownWatchdog force-exits the daemon if graceful shutdown does not
|
|
// complete within the leash. It runs in its own goroutine outside the
|
|
// daemon's event loop, so it fires even when the graceful-shutdown machinery
|
|
// it backstops is itself the thing that is hung. It depends only on the
|
|
// process being alive, never on a subsystem's clean shutdown.
|
|
type shutdownWatchdog struct {
|
|
leash time.Duration
|
|
log *slog.Logger
|
|
exit func(int)
|
|
}
|
|
|
|
// newShutdownWatchdog builds a watchdog that, once shutdown begins, stops the
|
|
// process if it does not complete within leash. exit is injectable so tests
|
|
// can observe a forced exit without terminating the test process.
|
|
func newShutdownWatchdog(leash time.Duration, log *slog.Logger) *shutdownWatchdog {
|
|
return &shutdownWatchdog{leash: leash, log: log, exit: os.Exit}
|
|
}
|
|
|
|
// watch starts the watchdog goroutine. The returned func disarms it: call it
|
|
// once graceful shutdown has completed so a shutdown that finishes within the
|
|
// leash is never cut short. If that func is never called -- a hung shutdown --
|
|
// the watchdog force-exits the process as soon as the leash expires.
|
|
//
|
|
// The goroutine blocks on a single select pair and never touches the daemon's
|
|
// own event loop or shutdown machinery, which is precisely the point: it must
|
|
// still be able to run when those are the parts that are stuck.
|
|
func (w *shutdownWatchdog) watch(shutdown <-chan struct{}) func() {
|
|
var once sync.Once
|
|
done := make(chan struct{})
|
|
disarm := func() { once.Do(func() { close(done) }) }
|
|
|
|
go func() {
|
|
// Wait for graceful shutdown to begin. If the process is exiting for
|
|
// some other reason, done is closed and the watchdog stands down
|
|
// before doing anything.
|
|
select {
|
|
case <-shutdown:
|
|
case <-done:
|
|
return
|
|
}
|
|
|
|
timer := time.NewTimer(w.leash)
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
case <-timer.C:
|
|
w.log.Error("shutdown watchdog: graceful shutdown did not complete within leash; forcing exit",
|
|
"leash", w.leash.String())
|
|
w.exit(1)
|
|
case <-done:
|
|
// Shutdown completed within the leash; stand down.
|
|
}
|
|
}()
|
|
|
|
return disarm
|
|
}
|
|
|
|
// startShutdownWatchdog arms the daemon's shutdown watchdog. It must be called
|
|
// before any other cleanup is registered so its disarm runs last in the LIFO
|
|
// cleanup chain: the watchdog stands down only after every other subsystem has
|
|
// shut down cleanly. A subsystem that hangs never runs the disarm, so the
|
|
// watchdog force-exits.
|
|
func (b *boot) startShutdownWatchdog(ctx context.Context) {
|
|
disarm := newShutdownWatchdog(shutdownLeash(), b.log).watch(ctx.Done())
|
|
b.addCleanup(disarm)
|
|
}
|