Skip to content

Commit

Permalink
Merge pull request #469 from gravitational/ev/rt
Browse files Browse the repository at this point in the history
Goroutine leak when closing reverse tunnels
  • Loading branch information
klizhentas authored Jun 30, 2016
2 parents efd19db + 8e1acd1 commit dc36f30
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/reversetunnel/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func (a *Agent) proxyTransport(ch ssh.Channel, reqC <-chan *ssh.Request) {
// to the given SSH connection.
//
func (a *Agent) runHeartbeat(conn *ssh.Client) {
ticker := time.NewTicker(defaults.ReverseTunnelAgentHeartbeatPeriod)
defer ticker.Stop()

heartbeatLoop := func() error {
if conn == nil {
return trace.Errorf("heartbeat cannot ping: need to reconnect")
Expand All @@ -258,8 +261,6 @@ func (a *Agent) runHeartbeat(conn *ssh.Client) {

// send first ping right away, then start a ping timer:
hb.SendRequest("ping", false, nil)
ticker := time.NewTicker(defaults.ReverseTunnelAgentHeartbeatPeriod)
defer ticker.Stop()

for {
select {
Expand Down Expand Up @@ -307,10 +308,18 @@ func (a *Agent) runHeartbeat(conn *ssh.Client) {
}
}

// run heartbeat loop, and when it fails (probably means that a tunnel got disconnected)
// keep repeating to reconnect until we're asked to stop
err := heartbeatLoop()
if err != nil || conn == nil {
time.Sleep(defaults.ReverseTunnelAgentHeartbeatPeriod)
a.Start()
select {
// abort if asked to stop:
case <-a.broadcastClose.C:
return
// reconnect
case <-ticker.C:
a.Start()
}
}
}

Expand Down

0 comments on commit dc36f30

Please sign in to comment.