Skip to content

Commit

Permalink
fix deadlock condition in onHeartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobsonchase committed Jan 4, 2024
1 parent b8838a3 commit 642ca9c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/tunnel/client/raw_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,26 @@ func (s *rawSession) rpc(reqtype proto.ReqType, req any, resp any) error {
}

func (s *rawSession) onHeartbeat(pingTime time.Duration, timeout bool) {
if timeout {
s.Error("heartbeat timeout, terminating session")
s.Close()
return
}

// make sure we don't send on a closed channel.
// Any number of `onHeartbeat` callbacks can be in flight at a given time,
// but only one Close.
s.closedLock.RLock()
defer s.closedLock.RUnlock()

if s.closed {
return
}

if timeout {
s.Error("heartbeat timeout, terminating session")
s.Close()
} else {
s.Debug("heartbeat received", "latency_ms", int(pingTime.Milliseconds()))
select {
case s.latency <- pingTime:
default:
}
s.Debug("heartbeat received", "latency_ms", int(pingTime.Milliseconds()))
select {
case s.latency <- pingTime:
default:
}
}

Expand Down

0 comments on commit 642ca9c

Please sign in to comment.