Skip to content

Commit 6928169

Browse files
committed
[FAB-4534] Use cancellation context in gossip
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]>
1 parent 4eb5815 commit 6928169

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

gossip/comm/comm_impl.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
187187
return nil, err
188188
}
189189

190-
if stream, err = cl.GossipStream(context.Background()); err == nil {
190+
ctx, cf := context.WithCancel(context.Background())
191+
if stream, err = cl.GossipStream(ctx); err == nil {
191192
connInfo, err = c.authenticateRemotePeer(stream)
192193
if err == nil {
193194
pkiID = connInfo.ID
@@ -201,6 +202,7 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
201202
conn.pkiID = pkiID
202203
conn.info = connInfo
203204
conn.logger = c.logger
205+
conn.cancel = cf
204206

205207
h := func(m *proto.SignedGossipMessage) {
206208
c.logger.Debug("Got message:", m)

gossip/comm/conn.go

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hyperledger/fabric/gossip/util"
2626
proto "github.com/hyperledger/fabric/protos/gossip"
2727
"github.com/op/go-logging"
28+
"golang.org/x/net/context"
2829
"google.golang.org/grpc"
2930
)
3031

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

208209
type connection struct {
210+
cancel context.CancelFunc
209211
info *proto.ConnectionInfo
210212
outBuff chan *msgSending
211213
logger *logging.Logger // logger
@@ -241,6 +243,10 @@ func (conn *connection) close() {
241243
conn.conn.Close()
242244
}
243245

246+
if conn.cancel != nil {
247+
conn.cancel()
248+
}
249+
244250
conn.Unlock()
245251

246252
}

0 commit comments

Comments
 (0)