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:
fatedier
2026-11-03 23:31:44 +08:00
committed by GitHub
parent 967a7505ae
commit f32adb3bbb
4 changed files with 208 additions and 12 deletions
+7 -5
View File
@@ -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,