From 4e025170013cb5f05abd4cb5c18d2a7410d1dc96 Mon Sep 17 00:00:00 2001 From: Shuntaro Takahashi Date: Mon, 25 May 2026 21:36:43 +0900 Subject: [PATCH] fix: use context.WithoutCancel for graceful shutdown (gosec G118) --- http/codegen/templates/server_end.go.tpl | 6 +++--- http/codegen/testdata/golden/server-no-server.golden | 6 +++--- .../golden/server-server-hosting-multiple-services.golden | 6 +++--- .../golden/server-server-hosting-service-subset.golden | 6 +++--- .../server-server-hosting-service-with-file-server.golden | 6 +++--- http/codegen/testdata/golden/server-streaming.golden | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/http/codegen/templates/server_end.go.tpl b/http/codegen/templates/server_end.go.tpl index 7316147eb2..b6f79f41e8 100644 --- a/http/codegen/templates/server_end.go.tpl +++ b/http/codegen/templates/server_end.go.tpl @@ -23,12 +23,12 @@ log.Printf(ctx, "shutting down HTTP server at %q", u.Host) {{ comment "Shutdown gracefully with a 30s timeout." }} - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() } diff --git a/http/codegen/testdata/golden/server-no-server.golden b/http/codegen/testdata/golden/server-no-server.golden index b2cf9e4ca3..7b98d373a8 100644 --- a/http/codegen/testdata/golden/server-no-server.golden +++ b/http/codegen/testdata/golden/server-no-server.golden @@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service log.Printf(ctx, "shutting down HTTP server at %q", u.Host) // Shutdown gracefully with a 30s timeout. - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() } diff --git a/http/codegen/testdata/golden/server-server-hosting-multiple-services.golden b/http/codegen/testdata/golden/server-server-hosting-multiple-services.golden index 1c7908993b..b21132dcb9 100644 --- a/http/codegen/testdata/golden/server-server-hosting-multiple-services.golden +++ b/http/codegen/testdata/golden/server-server-hosting-multiple-services.golden @@ -73,12 +73,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service log.Printf(ctx, "shutting down HTTP server at %q", u.Host) // Shutdown gracefully with a 30s timeout. - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() } diff --git a/http/codegen/testdata/golden/server-server-hosting-service-subset.golden b/http/codegen/testdata/golden/server-server-hosting-service-subset.golden index b2cf9e4ca3..7b98d373a8 100644 --- a/http/codegen/testdata/golden/server-server-hosting-service-subset.golden +++ b/http/codegen/testdata/golden/server-server-hosting-service-subset.golden @@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service log.Printf(ctx, "shutting down HTTP server at %q", u.Host) // Shutdown gracefully with a 30s timeout. - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() } diff --git a/http/codegen/testdata/golden/server-server-hosting-service-with-file-server.golden b/http/codegen/testdata/golden/server-server-hosting-service-with-file-server.golden index 09d325cb0d..a30a6ff0ad 100644 --- a/http/codegen/testdata/golden/server-server-hosting-service-with-file-server.golden +++ b/http/codegen/testdata/golden/server-server-hosting-service-with-file-server.golden @@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, wg *sync.WaitGroup, errc log.Printf(ctx, "shutting down HTTP server at %q", u.Host) // Shutdown gracefully with a 30s timeout. - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() } diff --git a/http/codegen/testdata/golden/server-streaming.golden b/http/codegen/testdata/golden/server-streaming.golden index 5de719588c..6fcd1e1c66 100644 --- a/http/codegen/testdata/golden/server-streaming.golden +++ b/http/codegen/testdata/golden/server-streaming.golden @@ -74,12 +74,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, streamingServiceAEndpoint log.Printf(ctx, "shutting down HTTP server at %q", u.Host) // Shutdown gracefully with a 30s timeout. - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() - err := srv.Shutdown(ctx) + err := srv.Shutdown(shutdownCtx) if err != nil { - log.Printf(ctx, "failed to shutdown: %v", err) + log.Printf(shutdownCtx, "failed to shutdown: %v", err) } }() }