Skip to content

fix(control): no SQL in the control tick for curtailment identity - #1886

Merged
frahlg merged 2 commits into
masterfrom
a8-no-sql-in-tick
Oct 27, 2026
Merged

frahlg merged 2 commits into
masterfrom
a8-no-sql-in-tick

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Item A8 of the stable plan.

Problem

On every tick, forecastCurtail.PendingDrivers(reg.Names()) (main.go:3224) calls LookupDeviceByDriverName once per driver, even when nothing is curtailed. Each call is a s.db.QueryRow with no context (state/devices.go:219) on a state.db pool capped at 4 connections. With 4 drivers and the default 2 s interval, that is 120 queries a minute that cannot change the answer. If something else holds all 4 connections, the tick waits with no bound. Any read error also returns nil, so batteryModelKey (store.go:1392) quietly falls back to the driver name.

Change

  • PendingDrivers takes f.mu once. When no curtailed identity is stored (f.ids empty), it keeps the lock and skips the identity lookups. It still walks f.entries, so a driver curtailed in this run without a known identity still shows as pending. Only when it has identities to look up does it drop the lock for the lookups, as on master.
  • LookupDeviceByDriverName reads the committed deviceCache under its read lock and picks the newest LastSeenMs for that driver name, as the SQL did. RegisterDevice is the only writer of devices and updates the cache after each write, and Open loads the cache. This drops the SQL and the error fallback.

The first commit read len(f.ids), dropped f.mu, skipped the lookups and then took f.mu again. In that gap the background persist could move an active entry from f.entries into f.ids, and the driver dropped out of the pending list. That delayed a PV curtailment release retry by one tick. It also made TestForecastCurtailmentReleaseRequiresEvidence fail in 20 of 600 -race runs, against 0 of 600 on master. An earlier note called that failure an existing flaky test. That was wrong: this PR caused it. The second commit fixes it.

Tests

  • TestPendingDriversSkipsIdentityLookupsWithNothingCurtailed (from audit worktree caa-47): 30 ticks with 4 drivers. It counted 120 lookups on master and counts 0 now.
  • TestLookupDeviceByDriverNameDoesNotWaitForStatePool (from caa-141): holds all 4 pool connections. On master the lookup waited over 1 s; it now answers from the cache.
  • TestPendingDriversKeepsEntryMovedByPersist (from review): records pv as curtailed, then calls PendingDrivers over 20001 names while the worker persists. On the first commit pv went missing in 73 of 100 runs under -race and 61 of 100 without. With the fix it passes.
  • TestLookupDeviceByDriverNameCacheMatchesSQL (from review): compares the cache lookup with the old ORDER BY last_seen_ms DESC query across a hardware swap, a rename, the old device's return, a MAC-only id and a store reopen. It also checks batteryModelKey after the reopen.
  • go test -race ./cmd/ftw -run TestForecastCurtailmentReleaseRequiresEvidence -count=600: ok, 0 of 600 fail. This rerun shows the fix closes the gap.
  • go test -race ./cmd/ftw -run 'Curtail|PendingDrivers' -count=5: ok
  • go test -race ./cmd/ftw ./internal/state/... ./internal/notifications/... -count=1: ok
  • go vet ./cmd/ftw ./internal/state and go build ./...: clean

Risk

Low. Lookups return the same row as before, unless the in-memory cache and the table disagree. That can only happen if a process outside Core edits the table while Core runs. Notifications, battery-model keys and the battery-model key migration use the same lookup and now read the cache too. No effect on sign handling, stale-meter stop, default mode, planner validation, fuse or SoC floor.

🤖 Generated with Claude Code

PendingDrivers looked up every driver's device identity on each tick,
even with nothing curtailed, and each lookup was a context-free SQLite
query on the four-connection state pool. Skip the lookups when no
curtailed identity is stored, and make LookupDeviceByDriverName read
the committed in-memory device cache instead of SQL. This also removes
the silent fall back to the driver name in batteryModelKey on a read
error.

Item A8 of the stable plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T13:16:44.744024Z 520ee0c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…ored

The previous commit read len(f.ids) under f.mu, dropped the lock, skipped
the identity lookups, then took f.mu again. In that gap persist could move
an active entry into f.ids, so the driver dropped out of the pending list
and its release retry slipped a tick. TestForecastCurtailmentReleaseRequiresEvidence
failed in about 3% of -race runs because of it.

Now PendingDrivers drops f.mu only when it has identities to look up. Add a
race test for the moved entry and a test that the device cache gives the
same answer as the old SQL query across swaps, renames and a reopen.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg merged commit 10de589 into master Oct 27, 2026
23 checks passed
@frahlg
frahlg deleted the a8-no-sql-in-tick branch November 11, 2026 09:12
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.

1 participant