fix: use context.WithoutCancel for graceful shutdown (gosec G118)#3929
Merged
raphael merged 2 commits intoMay 27, 2026
Merged
Conversation
3 tasks
Member
|
This is great, thank you! |
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.
Summary
http/codegen/templates/server_end.go.tpl) so the generated server file (cmd/<svc>/http.go) passesgolangci-lintwithgosecG118 (which flags implicitcontext.Background()/context.TODO()calls inside goroutines)ctxtoshutdownCtxto remove the shadowing of the outer context (thelog.Printfcalls below it picked up the wrong variable)http/codegen/testdata/golden/Today the generated server does
context.WithTimeout(context.Background(), 30*time.Second)inside the shutdown goroutine, which gosec G118 flags by default. It also drops any logger / trace span / request-scoped value attached to the outerctx.context.WithoutCancel(ctx)keeps those values while stripping cancellation. Graceful shutdown only runs after the outerctxis already canceled, socontext.WithTimeout(ctx, ...)would inherit that cancellation and fire immediately —WithoutCancelavoids this while preserving the logger / trace span attached to the original context.go.modalready requires Go 1.25 (≥ 1.21), socontext.WithoutCancelis unconditionally available.Continues the same golangci-lint cleanup series as #3927 and #3928 (with earlier precedents in #3541 / #3542 / #3548 / #3736).
Test plan
go test ./http/codegen/...make testmake lint