fix(web): reap the shutdown goroutine when Start fails to bind#74
Merged
Conversation
Server.Start spawns a goroutine that blocks on the caller's context until shutdown, then calls srv.Shutdown. But it waited on the caller's ctx directly, and Start has an early-return path: when srv.ListenAndServe returns a non-ErrServerClosed error — the common case being a bind failure, "address already in use" — Start returns that error WITHOUT cancelling ctx. The shutdown goroutine is then stuck on <-ctx.Done() forever (until the caller eventually cancels its context, which on an error return it may never do). Every failed bind leaks one goroutine. Fixed by deriving a cancellable context (runCtx) from the caller's ctx with a deferred cancel, and having the goroutine wait on runCtx. Now the goroutine is guaranteed to be reaped on every Start return path: on the bind-error path the deferred cancel wakes it (srv.Shutdown on a server that never served is a no-op), and on the normal path the caller's ctx cancellation still propagates through runCtx exactly as before, so graceful shutdown is unchanged. Regression test (mirrors the runtime.NumGoroutine before/after pattern already used in internal/agent/streaming_test.go): occupy a loopback port, call Start on it so ListenAndServe fails to bind, and assert — with the caller's ctx left uncancelled — that the goroutine count returns to baseline. Verified red→green: the test fails on the pre-fix code (2 -> 3 goroutines, one never reaped) and passes after the fix (reaped in ~0.07s). gofmt / go vet / golangci-lint (0 issues) / go test -race ./internal/web/ all green.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Server.Startspawns a goroutine that blocks on the caller's context until shutdown, then callssrv.Shutdown. It waited on the caller'sctxdirectly — butStarthas an early-return path: whensrv.ListenAndServereturns a non-ErrServerClosederror (the common case being a bind failure,address already in use),Startreturns that error without cancellingctx. The shutdown goroutine is then stuck on<-ctx.Done()forever (until the caller eventually cancels its context — which on an error return it may never do). Every failed bind leaks one goroutine.Fix
Derive a cancellable context (
runCtx) from the caller'sctxwith a deferred cancel, and have the goroutine wait onrunCtx:Now the goroutine is reaped on every return path:
cancelRunwakes it (srv.Shutdownon a server that never served is a no-op)ctxcancellation still propagates throughrunCtxexactly as before, so graceful shutdown is unchangedVerification
Regression test mirrors the
runtime.NumGoroutinebefore/after pattern already used ininternal/agent/streaming_test.go: occupy a loopback port, callStarton it soListenAndServefails to bind, and assert — with the caller'sctxleft uncancelled — that the goroutine count returns to baseline.Red→green proven: fails on pre-fix code (2 → 3 goroutines, one never reaped), passes after the fix (reaped in ~0.07s).
gofmt/go vetcleangolangci-lint run ./...→ 0 issuesgo test -race ./internal/web/→ok