Skip to content

Commit

Permalink
fix: Cleaner cannot delete objects from pool occuring memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed May 22, 2023
1 parent 96ef656 commit 5e07cc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
24 changes: 9 additions & 15 deletions internal/bridge/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ func Clean() {
}
}

for _, tunnel := range Tunnels.Connections {
if now.Sub(tunnel.LastConnection).Seconds() > 266 {
closeTunnel(tunnel)
continue
}

if tunnel.SshClient == nil {
for key, tunnel := range Tunnels.Connections {
if now.Sub(tunnel.LastConnection).Seconds() > 266 || tunnel.SshClient == nil {
closeTunnel(tunnel, key)
continue
}

Expand All @@ -81,7 +77,7 @@ func Clean() {
10*time.Second,
)
if err != nil {
closeTunnel(tunnel)
closeTunnel(tunnel, key)
continue
}

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

Expand All @@ -106,7 +102,7 @@ func Clean() {
case <-ch:
continue
case <-time.After(10 * time.Second):
closeTunnel(tunnel)
closeTunnel(tunnel, key)
continue
}
}
Expand All @@ -118,15 +114,13 @@ func Clean() {
func closeSession(s *Session, key string) {
s.Mutex.Lock()
s.CloseAllConnections()
s.Mutex.Unlock()

Sessions.Delete(key)
s.Mutex.Unlock()
}

func closeTunnel(t *Tunnel) {
func closeTunnel(t *Tunnel, key string) {
t.Mutex.Lock()
t.Stop()
Tunnels.Delete(key)
t.Mutex.Unlock()

t.errHandler()
}
3 changes: 2 additions & 1 deletion internal/bridge/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (t *TunnelPool) Set(remoteHost, remotePort, username string, tunnel *Tunnel
func (t *TunnelPool) Delete(key string) {
t.Lock()
defer t.Unlock()
delete(Tunnels.Connections, key)
t.Connections[key] = nil
delete(t.Connections, key)
}

// VerifyAuth verifies key when connecting
Expand Down

0 comments on commit 5e07cc5

Please sign in to comment.