mirror of
https://github.com/fatedier/frp.git
synced 2026-11-05 07:01:39 +08:00
fix(server): close the mux session when a control is replaced or exits (#5596)
When a client logs in again with the same run ID, frps replaces the old control and closes its control stream, but the yamux session that carried it stays open until keepalive reclaims it. The replaced control's work connections and streams share that session, so they are left behind for up to tcpMuxKeepaliveInterval plus the ping budget. A mux session carries exactly one control connection, so the control now records its session and closes it from the single teardown point shared by replacement, fatal exit and shutdown. QUIC and non-multiplexed transports keep the previous path. Closing the session ends every stream that still belongs to the replaced control. Those proxies are already being torn down, so the cost is that in-flight connections of the old control are interrupted immediately instead of at the keepalive deadline. A replacement control is not affected, and the ownership rules for a still-online control do not change. Tests: replacement and fatal exit close the session while the replacement control keeps working, and concurrent logins with the same run ID leave exactly one live session.
This commit is contained in:
+7
-5
@@ -521,7 +521,7 @@ func (svr *Service) closeResources() {
|
||||
svr.ctlManager.Close()
|
||||
}
|
||||
|
||||
func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, internal bool) {
|
||||
func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, internal bool, session io.Closer) {
|
||||
xl := xlog.FromContextSafe(ctx)
|
||||
|
||||
acceptedConn, err := svr.acceptConnection(ctx, conn)
|
||||
@@ -552,7 +552,7 @@ func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, interna
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
ctl, err = svr.RegisterControl(controlConn, m, internal, acceptedConn.wireProtocol, acceptedConn.udpPacketCodec)
|
||||
ctl, err = svr.RegisterControl(controlConn, m, internal, acceptedConn.wireProtocol, acceptedConn.udpPacketCodec, session)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -813,10 +813,10 @@ func (svr *Service) HandleListener(l net.Listener, internal bool) {
|
||||
session.Close()
|
||||
return
|
||||
}
|
||||
go svr.handleConnection(ctx, stream, internal)
|
||||
go svr.handleConnection(ctx, stream, internal, session)
|
||||
}
|
||||
} else {
|
||||
svr.handleConnection(ctx, frpConn, internal)
|
||||
svr.handleConnection(ctx, frpConn, internal, nil)
|
||||
}
|
||||
}(ctx, c)
|
||||
}
|
||||
@@ -839,7 +839,7 @@ func (svr *Service) HandleQUICListener(l *quic.Listener) {
|
||||
_ = frpConn.CloseWithError(0, "")
|
||||
return
|
||||
}
|
||||
go svr.handleConnection(ctx, netpkg.QuicStreamToNetConn(stream, frpConn), false)
|
||||
go svr.handleConnection(ctx, netpkg.QuicStreamToNetConn(stream, frpConn), false, nil)
|
||||
}
|
||||
}(context.Background(), c)
|
||||
}
|
||||
@@ -851,6 +851,7 @@ func (svr *Service) RegisterControl(
|
||||
internal bool,
|
||||
wireProtocol string,
|
||||
udpPacketCodec string,
|
||||
session io.Closer,
|
||||
) (*Control, error) {
|
||||
switch wireProtocol {
|
||||
case wire.ProtocolV1:
|
||||
@@ -900,6 +901,7 @@ func (svr *Service) RegisterControl(
|
||||
AuthVerifier: authVerifier,
|
||||
EncryptionKey: svr.auth.EncryptionKey(),
|
||||
Conn: ctlConn,
|
||||
Session: session,
|
||||
LoginMsg: loginMsg,
|
||||
ServerCfg: svr.cfg,
|
||||
WireProtocol: wireProtocol,
|
||||
|
||||
Reference in New Issue
Block a user