From e3098ed0f86feff0f695a83d574062ad4e7a4201 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 16 Nov 2019 12:42:03 +0300 Subject: [PATCH] network: write messages atomically Right now message can be written in several Write's so concurrent calls of writeMsg() can in theory interleave. This commit fixes it. Signed-off-by: Evgenii Stratonikov --- pkg/network/tcp_peer.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/network/tcp_peer.go b/pkg/network/tcp_peer.go index 074bf6016..78f10862b 100644 --- a/pkg/network/tcp_peer.go +++ b/pkg/network/tcp_peer.go @@ -63,8 +63,14 @@ func (p *TCPPeer) writeMsg(msg *Message) error { case err := <-p.done: return err default: - w := io.NewBinWriterFromIO(p.conn) - return msg.Encode(w) + w := io.NewBufBinWriter() + if err := msg.Encode(w.BinWriter); err != nil { + return err + } + + _, err := p.conn.Write(w.Bytes()) + + return err } }