mirror of
https://github.com/fatedier/frp.git
synced 2026-10-15 02:35:56 +08:00
fix: synchronize service startup and shutdown (#5560)
This commit is contained in:
@@ -267,8 +267,9 @@ func (pw *Wrapper) InWorkConn(workConn net.Conn, m *msg.StartWorkConn) {
|
||||
xl := pw.xl
|
||||
pw.mu.RLock()
|
||||
pxy := pw.pxy
|
||||
running := pw.Phase == ProxyPhaseRunning
|
||||
pw.mu.RUnlock()
|
||||
if pxy != nil && pw.Phase == ProxyPhaseRunning {
|
||||
if pxy != nil && running {
|
||||
xl.Debugf("start a new work connection, localAddr: %s remoteAddr: %s", workConn.LocalAddr().String(), workConn.RemoteAddr().String())
|
||||
go pxy.InWorkConn(workConn, m)
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//go:build !frps
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/fatedier/frp/pkg/msg"
|
||||
"github.com/fatedier/frp/pkg/util/xlog"
|
||||
)
|
||||
|
||||
type closingWorkProxy struct {
|
||||
Proxy
|
||||
accepted atomic.Int64
|
||||
}
|
||||
|
||||
func (p *closingWorkProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
|
||||
p.accepted.Add(1)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
func assertWrapperWorkConnClosed(t *testing.T, pw *Wrapper) {
|
||||
t.Helper()
|
||||
conn, peer := net.Pipe()
|
||||
defer conn.Close()
|
||||
defer peer.Close()
|
||||
require.NoError(t, peer.SetReadDeadline(time.Now().Add(time.Second)))
|
||||
pw.InWorkConn(conn, &msg.StartWorkConn{})
|
||||
_, err := peer.Read(make([]byte, 1))
|
||||
require.ErrorIs(t, err, io.EOF)
|
||||
}
|
||||
|
||||
func TestWrapperInWorkConnPhaseSnapshot(t *testing.T) {
|
||||
pxy := &closingWorkProxy{}
|
||||
pw := &Wrapper{pxy: pxy, xl: xlog.New()}
|
||||
for _, phase := range []string{ProxyPhaseNew, ProxyPhaseWaitStart, ProxyPhaseStartErr, ProxyPhaseCheckFailed, ProxyPhaseClosed} {
|
||||
pw.Phase = phase
|
||||
assertWrapperWorkConnClosed(t, pw)
|
||||
}
|
||||
require.Zero(t, pxy.accepted.Load(), "inactive proxy received a work connection")
|
||||
pw.Phase = ProxyPhaseRunning
|
||||
pw.pxy = nil
|
||||
assertWrapperWorkConnClosed(t, pw)
|
||||
pw.pxy = pxy
|
||||
assertWrapperWorkConnClosed(t, pw)
|
||||
require.Equal(t, int64(1), pxy.accepted.Load())
|
||||
|
||||
start, done := make(chan struct{}), make(chan struct{})
|
||||
go func() {
|
||||
<-start
|
||||
for range 20000 {
|
||||
pw.mu.Lock()
|
||||
pw.Phase = ProxyPhaseRunning
|
||||
pw.mu.Unlock()
|
||||
pw.mu.Lock()
|
||||
pw.Phase = ProxyPhaseClosed
|
||||
pw.mu.Unlock()
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
close(start)
|
||||
for range 10000 {
|
||||
assertWrapperWorkConnClosed(t, pw)
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("phase writer did not finish")
|
||||
}
|
||||
}
|
||||
+29
-4
@@ -155,6 +155,9 @@ type Service struct {
|
||||
ctx context.Context
|
||||
// call cancel to stop service
|
||||
cancel context.CancelCauseFunc
|
||||
// Protect cancel publication and shutdown before the first Run.
|
||||
lifecycleMu sync.Mutex
|
||||
closed bool
|
||||
|
||||
connectorCreator func(context.Context, *v1.ClientCommonConfig) Connector
|
||||
handleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
|
||||
@@ -225,8 +228,20 @@ func NewService(options ServiceOptions) (*Service, error) {
|
||||
|
||||
func (svr *Service) Run(ctx context.Context) error {
|
||||
ctx, cancel := context.WithCancelCause(ctx)
|
||||
svr.lifecycleMu.Lock()
|
||||
if svr.closed {
|
||||
svr.lifecycleMu.Unlock()
|
||||
cancel(nil)
|
||||
return ctx.Err()
|
||||
}
|
||||
svr.ctx = xlog.NewContext(ctx, xlog.FromContextSafe(ctx))
|
||||
svr.cancel = cancel
|
||||
svr.lifecycleMu.Unlock()
|
||||
defer cancel(nil)
|
||||
defer svr.stop()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set custom DNSServer
|
||||
if svr.common.DNSServer != "" {
|
||||
@@ -237,7 +252,6 @@ func (svr *Service) Run(ctx context.Context) error {
|
||||
vnetController := svr.vnetController
|
||||
if err := svr.vnetController.Init(); err != nil {
|
||||
log.Errorf("init virtual network controller error: %v", err)
|
||||
svr.stop()
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
@@ -263,14 +277,12 @@ func (svr *Service) Run(ctx context.Context) error {
|
||||
if svr.ctl == nil {
|
||||
cancelCause := cancelErr{}
|
||||
_ = errors.As(context.Cause(svr.ctx), &cancelCause)
|
||||
svr.stop()
|
||||
return fmt.Errorf("login to the server failed: %v. With loginFailExit enabled, no additional retries will be attempted", cancelCause.Err)
|
||||
}
|
||||
|
||||
go svr.keepControllerWorking()
|
||||
|
||||
<-svr.ctx.Done()
|
||||
svr.stop()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -428,7 +440,20 @@ func (svr *Service) Close() {
|
||||
|
||||
func (svr *Service) GracefulClose(d time.Duration) {
|
||||
svr.gracefulShutdownDuration.Store(int64(d))
|
||||
svr.cancel(nil)
|
||||
svr.lifecycleMu.Lock()
|
||||
if svr.closed {
|
||||
svr.lifecycleMu.Unlock()
|
||||
return
|
||||
}
|
||||
svr.closed = true
|
||||
cancel := svr.cancel
|
||||
svr.lifecycleMu.Unlock()
|
||||
if cancel != nil {
|
||||
// Once Run has claimed startup, it also owns cleanup.
|
||||
cancel(nil)
|
||||
} else {
|
||||
svr.stop()
|
||||
}
|
||||
}
|
||||
|
||||
func (svr *Service) stop() {
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/fatedier/frp/pkg/config/source"
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/config/v1/validation"
|
||||
"github.com/fatedier/frp/pkg/msg"
|
||||
)
|
||||
|
||||
func newLifecycleTestService(t *testing.T, creator func(context.Context, *v1.ClientCommonConfig) Connector) (*Service, string) {
|
||||
t.Helper()
|
||||
svr, err := NewService(ServiceOptions{
|
||||
Common: &v1.ClientCommonConfig{
|
||||
LoginFailExit: new(false),
|
||||
WebServer: v1.WebServerConfig{Addr: "127.0.0.1", Port: getFreeTCPPort(t)},
|
||||
},
|
||||
ConfigSourceAggregator: source.NewAggregator(source.NewConfigSource()),
|
||||
ConnectorCreator: creator,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = validation.NewConfigValidator(nil).ValidateClientCommonConfig(svr.common)
|
||||
require.NoError(t, err)
|
||||
address := svr.webServer.Address()
|
||||
assertLifecycleAdminPort(t, address, false)
|
||||
return svr, address
|
||||
}
|
||||
|
||||
func assertLifecycleAdminPort(t *testing.T, address string, released bool) {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", address)
|
||||
if ln != nil {
|
||||
_ = ln.Close()
|
||||
}
|
||||
if released {
|
||||
require.NoError(t, err, "admin listener was not released")
|
||||
} else {
|
||||
require.Error(t, err, "admin listener was not bound")
|
||||
}
|
||||
}
|
||||
|
||||
func waitLifecycleSignal(t *testing.T, done <-chan struct{}) {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("service lifecycle operation did not finish")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRunAfterEarlyShutdown(t *testing.T) {
|
||||
for _, mode := range []string{"close before Run", "parent already cancelled"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
var attempts atomic.Int64
|
||||
svr, address := newLifecycleTestService(t, func(context.Context, *v1.ClientCommonConfig) Connector {
|
||||
attempts.Add(1)
|
||||
return &failingConnector{err: net.ErrClosed}
|
||||
})
|
||||
// Detect startup's global DNS side effect without making DNS requests.
|
||||
resolver := net.DefaultResolver
|
||||
t.Cleanup(func() { net.DefaultResolver = resolver })
|
||||
svr.common.DNSServer = "127.0.0.1:53"
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
if mode == "close before Run" {
|
||||
svr.Close()
|
||||
assertLifecycleAdminPort(t, address, true)
|
||||
} else {
|
||||
cancel()
|
||||
}
|
||||
done := make(chan struct{})
|
||||
var runErr error
|
||||
go func() {
|
||||
runErr = svr.Run(ctx)
|
||||
close(done)
|
||||
}()
|
||||
waitLifecycleSignal(t, done)
|
||||
require.ErrorIs(t, runErr, context.Canceled)
|
||||
require.Zero(t, attempts.Load(), "closed service attempted login")
|
||||
require.Same(t, resolver, net.DefaultResolver, "closed service changed DNS")
|
||||
assertLifecycleAdminPort(t, address, true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceStartupShutdownLeavesCleanupToRun(t *testing.T) {
|
||||
for _, mode := range []string{"Close", "parent cancellation"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
entered, resume := make(chan struct{}), make(chan struct{})
|
||||
svr, address := newLifecycleTestService(t, func(ctx context.Context, _ *v1.ClientCommonConfig) Connector {
|
||||
close(entered)
|
||||
<-resume
|
||||
return &failingConnector{err: ctx.Err()}
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
defer close(resume)
|
||||
runDone := make(chan struct{})
|
||||
go func() { _ = svr.Run(ctx); close(runDone) }()
|
||||
waitLifecycleSignal(t, entered)
|
||||
closeDone := make(chan struct{})
|
||||
go func() {
|
||||
if mode == "Close" {
|
||||
svr.Close()
|
||||
} else {
|
||||
cancel()
|
||||
}
|
||||
close(closeDone)
|
||||
}()
|
||||
waitLifecycleSignal(t, closeDone)
|
||||
select {
|
||||
case <-runDone:
|
||||
t.Fatal("Run exited while startup still owned its resources")
|
||||
default:
|
||||
}
|
||||
assertLifecycleAdminPort(t, address, false)
|
||||
resume <- struct{}{}
|
||||
waitLifecycleSignal(t, runDone)
|
||||
assertLifecycleAdminPort(t, address, true)
|
||||
require.ErrorIs(t, svr.ctx.Err(), context.Canceled)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceShutdownAfterLogin(t *testing.T) {
|
||||
for _, mode := range []string{"Close", "parent cancellation"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
clientConn, serverConn := net.Pipe()
|
||||
defer clientConn.Close()
|
||||
defer serverConn.Close()
|
||||
connector := &testConnector{conn: clientConn}
|
||||
svr, address := newLifecycleTestService(t, func(context.Context, *v1.ClientCommonConfig) Connector {
|
||||
return connector
|
||||
})
|
||||
defer svr.Close()
|
||||
peerDone := make(chan error, 1)
|
||||
go func() {
|
||||
rw := msg.NewV1ReadWriter(serverConn)
|
||||
var login msg.Login
|
||||
if err := rw.ReadMsgInto(&login); err != nil {
|
||||
peerDone <- err
|
||||
return
|
||||
}
|
||||
if err := rw.WriteMsg(&msg.LoginResp{RunID: "lifecycle-test"}); err != nil {
|
||||
peerDone <- err
|
||||
return
|
||||
}
|
||||
_, err := io.Copy(io.Discard, serverConn)
|
||||
peerDone <- err
|
||||
}()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
runDone := make(chan struct{})
|
||||
var runErr error
|
||||
go func() { runErr = svr.Run(ctx); close(runDone) }()
|
||||
require.Eventually(t, func() bool {
|
||||
svr.ctlMu.RLock()
|
||||
defer svr.ctlMu.RUnlock()
|
||||
return svr.ctl != nil
|
||||
}, 3*time.Second, time.Millisecond, "login did not establish control")
|
||||
if mode == "Close" {
|
||||
svr.Close()
|
||||
} else {
|
||||
cancel()
|
||||
}
|
||||
waitLifecycleSignal(t, runDone)
|
||||
require.NoError(t, runErr)
|
||||
require.True(t, connector.closed.Load())
|
||||
select {
|
||||
case err := <-peerDone:
|
||||
require.NoError(t, err)
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("control connection was not released")
|
||||
}
|
||||
assertLifecycleAdminPort(t, address, true)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user