Skip to content

fix(drivers): harden driver stop, failed start and reload paths - #1444

Merged
frahlg merged 3 commits into
masterfrom
fix/driver-lifecycle-hardening
Sep 26, 2026
Merged

frahlg merged 3 commits into
masterfrom
fix/driver-lifecycle-hardening

Conversation

@frahlg

@frahlg frahlg commented Sep 26, 2026

Copy link
Copy Markdown
Member

From the multi-agent hardening review of master. Each finding was confirmed by a skeptic reading the code, and the diff was reviewed again after the fix.

Fixed

  • An observe_only driver was handed back to its default mode on every Remove, Restart, Reload and shutdown.
    • Example: a Sungrow battery that a retailer's VPP controls got set_self_consumption written on each FTW restart or update.
    • removeLocked now treats observe_only as skip-default. The VM is discarded without driver_cleanup, because many cleanups write hardware.
    • SendDefault returns ErrObserveOnly, as main's watchdog already did, and DELETE /api/drivers/{name}/control answers 403.
  • A failed Add leaked every capability it had opened (MQTT, Modbus, serial, WS, TCP).
    • A failed driver_init or a failed "Test connection" left a paho client reconnecting forever under the same client id. The next successful Add then fought it for the broker session, which is the flap behind the ferroamp incident.
    • The close logic moved to HostEnv.closeCapabilities. add() defers it on every error return.
  • driver_cleanup never ran on shutdown or restart. It got an already-canceled context, so it had been silently skipped since 1f6686b. It now runs after the shutdown default with its own 5 s bound. Probes and observe_only stops discard the VM instead.
  • A replacement Add was cancelled by the HTTP caller. A browser that disconnected mid-restart left the driver stopped. Restart, RestartByName and Reload now start the replacement with context.WithoutCancel.
  • Hot reload missed some config changes. Changes to the HTTP grant (hosts, TLS pin, allow_write), the WebSocket grant and supports_pv_curtail were ignored; they now restart the driver.

Not changed (owner decisions)

  • Honouring the interval that driver_poll returns. This would change the poll cadence of about 15 bundled drivers: some Modbus drivers would poll 5× more often, and myuplink could flap at the 60 s watchdog.
  • A time bound on a legacy driver_init. Slow or offline devices would fail at boot instead of starting degraded.

Behaviour to know

  • Drivers whose cleanup writes hardware (sungrow, ferroamp, deye, …) repeat their hand-back after the default on stop. Worst case, this adds 5 s per unreachable driver to ShutdownAll. The systemd default stop timeout is 90 s.
  • The config-watcher Reload ignores cancellation of the process context. An in-flight driver_init finishes before ShutdownAll.

Evidence

  • 8 new tests in registry_lifecycle_test.go and registry_config_values_test.go. All of them fail with the old registry.go/host.go.
  • go test ./internal/drivers passes, also with -race on the lifecycle tests.
  • Also passing: ./internal/api, driverrepo, driverinventory, integration, modbus, mqtt, ./cmd/ftw and FTW_E2E=1 ./test/e2e.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7

frahlg and others added 2 commits September 26, 2026 09:47
- observe_only drivers are never handed back on the way out: Remove,
  Restart, Reload and ShutdownAll skip driver_default_mode and
  driver_cleanup for them, and SendDefault returns ErrObserveOnly, as
  main's watchdog already did. A VPP-owned battery keeps its settings.
- driver_cleanup runs again on a controlled driver's shutdown. It was
  called with the already-canceled lifecycle context and aborted at its
  first instruction, so Sungrow's curtailment release was lost. It now
  gets its own 5 s bound; probes and observe_only drivers discard the VM
  without it, including when their driver_init fails.
- A failed Add closes the MQTT, Modbus, serial, WebSocket and TCP
  capabilities it opened. A leaked MQTT client reconnected forever under
  the same client ID and fought the next Add for the session.
- Restart, RestartByName and Reload start the replacement with a context
  detached from the caller's cancellation, so an HTTP client that
  disconnects after the old generation stopped no longer leaves the driver
  (possibly the site meter) stopped until the next reload.
- sameDriverConfig compares the HTTP grant (allowed hosts, TLS pin,
  allow_write), the WebSocket grant and supports_pv_curtail, so reload
  applies a revoked allow_write or a rotated pin. nil and empty
  allowlists stay equal to avoid a second restart after a settings save.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7
The control release now maps ErrObserveOnly the same way the control
POST does, so a normal refusal is not reported as a server error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 26, 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-09-26T08:06:18.048435Z 62b24da 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 30017ac into master Sep 26, 2026
14 checks passed
@frahlg
frahlg deleted the fix/driver-lifecycle-hardening branch September 26, 2026 08:24
frahlg added a commit that referenced this pull request Sep 27, 2026
…st proxies (#1454)

Owner decision 2026-09-26: the signed control v2 runtime has been
unreachable since the Device Support package format was removed (#1432);
driverrepo only ever binds a read-only policy now.

- #16 Remove control v2: LuaDriver.CommandV2/DefaultModeV2, the v2 load and
  lifecycle timeouts, write scopes, write budget and host evidence, the
  command/result envelope types and validation, the registry's v2 dispatch,
  lease timer, stale/lease-expiry defaults and v2 startup default, and
  Registry.CommandResultSink with its main.go wiring. The legacy
  driver_command/driver_default_mode path, the recovery gate and the
  observe_only handling from #1444 are unchanged.
- RuntimePolicy (control_v2.go -> runtime_policy.go) keeps only the
  read-only policy; validate rejects any other, and allowWrite refuses
  every write from a driver that carries a policy.
- Drop the 100 ms host.sleep cap that, after v2, applied only to signed
  read-only drivers, so a file sleeps the same whichever source it came
  from (new test).
- driver_command_results is no longer created and its write-only store
  method is gone; existing tables are left inert with a note in migrate().
- #38 Delete the unused HostEnv mqttSubscribe, mqttPublish,
  mqttPollMessages, modbusWriteSingle and modbusWriteMulti proxies.
- Move the surviving read-only lifecycle test, adapt the HTTP 302 and
  OAuth sign-in tests, and update the roadmap, driver guide and comments
  that described v2.


Claude-Session: https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7

Co-authored-by: Claude Opus 5.5 <[email protected]>
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