Skip to content

Commit

Permalink
Only add connection to ch.conns if it's active
Browse files Browse the repository at this point in the history
If a connection becomes active, and then closes almost immediately, then
it's possible that the close is handled before onActive is done
processing which leads to adding a inactive connection to ch.conns.

This causes the test TestTimeoutCallsThenClose to fail with state
mismatch since the number of connections has changed.

Check the connection state before adding it to the channel to avoid this
race.
  • Loading branch information
prashantv committed May 27, 2016
1 parent 72d010b commit dfef9e0
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions channel.go
Expand Up @@ -541,6 +541,10 @@ func (ch *Channel) addConnection(c *Connection, direction connectionDirection) b
ch.mutable.Lock()
defer ch.mutable.Unlock()

if c.readState() != connectionActive {
return false
}

switch state := ch.mutable.state; state {
case ChannelStartClose:
// Outbound connections are allowed in ChannelStartClose, but the
Expand Down Expand Up @@ -572,6 +576,7 @@ func (ch *Channel) connectionActive(c *Connection, direction connectionDirection
c.log.WithFields(
LogField{"remoteHostPort", c.remotePeerInfo.HostPort},
LogField{"direction", direction},
ErrField(err),
).Warn("Failed to add connection to peer")
}

Expand Down

0 comments on commit dfef9e0

Please sign in to comment.