Merge pull request #1151 from tendermint/fix/p2p-stop-conn

p2p/conn: fix blocking on pong during quit and break out of loops
This commit is contained in:
Ethan Buchman 2018-01-25 02:45:06 -05:00 committed by GitHub
commit 3366dfe32a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -189,11 +189,11 @@ func (c *MConnection) OnStop() {
close(c.quit) close(c.quit)
} }
c.conn.Close() // nolint: errcheck c.conn.Close() // nolint: errcheck
// We can't close pong safely here because // We can't close pong safely here because
// recvRoutine may write to it after we've stopped. // recvRoutine may write to it after we've stopped.
// Though it doesn't need to get closed at all, // Though it doesn't need to get closed at all,
// we close it @ recvRoutine. // we close it @ recvRoutine.
// close(c.pong)
} }
func (c *MConnection) String() string { func (c *MConnection) String() string {
@ -450,7 +450,11 @@ FOR_LOOP:
case packetTypePing: case packetTypePing:
// TODO: prevent abuse, as they cause flush()'s. // TODO: prevent abuse, as they cause flush()'s.
c.Logger.Debug("Receive Ping") c.Logger.Debug("Receive Ping")
c.pong <- struct{}{} select {
case c.pong <- struct{}{}:
case <-c.quit:
break FOR_LOOP
}
case packetTypePong: case packetTypePong:
// do nothing // do nothing
c.Logger.Debug("Receive Pong") c.Logger.Debug("Receive Pong")
@ -470,6 +474,7 @@ FOR_LOOP:
err := fmt.Errorf("Unknown channel %X", pkt.ChannelID) err := fmt.Errorf("Unknown channel %X", pkt.ChannelID)
c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err) c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err)
c.stopForError(err) c.stopForError(err)
break FOR_LOOP
} }
msgBytes, err := channel.recvMsgPacket(pkt) msgBytes, err := channel.recvMsgPacket(pkt)
@ -489,6 +494,7 @@ FOR_LOOP:
err := fmt.Errorf("Unknown message type %X", pktType) err := fmt.Errorf("Unknown message type %X", pktType)
c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err) c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err)
c.stopForError(err) c.stopForError(err)
break FOR_LOOP
} }
// TODO: shouldn't this go in the sendRoutine? // TODO: shouldn't this go in the sendRoutine?