fix(archied): the watchdog heartbeat proves liveness and a stalled run loop reports degraded readiness (#1840)

State Store calls the daemon makes without a deadline now get one.
This commit is contained in:
sam committed 2026-11-09 14:36:08 +11:00
1 parent 44b52c54c0
commit f44db54948
6 files changed
+68 -59

No files matched your search

+7 -40
View File
@@ -1,6 +1,5 @@
// Package sdnotify sends systemd sd_notify READY=1 and WATCHDOG=1 messages.
// Heartbeats stop when the caller's progress marker goes stale. Without
// NOTIFY_SOCKET nothing is sent.
// Without NOTIFY_SOCKET nothing is sent.
package sdnotify
import (
@@ -106,58 +105,26 @@ func Heartbeat(getenv func(string) string, pid int, log *slog.Logger) (time.Dura
return watchdog / watchdogHeartbeatDivisor, true
}
// Watchdog samples the caller's progress marker and emits the watchdog
// heartbeat only while that marker is fresh. Sampling is periodic; the beat is
// not, because it is licensed by the marker rather than by the clock.
// Watchdog emits the watchdog heartbeat on every tick for as long as the
// process can run a goroutine. It proves the process is alive, not that any
// one piece of work finished: a peer that answers slowly must degrade
// readiness, not get the process killed.
type Watchdog struct {
// Interval is the sampling cadence: half of systemd's WatchdogSec.
// Interval is the beat cadence: half of systemd's WatchdogSec.
Interval time.Duration
// Cadence reports how long the loop may go between passes.
Cadence func() time.Duration
// Progress reports the stalest loop and the start of its most recent
// pass, or the zero time when no pass has begun yet.
Progress func() (string, time.Time)
// Beat emits one WATCHDOG=1.
Beat func()
Log *slog.Logger
}
// Run samples until ctx ends. It returns ctx.Err() on shutdown, which is the
// only way out: this sampler never gives up on its own, because a sampler that
// stopped would leave systemd watching a process nothing reports for.
// Run beats until ctx ends and returns ctx.Err().
func (w Watchdog) Run(ctx context.Context) error {
armedAt := time.Now()
stalled := false
ticker := time.NewTicker(w.Interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
loop, last := w.Progress()
if last.IsZero() {
// No pass has begun at all. Measure staleness from arming, so a
// loop that never starts polling is caught like one that stopped.
last = armedAt
}
cadence := w.Cadence()
switch age := time.Since(last); {
case age > cadence:
if !stalled {
w.Log.WarnContext(ctx, "systemd watchdog: run loop has not begun a poll pass within its interval; withholding the heartbeat until it does",
"loop", loop, "stale_for", age.String(), "loop_cadence", cadence.String())
stalled = true
}
default:
if stalled {
w.Log.InfoContext(ctx, "systemd watchdog: run loop resumed; heartbeats resumed",
"last_pass_age", age.String())
stalled = false
}
w.Beat()
}
}