Skip to content

Commit 0c5fa35

Browse files
stakahashyraphael
andauthored
fix: use context.WithoutCancel for graceful shutdown (gosec G118) (#3929)
Co-authored-by: Raphael Simon <simon.raphael@gmail.com>
1 parent a8e9551 commit 0c5fa35

6 files changed

Lines changed: 18 additions & 18 deletions

http/codegen/templates/server_end.go.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
2424

2525
{{ comment "Shutdown gracefully with a 30s timeout." }}
26-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
26+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
2727
defer cancel()
2828

29-
err := srv.Shutdown(ctx)
29+
err := srv.Shutdown(shutdownCtx)
3030
if err != nil {
31-
log.Printf(ctx, "failed to shutdown: %v", err)
31+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
3232
}
3333
}()
3434
}

http/codegen/testdata/golden/server-no-server.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service
6767
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
6868

6969
// Shutdown gracefully with a 30s timeout.
70-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
70+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
7171
defer cancel()
7272

73-
err := srv.Shutdown(ctx)
73+
err := srv.Shutdown(shutdownCtx)
7474
if err != nil {
75-
log.Printf(ctx, "failed to shutdown: %v", err)
75+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
7676
}
7777
}()
7878
}

http/codegen/testdata/golden/server-server-hosting-multiple-services.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service
7373
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
7474

7575
// Shutdown gracefully with a 30s timeout.
76-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
76+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
7777
defer cancel()
7878

79-
err := srv.Shutdown(ctx)
79+
err := srv.Shutdown(shutdownCtx)
8080
if err != nil {
81-
log.Printf(ctx, "failed to shutdown: %v", err)
81+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
8282
}
8383
}()
8484
}

http/codegen/testdata/golden/server-server-hosting-service-subset.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, serviceEndpoints *service
6767
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
6868

6969
// Shutdown gracefully with a 30s timeout.
70-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
70+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
7171
defer cancel()
7272

73-
err := srv.Shutdown(ctx)
73+
err := srv.Shutdown(shutdownCtx)
7474
if err != nil {
75-
log.Printf(ctx, "failed to shutdown: %v", err)
75+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
7676
}
7777
}()
7878
}

http/codegen/testdata/golden/server-server-hosting-service-with-file-server.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, wg *sync.WaitGroup, errc
6767
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
6868

6969
// Shutdown gracefully with a 30s timeout.
70-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
70+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
7171
defer cancel()
7272

73-
err := srv.Shutdown(ctx)
73+
err := srv.Shutdown(shutdownCtx)
7474
if err != nil {
75-
log.Printf(ctx, "failed to shutdown: %v", err)
75+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
7676
}
7777
}()
7878
}

http/codegen/testdata/golden/server-streaming.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ func handleHTTPServer(ctx context.Context, u *url.URL, streamingServiceAEndpoint
7474
log.Printf(ctx, "shutting down HTTP server at %q", u.Host)
7575

7676
// Shutdown gracefully with a 30s timeout.
77-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
77+
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
7878
defer cancel()
7979

80-
err := srv.Shutdown(ctx)
80+
err := srv.Shutdown(shutdownCtx)
8181
if err != nil {
82-
log.Printf(ctx, "failed to shutdown: %v", err)
82+
log.Printf(shutdownCtx, "failed to shutdown: %v", err)
8383
}
8484
}()
8585
}

0 commit comments

Comments
 (0)