Skip to content

Commit 5e07cc5

Browse files
committed
fix: Cleaner cannot delete objects from pool occuring memory leaks
1 parent 96ef656 commit 5e07cc5

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

internal/bridge/clean.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,9 @@ func Clean() {
6565
}
6666
}
6767

68-
for _, tunnel := range Tunnels.Connections {
69-
if now.Sub(tunnel.LastConnection).Seconds() > 266 {
70-
closeTunnel(tunnel)
71-
continue
72-
}
73-
74-
if tunnel.SshClient == nil {
68+
for key, tunnel := range Tunnels.Connections {
69+
if now.Sub(tunnel.LastConnection).Seconds() > 266 || tunnel.SshClient == nil {
70+
closeTunnel(tunnel, key)
7571
continue
7672
}
7773

@@ -81,7 +77,7 @@ func Clean() {
8177
10*time.Second,
8278
)
8379
if err != nil {
84-
closeTunnel(tunnel)
80+
closeTunnel(tunnel, key)
8581
continue
8682
}
8783

@@ -94,7 +90,7 @@ func Clean() {
9490
default:
9591
_, _, err := tunnel.SshClient.SendRequest("[email protected]", true, nil)
9692
if err != nil {
97-
closeTunnel(tunnel)
93+
closeTunnel(tunnel, key)
9894
logger.Sugar().Warnw("error when sending request")
9995
}
10096

@@ -106,7 +102,7 @@ func Clean() {
106102
case <-ch:
107103
continue
108104
case <-time.After(10 * time.Second):
109-
closeTunnel(tunnel)
105+
closeTunnel(tunnel, key)
110106
continue
111107
}
112108
}
@@ -118,15 +114,13 @@ func Clean() {
118114
func closeSession(s *Session, key string) {
119115
s.Mutex.Lock()
120116
s.CloseAllConnections()
121-
s.Mutex.Unlock()
122-
123117
Sessions.Delete(key)
118+
s.Mutex.Unlock()
124119
}
125120

126-
func closeTunnel(t *Tunnel) {
121+
func closeTunnel(t *Tunnel, key string) {
127122
t.Mutex.Lock()
128123
t.Stop()
124+
Tunnels.Delete(key)
129125
t.Mutex.Unlock()
130-
131-
t.errHandler()
132126
}

internal/bridge/connection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func (t *TunnelPool) Set(remoteHost, remotePort, username string, tunnel *Tunnel
8282
func (t *TunnelPool) Delete(key string) {
8383
t.Lock()
8484
defer t.Unlock()
85-
delete(Tunnels.Connections, key)
85+
t.Connections[key] = nil
86+
delete(t.Connections, key)
8687
}
8788

8889
// VerifyAuth verifies key when connecting

0 commit comments

Comments
 (0)