Skip to content

fix: use context.WithoutCancel for graceful shutdown (gosec G118)#3929

Merged
raphael merged 2 commits into
goadesign:v3from
stakahashy:fix/shutdown-context-without-cancel
May 27, 2026
Merged

fix: use context.WithoutCancel for graceful shutdown (gosec G118)#3929
raphael merged 2 commits into
goadesign:v3from
stakahashy:fix/shutdown-context-without-cancel

Conversation

@stakahashy

@stakahashy stakahashy commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • update the server codegen template (http/codegen/templates/server_end.go.tpl) so the generated server file (cmd/<svc>/http.go) passes golangci-lint with gosec G118 (which flags implicit context.Background() / context.TODO() calls inside goroutines)
  • rename the local ctx to shutdownCtx to remove the shadowing of the outer context (the log.Printf calls below it picked up the wrong variable)
  • update the affected server golden files under 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 outer ctx.

context.WithoutCancel(ctx) keeps those values while stripping cancellation. Graceful shutdown only runs after the outer ctx is already canceled, so context.WithTimeout(ctx, ...) would inherit that cancellation and fire immediately — WithoutCancel avoids this while preserving the logger / trace span attached to the original context.

go.mod already requires Go 1.25 (≥ 1.21), so context.WithoutCancel is 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 test
  • make lint

@raphael

raphael commented May 27, 2026

Copy link
Copy Markdown
Member

This is great, thank you!

@raphael raphael merged commit 0c5fa35 into goadesign:v3 May 27, 2026
5 checks passed
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