Repository navigation
fix(control): supervise the control tick and commit a trial after the first tick - #1880
Conversation
… first tick A supervisor goroutine watches the time of the last completed tick. When it is older than max(3 x interval, 10 s), every driver gets its default mode in parallel; observe-only drivers are skipped. When it is older than max(60 s, twice that), Core exits 1 so the service restarts it and probation counts the exit. Every path out of a tick records it, including the stale-meter and setup-measurement paths. The first completed tick commits a native trial, instead of the moment HTTP is up. ComputeDispatch and SelfTuneDispatch run under a deferred ctrlMu unlock. Item A7 of the stable plan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d30d2e8ea9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // per stall; exit stays true while the stall lasts past tickExitAfter. | ||
| func (s *tickSupervisor) check(now time.Time) (sendDefaults, exit bool, age time.Duration) { | ||
| interval := time.Duration(s.intervalNs.Load()) | ||
| age = now.Sub(time.Unix(0, s.lastNs.Load())) |
There was a problem hiding this comment.
Measure tick age with a monotonic clock
When the host clock is corrected while a tick is running, converting the heartbeat to UnixNano and reconstructing it with time.Unix makes this subtraction use wall-clock time rather than Go's monotonic clock. A forward correction of at least 60 seconds can therefore terminate a healthy Core on the next supervisor check, while a backward correction can postpone defaults and restart for a genuinely blocked tick until the clock catches up. Preserve the monotonic component of the heartbeat, for example by storing the original time.Time under a mutex, so clock synchronization cannot change these safety deadlines.
Useful? React with 👍 / 👎.
The supervisor stored the last tick as wall-clock nanoseconds, so its age followed the wall clock. A Pi without an RTC boots on a stale clock and NTP steps it forward, often while the loop already runs. A forward step of 60 s or more made a healthy Core send defaults and exit; a backward step hid a real stall. The supervisor now keeps a base time from newTickSupervisor and stores each tick as the time since that base, so ages use the monotonic reading. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Item A7 of the stable plan.
Problem
The control tick runs every safety check itself: the driver watchdog, the stale-meter stop and the fuse guard. Nothing watches the tick from outside.
forecastCurtail.PendingDriverswaiting for a free SQLite connection (the pool holds 4), the battery keeps its last setpoint with no time limit. A Sungrow leaves forced mode only through its default (sungrow.lua:839). The HTTP server and drivers keep running, so systemd sees a healthy process.ctrlMuis taken withoutdeferaroundComputeDispatch(main.go:3149-3161). A panic there leaves the lock held. Shutdown then runsmpcSvc.Stopfirst, which waits for a replan that needsctrlMu, so the process never reachesreg.ShutdownAlland never exits.Change
sendDriverDefaultthe watchdog uses (observe-only drivers are skipped). When it is older than max(60 s, twice that), it callsos.Exit(1). No clean-exit mark is written, so probation counts the exit. A tick that recovers re-arms the supervisor.time.Nowand stores each tick as the time since that base. A Pi without an RTC boots on a stale clock, and the unit orders only afternetwork-online.target, so NTP often steps the clock while the loop runs. A wall-clock age would make a healthy Core exit after a forward step of 60 s or more, and a backward step would hide a stall.continueand the setup-measurementcontinue. A blocked-by-stale-meter tick therefore counts as healthy.ComputeDispatchandSelfTuneDispatchrun inwithCtrlLock, which unlocks withdefer.docs/self-update.mdnow says a trial becomes current after its first control tick.The audit's source-order repro also asked for systemd
WatchdogSecwithsd_notify. The plan picked the in-process supervisor, so that half is replaced by real tests of a blocked tick. The audit noted that 10 s may be tight for a slow but healthy tick; I kept the plan's value. A false trip sends defaults once, and the next tick dispatches again.Tests
go test -race -count=1 ./cmd/ftw/: ok (261 s).go/cmd/ftw/tick_supervisor_test.go:TestTickSupervisorDefaultsThenExitsOnBlockedTick: one tick completes, the next blocks. No defaults at 9 s, one default at 11 s, none more at 30 s, exit at 61 s.TestTickSupervisorRearmsAfterTheTickRecovers,TestTickSupervisorThresholdsFollowTheInterval(2 s → 10/60 s, 5 s → 15/60 s, 30 s → 90/180 s).TestTickSupervisorIgnoresWallClockStep(2 s of real time, 10 min forward step: no defaults, no exit) andTestTickSupervisorSeesStallAfterBackwardStep(70 s stall, 1 h backward step: defaults and exit). Both move only the wall clock of atime.Nowvalue and keep its monotonic reading. Both fail without the monotonic base.TestSendDefaultsInParallelDoesNotWaitOnOneSlowDriver,TestWithCtrlLockReleasesTheLockOnPanic.TestNativeTrialCommitsOnlyAfterFirstControlTick: the audit repro (caa-81), adapted. It fails on master. It checks that the commit lives only intickDoneand that everycontinueout of the tick, and its end, callstickDone().go vet ./...andgo build ./...: clean.Risk
Medium. The supervisor only sends defaults and exits; it takes no control lock, so a wedged tick cannot block it. A tick slower than 10 s now sends defaults once, which is the safe state. The trial commit moves from "HTTP up" to about one control interval later.
🤖 Generated with Claude Code