only clean up connections when stopped

This commit is contained in:
WeidiDeng 2026-04-21 17:09:40 +08:00
parent 050d137e76
commit f04b77198e
No known key found for this signature in database
GPG Key ID: 25F87CE1741EC7CD

View File

@ -451,6 +451,7 @@ type tunnelState struct {
connections map[io.ReadWriteCloser]openConnection
closeTimer *time.Timer
closeDelay time.Duration
stopped bool
mu sync.Mutex
logger *zap.Logger
}
@ -472,7 +473,7 @@ func (ts *tunnelState) registerConnection(conn io.ReadWriteCloser, gracefulClose
return func() {
ts.mu.Lock()
delete(ts.connections, conn)
if len(ts.connections) == 0 {
if len(ts.connections) == 0 && ts.stopped {
unregisterDetachedTunnelStates(ts)
if ts.closeTimer != nil {
if ts.closeTimer.Stop() {
@ -490,6 +491,7 @@ func (ts *tunnelState) closeAttachedConnections() error {
var err error
ts.mu.Lock()
defer ts.mu.Unlock()
ts.stopped = true
for _, oc := range ts.connections {
// detached connections are only closed when the upstream is gone from the config
if oc.detached {
@ -654,6 +656,9 @@ func (ts *tunnelState) closeConnectionsForUpstream(addr string) error {
var err error
ts.mu.Lock()
defer ts.mu.Unlock()
if !ts.stopped {
return nil
}
for _, oc := range ts.connections {
if oc.upstream != addr {
continue