Files
coder/cli
Michael Suchacz 7c39819665 fix(cli): close coderd on server startup errors (#30280)
`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.
2026-10-07 13:39:31 +02:00
..
…
…
…
…