Skip to content

fix(drivers): bound driver_init and apply fuse settings before reload - #1893

Merged
frahlg merged 2 commits into
masterfrom
b2-driver-init-deadline-fuse-first
Oct 28, 2026
Merged

frahlg merged 2 commits into
masterfrom
b2-driver-init-deadline-fuse-first

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026

Copy link
Copy Markdown
Member

Item B2 of the stable plan.

Problem

  • driver_init runs with no deadline (lua.go, setLifecycleContext). Reload, Restart and RestartByName drop the caller's cancellation (replacementContext), and boot uses the process context. A driver whose init is while true do end blocks boot or Reload forever. Reload runs inside the settings save, which holds configWriteMu, so every later settings write blocks too.
  • applyConfigChange calls reg.Reload first and writes the fuse, MaxExportW, DriverLimits and the loadpoint SetSiteFuse only afterwards. One changed driver costs up to 5 s shutdown default + 5 s cleanup + init + 5 s startup default. During that time /api/config already says 16 A while dispatch and the EV clamp still use 25 A. A hung init means the new fuse never reaches control.

Change

  • driver_init runs under setLuaCallContext with a 30 s bound. A timeout is an init failure: the driver is not added, as with any other init error. A shorter caller deadline still wins.
  • configreload.Apply writes the fuse (amps, voltage, phases, safety margin) and MaxExportW under ctrlMu, before the applier runs. The lines move out of main.go.
  • The applier pushes DriverLimits and lpController.SetSiteFuse before reg.Reload. A driver that leaves the config keeps its old limits until Reload has stopped it, so it never runs without limits. After Reload the map is set to exactly the new limits.

Tests

  • New go/internal/drivers/driver_init_deadline_test.go (from the audit repro, caa-107): TestLoopingDriverInitDoesNotBlockReload and TestLoopingDriverInitFailsAdd fail on master (blocked after 5 s) and pass here in 0.2 s. TestLoopingDriverInitHonoursCallerDeadline passes on both.
  • New go/internal/configreload/apply_fuse_order_test.go (caa-127): TestApplyFuseReachesControlBeforeRuntimeApplier fails on master (dispatch fuse at applier start = 25 A while committed config says 16 A) and passes here.
  • go test -race -count=1 ./internal/drivers/... ./internal/configreload/... ./internal/api/... ./cmd/ftw/...: all ok.
  • go vet ./... and go build ./...: clean.

The order inside applyConfigChange has no unit test, because it is a closure in main.

Risk

Low. A real driver whose init takes more than 30 s now fails to start instead of starting late. Bundled inits do a few Modbus reads (5 s timeout, fast backoff after a failed dial) or one HTTP call (15 s timeout), so they stay well under the bound. The myUplink token path calls the applier without Apply, but it never changes the fuse.

🤖 Generated with Claude Code

driver_init ran with no deadline, and Reload strips the caller's
cancellation, so a looping init blocked boot, Reload and every settings
write. It now runs under a 30 s bound; a timeout is an init failure.

Applying settings wrote the fuse, export limit, driver limits and the
EV fuse clamp only after reg.Reload, which can take seconds per changed
driver. configreload.Apply now writes the fuse and export limit under
ctrlMu, and the applier pushes driver limits and SetSiteFuse before
Reload. A driver leaving the config keeps its old limits until Reload
stops it.

Item B2 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:29:47.568850Z 7fa4df7 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.

@frahlg
frahlg merged commit 573d24f into master Oct 28, 2026
24 checks passed
@frahlg
frahlg deleted the b2-driver-init-deadline-fuse-first 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