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
+8
View File
@@ -17,6 +17,7 @@ package server
import (
"context"
"fmt"
"io"
"math"
"net"
"runtime/debug"
@@ -366,6 +367,9 @@ type SessionContext struct {
EncryptionKey []byte
// control connection
Conn *msg.Conn
// A mux session carries exactly one control. Close it when the control is
// replaced or exits fatally to release all remaining streams and work connections.
Session io.Closer
// login message
LoginMsg *msg.Login
// server configuration
@@ -575,6 +579,10 @@ func (ctl *Control) markReplaced() <-chan struct{} {
func (ctl *Control) interruptReadAndClose() error {
ctl.interruptOnce.Do(func() {
_ = ctl.sessionCtx.Conn.SetReadDeadline(time.Now())
if ctl.sessionCtx.Session != nil {
ctl.interruptErr = ctl.sessionCtx.Session.Close()
return
}
ctl.interruptErr = ctl.sessionCtx.Conn.Close()
})
return ctl.interruptErr