Skip to content

fix(drivers): retry a driver that fails to start - #1890

Merged
frahlg merged 2 commits into
masterfrom
b3-retry-failed-driver-spawn
Oct 27, 2026
Merged

frahlg merged 2 commits into
masterfrom
b3-retry-failed-driver-spawn

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Item B3 of the stable plan.

Problem

The boot loop calls reg.Add once per driver and only logs a failure (cmd/ftw/main.go:796-805). A serial open fails inside add before the driver is recorded (registry.go:631-636), and so does a driver_init error. Nothing retries it. The site meter driver stays absent, liveSiteMeter returns false, and dispatch stays blocked until someone saves settings, calls the restart API or restarts Core.

Scenario: after a power cut, the box boots before the USB P1 reader shows up as /dev/ttyUSB0. Add fails with serial capability: open .../ttyUSB0: no such file or directory. The node appears a moment later, but on master the meter is still missing 15 s later, and stays missing.

Change

The registry keeps a list of drivers whose Add failed and retries each one after 10 s, doubling the wait up to 5 min. This is the narrow follow-up that #1771 asked for: enabled and not running, retry add with backoff.

  • A retry runs under the driver's lifecycle gate with startupDefault on, so the recovered driver confirms its default before control resumes.
  • Any Add, Restart or Reload of the same name replaces the pending retry, and a user action restarts the backoff at 10 s. A stale timer finds a different entry and does nothing.
  • Remove, a Reload that no longer lists the driver, and ShutdownAll end the retry.
  • A retry marks its name in flight while add runs. A Reload that drops or disables the driver waits on its gate and removes whatever the retry left, and ShutdownAll waits for the attempt before it returns.
  • Running drivers are never torn down, and health records are not wiped. Probes (AddProbe) are not retried.

main.go is unchanged. The diff to registry.go is 27 lines; the rest is a new file.

Tests

  • TestBootSpawnFailureIsRetriedWhenSerialDeviceAppears (repro from the audit, caa-75): fails on master after 15 s and passes here in 10 s.
  • TestSpawnRetryBacksOffAndStopsWhenDriverLeaves covers backoff to the cap, a reset on user Add, a stale timer, and the end of retries on Reload, Remove and ShutdownAll. TestProbeFailureIsNotRetried covers probes.
  • TestReloadDuringInflightRetrySuccessStopsDriver, TestReloadDuringInflightRetryFailureStopsRetrying and TestShutdownAllWaitsForInflightRetry block a retry in its serial open, then call Reload or ShutdownAll. All three fail without the in-flight mark (review finding) and pass with it.
  • go test -race -count=1 ./internal/drivers/... ./internal/api/... ./cmd/ftw/...: pass.
  • go vet ./... and go build ./...: clean.

Risk

Low. The change only acts when Add has already failed.

One known cost: a cloud driver whose driver_init fails on bad credentials now logs in every 5 min, which #1771 named as a lockout risk. The plan accepts that backoff. If a vendor locks accounts at that rate, a cap for login failures can follow.

No hardware run. A serial P1 site should confirm recovery after a power cut on the next beta.

🤖 Generated with Claude Code

A driver whose Add fails (a serial node not enumerated yet, a device still
booting after a power cut) was logged once and forgotten, so a missing P1
meter blocked dispatch until a restart or a settings save. The registry now
retries it after 10 s, doubling to 5 min, until it starts, leaves the
config, is removed, or the registry shuts down. Running drivers are never
torn down, and probes are not retried.

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:25:48.753832Z 264abcf 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 264abcf04c

ℹ️ 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".

Comment on lines +1483 to +1486
// Stop retrying a driver that failed to start and has left the config.
for _, n := range r.pendingSpawnRetries() {
if _, ok := findDriver(active, n); !ok {
release := r.acquireLifecycle(n)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep in-flight retries visible to config reloads

If a user removes or disables a driver while its retry is constructing capabilities or running driver_init, add has already deleted its retry entry through takeSpawnRetry, but has not yet inserted it into r.rec. Consequently, Reload sees the name in neither pendingSpawnRetries() nor oldCfgs, skips its lifecycle gate, and returns without canceling it. The retry can then register and poll the removed driver, or schedule further retries if initialization fails, despite the saved configuration disabling it. Keep the in-flight attempt discoverable until registration completes, so reload can serialize with it and enforce removal.

AGENTS.md reference: AGENTS.md:L20-L21

Useful? React with 👍 / 👎.

…nAll

add() deletes the retry entry before the driver reaches r.rec, so while a
retry ran its name was in neither list Reload checks. A driver the user
removed or disabled could then start, or schedule yet another retry.
ShutdownAll could also return while the retry was still starting a driver.

The retry now marks its name in flight under r.mu for the length of add.
Reload sees the mark, takes the name's gate and removes what the retry left.
ShutdownAll waits on the gate of each in-flight name before it lists drivers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg merged commit 47b54d4 into master Oct 27, 2026
22 checks passed
@frahlg
frahlg deleted the b3-retry-failed-driver-spawn 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