fix: drain in-flight requests on SIGTERM and close idle connections promptly (#40472)

`gitea web` derived the graceful manager's context from the CLI context,
which is canceled on the first SIGTERM, so in-flight requests were
force-closed and the terminate stage never ran.

- Detach the manager's context from that cancellation. With
`SKIP_MINWINSVC=1` on Windows, Ctrl+C now takes two.
- Close idle HTTP/2 connections at shutdown, which otherwise held every
drain until the hammer.

| SIGTERM with | main | this PR |
|---|---|---|
| no connections | 0.01s, no terminate stage | 1.03s |
| 64 MiB download in flight | cut after 10 MB | completes |
| idle HTTP/2 connection | 0.01s | 1.01s |

Regressed in: https://github.com/go-gitea/gitea/pull/35348
Carries forward https://github.com/go-gitea/gitea/pull/39533 by
@mandel-macaque.

Assisted-by: Hermes Agent:claude-opus-5-5
Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-11-22 10:05:40 +00:00
1 parent 2b843af2b8
commit 890effbc91
5 files changed
+10 -41

No files matched your search

+1 -5
View File
@@ -258,15 +258,11 @@ func servePprof() {
finished()
}
func runWeb(ctx context.Context, cmd *cli.Command) error {
func runWeb(_ context.Context, cmd *cli.Command) error {
if subCmdName, valid := isValidDefaultSubCommand(cmd); !valid {
return fmt.Errorf("unknown command: %s", subCmdName)
}
managerCtx, cancel := context.WithCancel(ctx)
graceful.InitManager(managerCtx)
defer cancel()
if os.Getppid() > 1 && len(os.Getenv("LISTEN_FDS")) > 0 {
log.Info("Restarting Gitea on PID: %d from parent PID: %d", os.Getpid(), os.Getppid())
} else {
+4 -24
View File
@@ -39,30 +39,10 @@ type RunCanceler interface {
// and add a function to call manager.InformCleanup if it's not going to be used
const numberOfServersToCreate = 4
var (
manager *Manager
initOnce sync.Once
)
// GetManager returns the Manager
func GetManager() *Manager {
initManager(context.Background())
return manager
}
// InitManager creates the graceful manager in the provided context
func InitManager(ctx context.Context) {
if manager != nil {
log.Error("graceful.InitManager called more than once")
}
initManager(ctx) // FIXME: this design is not right, it conflicts with the "Background" context used in GetManager
}
func initManager(ctx context.Context) {
initOnce.Do(func() {
manager = newGracefulManager(ctx)
})
}
// GetManager gets the global graceful manager which uses process signal to cancel its managed contexts
var GetManager = sync.OnceValue(func() *Manager {
return newGracefulManager(context.Background())
})
// RunWithCancel helps to run a function with a custom context, the Cancel function will be called at shutdown
// The Cancel function should stop the Run function in predictable time.
+4 -6
View File
@@ -24,9 +24,7 @@ func (srv *Server) awaitShutdown() {
srv.doHammer()
}
// shutdown closes the listener so that no new connections are accepted
// and starts a goroutine that will hammer (stop all running requests) the server
// after setting.GracefulHammerTime.
// doShutdown closes the listener so that no new connections are accepted, then runs OnShutdown
func (srv *Server) doShutdown() {
// only shutdown if we're running.
if srv.getState() != stateRunning {
@@ -35,15 +33,15 @@ func (srv *Server) doShutdown() {
srv.setState(stateShuttingDown)
if srv.OnShutdown != nil {
srv.OnShutdown()
}
err := srv.listener.Close()
if err != nil {
log.Error("PID: %d Listener.Close() error: %v", os.Getpid(), err)
} else {
log.Info("PID: %d Listener (%s) closed.", os.Getpid(), srv.listener.Addr())
}
if srv.OnShutdown != nil {
srv.OnShutdown()
}
}
func (srv *Server) doHammer() {
+1 -1
View File
@@ -24,7 +24,7 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
ReadHeaderTimeout: 10 * time.Second,
}
server.OnShutdown = func() {
httpServer.SetKeepAlivesEnabled(false)
_ = httpServer.Shutdown(GetManager().HammerContext()) // unlike SetKeepAlivesEnabled(false), it also closes idle HTTP/2 connections
}
return server, httpServer.Serve
}
-5
View File
@@ -5,7 +5,6 @@ package integration
import (
"bytes"
"context"
"encoding/base64"
"flag"
"fmt"
@@ -44,10 +43,6 @@ var testWebRoutes *web.Router
func testMain(m *testing.M) int {
defer log.GetManager().Close()
managerCtx, cancel := context.WithCancel(context.Background())
graceful.InitManager(managerCtx)
defer cancel()
workPath, err := tests.InitIntegrationTest()
if err != nil {
return testlogger.MainErrorf("InitTest error: %v", err)