Skip to content

fix(coderd/x/plugins): back off restarts of a failing plugin - #361

Merged
ibetitsmike merged 7 commits into
mainfrom
mike/issue-305-restart-backoff
Nov 16, 2026
Merged

ibetitsmike merged 7 commits into
mainfrom
mike/issue-305-restart-backoff

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #305. This is item 4, restart backoff. It was stacked on #360 (lease renewal), which merged as 29781c8. The base is now main.

Problem

When a plugin's host process stopped with an error, the Manager started it again on the next lease interval. That repeated every 10 seconds forever, and each start read the module from the database (up to 48 MiB) and compiled it again.

Change

  • The runner records failures, the count of earlier runners in a row that stopped with an error. When the runner stops with an error, it sets restartAt = now + min(10s x 2^failures, 10min) before done closes.
  • Manager.reconcile keeps the exited runner and its lease until restartAt. Then it starts a new runner with failures + 1.
  • A runner that stayed up for 5 minutes after it became ready (module loaded) resets the count to 0. A slow load that fails does not reset it.
  • A plugin whose updated_at changed starts at once, with the count reset.
  • last_error now reads restarting in <delay> after: <error>. The delay comes first, so the 2048 byte cut cannot drop it.
  • The restart is checked on the lease loop. The delays are multiples of the 10 second lease interval, so a restart can be late by at most one interval.

Tests

All run under flock ~/.fork-go.lock.

  • TestManagerRestartBackoff (quartz) uses a host that fails at start.
    • Attempts at 0, 10, 30, 70 and 150 s.
    • One GetFileByID per attempt.
    • Delays in last_error: 10s, 20s, 40s, 1m20s.
    • The fifth start takes 5 minutes and fails. The runner never became ready, so the delay doubles to 2m40s and there is no attempt at 460 s.
    • A changed plugin starts at once, and its next delay is 10s.
  • TestRestartDelay checks the doubling and the 10 minute cap.
  • TestRunnerFailedResetsAfterUptime checks that only 5 minutes of uptime after ready resets the count.
  • go test -race -count=10 ./coderd/x/plugins/ -run 'TestManager|TestRestartDelay|TestRunnerFailed': 100 PASS.
  • go test -race ./coderd/x/plugins/...: ok.
  • go test ./coderd ./enterprise/coderd -run TestPlugin: ok.
  • golangci-lint run --fix=false ./coderd/x/plugins/...: 0 issues. gofmt clean.

Negative controls. Each one makes TestManagerRestartBackoff fail:

  • No backoff wait, which is the old behavior: the fifth attempt comes at 40 s and the test times out.
  • No reset after 5 minutes: TestRunnerFailedResetsAfterUptime fails (1m20s, not 10s).
  • The window counted from the start, not from ready: TestManagerRestartBackoff gets an extra attempt at 460 s.
  • A changed plugin keeps its failure count: the last delay is 5m20s, not 10s.

Not verified

  • A real plugin-host crash on a running server. The reset after 5 minutes of real uptime is tested on runner.failed directly, not with a running host.
  • A multi-replica deployment. During the backoff the replica keeps the lease, so no other replica takes the plugin over.

RenewPluginLeases renews this replica's leases and returns only id and
updated_at, so a renewal reads and decrypts no plugin config. The
manager reads a full row with GetPluginByID only when updated_at
changed or no runner exists. AcquirePluginLeases now takes only plugins
no replica holds, up to the free slots.

Bindings of archived chats are kept on purpose: deleting them loses the
cursor and the delivered mark, so an unarchived chat would replay its
history or move to another link. A test pins that archived chats are
not due and keep both.

Part of #305.
A plugin whose host process stopped with an error was started again on
the next lease interval, every 10 seconds forever, and each start read
the module from the database and compiled it again.

The runner now keeps its lease and waits 10 seconds after the first
failure, doubled for each failure in a row, up to 10 minutes. A runner
that stayed up for 5 minutes resets the count, and a changed plugin
starts at once. last_error names the delay.

Closes #305.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T05:04:40.181113Z b6adee0 Manual request
ℹ️ 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: 0b94972b5c

ℹ️ 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 thread coderd/x/plugins/runner.go Resolved
A start that took 5 minutes and then failed reset the failure count, so
a slow failing load retried every 10 seconds. The window now starts when
the module loaded and the runner became ready.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: e15ee57109

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

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged the #360 head (5a2b9d7, which now includes main at 2523de1) into this branch.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 9d86f1fabc

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

…backoff

# Conflicts:
#	coderd/x/plugins/manager.go
#	coderd/x/plugins/manager_internal_test.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

#360 merged, so this PR now targets main. I merged main (29781c8) into this branch. The resulting tree is the same as the reviewed head 9d86f1f: main's tree equals the old #360 head, which this branch already contained. Focused tests pass again under the lock.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: d468da9d65

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

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged main (d375e04, after #346 and #358) into this branch. No conflicts, and no file under coderd/x/plugins changed. fork-ci run 43805506969 is green on b6adee0.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b6adee0ce5

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

@ibetitsmike
ibetitsmike merged commit 54e2b41 into main Nov 16, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-305-restart-backoff branch November 16, 2026 05:31
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head b6adee0ce5198d0275b01531d1915685532687c8 as 54e2b41, on main d375e0433214f97d1e6a51dd8205e7f8ed2b377e, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).
  2. Every check run passed. fork-ci run 43805506969 passed on this head.
  3. Codex receipt for this head: fix(coderd/x/plugins): back off restarts of a failing plugin #361 (comment) ("Didn't find any major issues").
  4. No unresolved review thread. The one thread (P2 on the reset window) was fixed in e15ee57 and resolved.
  5. 0 commits behind main.
  6. The PR's own diff against main (3 files under coderd/x/plugins) is identical to its diff at the tested head d468da9d65, whose tree is byte-identical to the reviewed head 9d86f1fabc. All 38 files that differ between d468da9d65 and this head equal main's own delta from 29781c8f17 to d375e04332 (feat(site): stop and resume workflow runs from the card and panel #346, perf(chatd): cache wake-up organization spend and purge old run counts #358), so the head only adds a main merge.

Tests I ran on d468da9d65 (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432). Its tree is byte-identical to the reviewed head 9d86f1fabc:

  • go test -count=1 -race -v ./coderd/x/plugins/...: 47 PASS, 0 FAIL (plugins, pluginhost, sandbox ok).
  • go test -count=1 -v -run TestPlugin ./coderd/ ./enterprise/coderd/: 15 PASS, 0 FAIL.
  • Negative control: with the backoff wait in reconcile disabled (false &&), TestManagerRestartBackoff fails; TestRestartDelay and TestRunnerFailedResetsAfterUptime still pass. The source was restored after.
  • The workstream also reported -race -count=10 on the manager and restart tests (100 PASS) and three more negative controls (never reset, reset window from start, changed plugin keeps its count).

Not exercised: a real plugin-host crash on a running server, multi-replica behavior during the backoff (the replica keeps its lease), and the 5 minute uptime reset with a live host (tested through runner.failed directly).

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.

plugins: the runtime polls with nothing to run and rebinds every chat every 15 s

1 participant