Files
lyphyserandHanzo Dev 929441a870 watchdog: park the watchdog when the sandbox is idle
Watchdog.loop() armed a fresh time.After(period) timer for the lifetime of
the sentry (period = TaskTimeout/4, so 45s by default), so a fully idle
sandbox woke the host roughly every 45s only to scan a task set in which
nothing could be stuck.

The watchdog only reports tasks stuck in TaskGoroutineRunningSys. Such a task
counts as a running task (Kernel.runningTasks), so while runningTasks is 0
there is none to find; moreover the kernel CPU clock that the watchdog uses to
measure how long a task has been in RunningSys is frozen while no task is
running. So an idle sandbox has nothing for the watchdog to detect, and the
periodic scan is pure overhead that needlessly wakes the host.

Make the watchdog park while no tasks are running, mirroring the CPU clock
ticker's and Timekeeper's idle handling. The loop now blocks in
Kernel.WaitForTaskActivity() until a task becomes runnable instead of arming
its periodic timer, and reuses a single timer that it leaves stopped while
parked, so an idle sentry arms no host timer.

WaitForTaskActivity() returns immediately if a task is already running;
otherwise it blocks on activeNotifyCh, a channel that incRunningTasks() closes
on the 0->1 running-task transition and replaces for the next idle period.
Closing broadcasts to every parked waiter at once, and the channel is read
under runningTasksMu so the wakeup cannot be lost. A channel (rather than
runningTasksCond) lets the watchdog also select on its own stop channel, which
a sync.Cond cannot. The close fires on every 0->1 transition, so a waiter that
parked during the CPU clock ticker's one-tick slack window, or before
Kernel.Start(), is still woken.

Tasks executing application code (TaskGoroutineRunningApp) also count as
running, so a sandbox busy purely in userspace keeps the watchdog awake even
though no task is in RunningSys. That is harmless -- the host is busy running
guest code then anyway -- and tightening it is left for a later change.

A fully idle sentry now performs no periodic watchdog wakeups; stuck-task
detection while tasks are running is unchanged.

Co-authored-by: Hanzo Dev <[email protected]>
2026-06-19 14:41:23 +00:00
..