-
Notifications
You must be signed in to change notification settings - Fork 461
fix(api): keep API alive until in-flight requests finish #2708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jakubno
merged 8 commits into
main
from
bug-keep-api-alive-until-it-finishes-all-requests-eng-4076
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
845f8ec
fix(api): keep API alive until in-flight requests finish
jakubno d92c6f0
Apply suggestion from @jakubno
jakubno 9d5e7cf
fix(api): drain HTTP, gRPC, and pprof in parallel on shutdown
jakubno cf00e24
fix(api): don't wait on GracefulStop goroutine after forced Stop
jakubno f5d8d8d
fix(api): match gRPC shutdown budget to HTTP
jakubno af42f71
Apply suggestion from @jakubno
jakubno 1b127c9
chore: merge timeouts
jakubno a1d6b44
chore: merge timeouts
jakubno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package grpc | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "google.golang.org/grpc" | ||
| ) | ||
|
|
||
| // GracefulStopWithTimeout invokes srv.GracefulStop and falls back to Stop() if | ||
| // it does not return within d. Returns true if graceful stop completed before | ||
| // the deadline. | ||
| // | ||
| // grpc.Server.GracefulStop blocks until all pending RPCs finish, with no | ||
| // built-in deadline. A stuck stream would otherwise block process shutdown | ||
| // past Nomad's kill_timeout and result in SIGKILL. | ||
| // | ||
| // Stop() force-closes transports | ||
| func GracefulStopWithTimeout(srv *grpc.Server, d time.Duration) bool { | ||
| done := make(chan struct{}) | ||
|
|
||
| go func() { | ||
| srv.GracefulStop() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| return true | ||
| case <-time.After(d): | ||
| srv.Stop() | ||
|
|
||
| return false | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package grpc | ||
|
|
||
| import ( | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "google.golang.org/grpc" | ||
| ) | ||
|
|
||
| // TestGracefulStopWithTimeout_Clean ensures the helper returns true when the | ||
| // server has no in-flight RPCs and GracefulStop completes immediately. | ||
| func TestGracefulStopWithTimeout_Clean(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| srv := grpc.NewServer() | ||
| listenCfg := &net.ListenConfig{} | ||
| ln, err := listenCfg.Listen(t.Context(), "tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| go func() { _ = srv.Serve(ln) }() | ||
|
|
||
| start := time.Now() | ||
| ok := GracefulStopWithTimeout(srv, time.Second) | ||
| if !ok { | ||
| t.Fatal("expected clean stop") | ||
| } | ||
| if elapsed := time.Since(start); elapsed > 500*time.Millisecond { | ||
| t.Fatalf("stop took too long: %s", elapsed) | ||
| } | ||
| } | ||
|
|
||
| // TestGracefulStopWithTimeout_AlreadyStopped is a sanity check that calling | ||
| // the helper on an already-stopped server doesn't hang. | ||
| func TestGracefulStopWithTimeout_AlreadyStopped(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| srv := grpc.NewServer() | ||
| listenCfg := &net.ListenConfig{} | ||
| ln, err := listenCfg.Listen(t.Context(), "tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| go func() { _ = srv.Serve(ln) }() | ||
|
|
||
| if !GracefulStopWithTimeout(srv, time.Second) { | ||
| t.Fatal("first stop should be clean") | ||
| } | ||
| // Second invocation: GracefulStop on a stopped server is a no-op. | ||
| if !GracefulStopWithTimeout(srv, time.Second) { | ||
| t.Fatal("second stop should also report clean") | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using time.After in a select block creates a new timer that is not garbage collected until it expires, even if the other case is selected. While this is called during shutdown, it is better practice to use time.NewTimer and stop it to avoid unnecessary resource retention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read the docs: