Fix deadlock when checking whether to send keep alive

This commit is contained in:
Matt Joiner 2021-08-18 16:23:04 +10:00
parent a3c2d76b4a
commit 16c4b08885
1 changed files with 3 additions and 1 deletions

View File

@ -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)
}