Skip to content

Commit

Permalink
Fix deadlock when checking whether to send keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Aug 19, 2021
1 parent a3c2d76 commit 16c4b08
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion peer-conn-msg-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ func (cn *peerConnMsgWriter) run(keepAliveTimeout time.Duration) {
if cn.closed.IsSet() {
return
}
keepAlive := false
if cn.writeBuffer.Len() == 0 {
func() {
cn.mu.Unlock()
defer cn.mu.Lock()
cn.fillWriteBuffer()
keepAlive = cn.keepAlive()
}()
}
if cn.writeBuffer.Len() == 0 && time.Since(lastWrite) >= keepAliveTimeout && cn.keepAlive() {
if cn.writeBuffer.Len() == 0 && time.Since(lastWrite) >= keepAliveTimeout && keepAlive {
cn.writeBuffer.Write(pp.Message{Keepalive: true}.MustMarshalBinary())
torrent.Add("written keepalives", 1)
}
Expand Down

0 comments on commit 16c4b08

Please sign in to comment.