From 734338ad70f909efedae36fcc0c4984855b423a5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 29 Nov 2019 11:05:42 +0300 Subject: [PATCH] network: move Version sending to the tcp transport We have a race between reader and writer goroutines for the same connection that leads to handshake failures when reader is faster to read the incoming version (and try to reply to it) than writer is to write our own Version: WARN[0000] peer disconnected addr="172.200.0.4:20334" peerCount=5 reason="invalid handshake: tried to send VersionAck, but didn't send Version yet Fix it by moving Version sending before the reader loop starts. --- pkg/network/server.go | 6 ------ pkg/network/tcp_transport.go | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index 549605bc1..97196ec9c 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -200,12 +200,6 @@ func (s *Server) run() { } return case p := <-s.register: - // When a new peer is connected we send out our version immediately. - if err := s.sendVersion(p); err != nil { - log.WithFields(log.Fields{ - "addr": p.RemoteAddr(), - }).Error(err) - } s.lock.Lock() s.peers[p] = true s.lock.Unlock() diff --git a/pkg/network/tcp_transport.go b/pkg/network/tcp_transport.go index 8dba0be40..654515758 100644 --- a/pkg/network/tcp_transport.go +++ b/pkg/network/tcp_transport.go @@ -78,6 +78,12 @@ func (t *TCPTransport) handleConn(conn net.Conn) { t.server.register <- p + // When a new peer is connected we send out our version immediately. + if err := t.server.sendVersion(p); err != nil { + log.WithFields(log.Fields{ + "addr": p.RemoteAddr(), + }).Error(err) + } r := io.NewBinReaderFromIO(p.conn) for { msg := &Message{}