From 16c4b0888510fe2467745d041f4e70625f4d3514 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 18 Aug 2021 16:23:04 +1000 Subject: [PATCH] Fix deadlock when checking whether to send keep alive --- peer-conn-msg-writer.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/peer-conn-msg-writer.go b/peer-conn-msg-writer.go index 16892546e7..dff4eb9ec8 100644 --- a/peer-conn-msg-writer.go +++ b/peer-conn-msg-writer.go @@ -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) }