Skip to content

fix(cli): close coderd on server startup errors - #30280

Open
ibetitsmike wants to merge 1 commit into
mainfrom
mike/cli-server-startup-leak
Open

ibetitsmike wants to merge 1 commit into
mainfrom
mike/cli-server-startup-leak

Conversation

@ibetitsmike

Copy link
Copy Markdown
Collaborator

coder server closes the coderd API only in its graceful shutdown sequence. Every startup error returned after newAPI (creating aibridged, registering Prometheus metrics, writing the config URL, creating provisioner daemons, notifying systemd) skipped that close and leaked the whole coderd instance: tailnet and wireguard-go, gvisor, metricscache, dbrollup, cryptokeys, chatd, and pubsub queues. TestServer/Logging hits this when test cleanup cancels startup while aibridged is being created. The test helper ignores the resulting context canceled error, so only the package-level goleak check in cli fails.

The fix closes the API in a deferred sync.OnceValue registered right after newAPI, and the shutdown sequence calls the same function. The guard is needed because the closer is not safe to call twice: the AGPL API returns "API already closed", and the enterprise closer closes each component again. The normal shutdown path is unchanged.

Testing

The new TestServer/StartupErrorNoLeak fails startup after coderd is created by putting a directory at the config URL path.

  • Red (test only): go test ./cli -run '^TestServer$/^StartupErrorNoLeak$' -count=1 exited 1 in 3 of 3 runs with goleak: Errors on successful test run: found unexpected goroutines (coderd frames) while the subtest passed.
  • Green (with the fix): the same command passed in 3 of 3 runs.
  • TestServer/Logging alone, 20 runs from one test binary: 20 of 20 passed with no goleak failure, including 3 runs that hit the create aibridged startup error.
  • go test ./cli -count=1 passed in 4 of 4 runs, and go vet ./cli is clean.

Found in the CI of #30272 (test-go-pg (ubuntu-latest)).

Xum, an AI coding agent, implemented, tested, and opened this PR on behalf of @ibetitsmike.

`coder server` closed the coderd API only in its shutdown sequence,
so every startup error returned after the API was created leaked the
whole instance (tailnet, chatd, metricscache, pubsub queues). In CI,
TestServer/Logging hits this when cleanup cancels startup while
aibridged is being created, and the cli package goleak check fails.

Close the API in a defer right after it is created and reuse the same
call in the shutdown sequence. A sync.OnceValue guards it because the
closer is not safe to call twice.
@ibetitsmike
ibetitsmike marked this pull request as ready for review October 2, 2026 12:34
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 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-02T12:37:09.642736Z 9b2476d Draft marked ready
ℹ️ 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.

This branch has not been deployed

No deployments
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.

2 participants