fix(client): synchronize graceful shutdown duration (#5442)

This commit is contained in:
fatedier
2026-07-23 13:55:00 +08:00
committed by GitHub
parent 466a94f5d1
commit 18eef83b69
2 changed files with 103 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ import (
"net/http"
"os"
"sync"
"sync/atomic"
"time"
"github.com/fatedier/golib/crypto"
@@ -109,6 +110,9 @@ func setServiceOptionsDefault(options *ServiceOptions) error {
// Service is the client service that connects to frps and provides proxy services.
type Service struct {
ctlMu sync.RWMutex
// Stores gracefulShutdownDuration independently from ctlMu, because the
// graceful shutdown wait may hold ctlMu for an arbitrary duration.
gracefulShutdownDuration atomic.Int64
// manager control connection with server
ctl *Control
// Uniq id got from frps, it will be attached to loginMsg.
@@ -149,8 +153,7 @@ type Service struct {
// service context
ctx context.Context
// call cancel to stop service
cancel context.CancelCauseFunc
gracefulShutdownDuration time.Duration
cancel context.CancelCauseFunc
connectorCreator func(context.Context, *v1.ClientCommonConfig) Connector
handleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
@@ -412,7 +415,7 @@ func (svr *Service) Close() {
}
func (svr *Service) GracefulClose(d time.Duration) {
svr.gracefulShutdownDuration = d
svr.gracefulShutdownDuration.Store(int64(d))
svr.cancel(nil)
}
@@ -429,7 +432,8 @@ func (svr *Service) stop() {
svr.ctlMu.Lock()
defer svr.ctlMu.Unlock()
if svr.ctl != nil {
svr.ctl.GracefulClose(svr.gracefulShutdownDuration)
d := time.Duration(svr.gracefulShutdownDuration.Load())
svr.ctl.GracefulClose(d)
svr.ctl = nil
}
if svr.webServer != nil {