Skip to content

Commit

Permalink
[FAB-4534] Use cancellation context in gossip
Browse files Browse the repository at this point in the history
In gossip when a peer p closes a connection to another peer q
that it initiated connection with, it simply closes the gRPC stream,
but this doesn't always make the Recv() method to return and might
result in a stuck goroutine.

The solution would be to use a cancellation context when priming the stream object.

Change-Id: I6632333b09559b70670787418788674452749f0e
Signed-off-by: Yacov Manevich <[email protected]>
  • Loading branch information
yacovm committed Jun 10, 2017
1 parent 4eb5815 commit 6928169
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gossip/comm/comm_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
return nil, err
}

if stream, err = cl.GossipStream(context.Background()); err == nil {
ctx, cf := context.WithCancel(context.Background())
if stream, err = cl.GossipStream(ctx); err == nil {
connInfo, err = c.authenticateRemotePeer(stream)
if err == nil {
pkiID = connInfo.ID
Expand All @@ -201,6 +202,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
conn.pkiID = pkiID
conn.info = connInfo
conn.logger = c.logger
conn.cancel = cf

h := func(m *proto.SignedGossipMessage) {
c.logger.Debug("Got message:", m)
Expand Down
6 changes: 6 additions & 0 deletions gossip/comm/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -206,6 +207,7 @@ func newConnection(cl proto.GossipClient, c *grpc.ClientConn, cs proto.Gossip_Go
}

type connection struct {
cancel context.CancelFunc
info *proto.ConnectionInfo
outBuff chan *msgSending
logger *logging.Logger // logger
Expand Down Expand Up @@ -241,6 +243,10 @@ func (conn *connection) close() {
conn.conn.Close()
}

if conn.cancel != nil {
conn.cancel()
}

conn.Unlock()

}
Expand Down

0 comments on commit 6928169

Please sign in to comment.