From 22b038810aee12a0e4c647abb7ec1499c409c7b8 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Fri, 9 Feb 2018 21:58:02 +0400 Subject: [PATCH] do not block in recvRoutine --- p2p/conn/connection.go | 8 ++++---- p2p/conn/connection_test.go | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/p2p/conn/connection.go b/p2p/conn/connection.go index 938c3eb2c..46e363017 100644 --- a/p2p/conn/connection.go +++ b/p2p/conn/connection.go @@ -153,7 +153,7 @@ func NewMConnectionWithConfig(conn net.Conn, chDescs []*ChannelDescriptor, onRec sendMonitor: flow.New(0, 0), recvMonitor: flow.New(0, 0), send: make(chan struct{}, 1), - pong: make(chan struct{}), + pong: make(chan struct{}, 1), onReceive: onReceive, onError: onError, config: config, @@ -191,7 +191,7 @@ func (c *MConnection) OnStart() error { c.quit = make(chan struct{}) c.flushTimer = cmn.NewThrottleTimer("flush", c.config.FlushThrottle) c.pingTimer = cmn.NewRepeatTimer("ping", c.config.PingInterval) - c.pongTimeoutCh = make(chan bool) + c.pongTimeoutCh = make(chan bool, 1) c.chStatsTimer = cmn.NewRepeatTimer("chStats", updateStats) go c.sendRoutine() go c.recvRoutine() @@ -492,8 +492,8 @@ FOR_LOOP: c.Logger.Debug("Receive Pong") select { case c.pongTimeoutCh <- false: - case <-c.quit: - break FOR_LOOP + default: + // never block } case packetTypeMsg: pkt, n, err := msgPacket{}, int(0), error(nil) diff --git a/p2p/conn/connection_test.go b/p2p/conn/connection_test.go index 270b4ae92..d308ea61a 100644 --- a/p2p/conn/connection_test.go +++ b/p2p/conn/connection_test.go @@ -22,10 +22,10 @@ func createTestMConnection(conn net.Conn) *MConnection { } func createMConnectionWithCallbacks(conn net.Conn, onReceive func(chID byte, msgBytes []byte), onError func(r interface{})) *MConnection { - chDescs := []*ChannelDescriptor{&ChannelDescriptor{ID: 0x01, Priority: 1, SendQueueCapacity: 1}} cfg := DefaultMConnConfig() cfg.PingInterval = 90 * time.Millisecond cfg.PongTimeout = 45 * time.Millisecond + chDescs := []*ChannelDescriptor{&ChannelDescriptor{ID: 0x01, Priority: 1, SendQueueCapacity: 1}} c := NewMConnectionWithConfig(conn, chDescs, onReceive, onError, cfg) c.SetLogger(log.TestingLogger()) return c @@ -224,6 +224,7 @@ func TestMConnectionMultiplePings(t *testing.T) { defer mconn.Stop() // sending 3 pings in a row (abuse) + // see https://github.com/tendermint/tendermint/issues/1190 _, err = server.Write([]byte{packetTypePing}) require.Nil(t, err) _, err = server.Read(make([]byte, 1))