Skip to content

Commit b5f3790

Browse files
committed
[FAB-4533] Use context cancellations in deliveryservice
This addresses FAB-4503 and FAB-4489 The reason for the CI failures is that apparently, closing the underlying gRPC connection doesn't always make Recv() return. The solution is to use context.WithCancel and use the cancel function when Disconnect or Close are called and not only close the gRPC stream. Change-Id: Ia255467345dc6178764a2eb4f1e51167b914eebd Signed-off-by: Yacov Manevich <[email protected]>
1 parent 4eb5815 commit b5f3790

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

core/deliverservice/client.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ func (bc *broadcastClient) connect() error {
139139
logger.Error("Failed obtaining connection:", err)
140140
return err
141141
}
142-
abc, err := bc.createClient(conn).Deliver(context.Background())
142+
ctx, cf := context.WithCancel(context.Background())
143+
abc, err := bc.createClient(conn).Deliver(ctx)
143144
if err != nil {
144145
logger.Error("Connection to ", endpoint, "established but was unable to create gRPC stream:", err)
145146
conn.Close()
146147
return err
147148
}
148-
err = bc.afterConnect(conn, abc)
149+
err = bc.afterConnect(conn, abc, cf)
149150
if err == nil {
150151
return nil
151152
}
@@ -155,9 +156,9 @@ func (bc *broadcastClient) connect() error {
155156
return err
156157
}
157158

158-
func (bc *broadcastClient) afterConnect(conn *grpc.ClientConn, abc orderer.AtomicBroadcast_DeliverClient) error {
159+
func (bc *broadcastClient) afterConnect(conn *grpc.ClientConn, abc orderer.AtomicBroadcast_DeliverClient, cf context.CancelFunc) error {
159160
bc.Lock()
160-
bc.conn = &connection{ClientConn: conn}
161+
bc.conn = &connection{ClientConn: conn, cancel: cf}
161162
bc.BlocksDeliverer = abc
162163
if bc.shouldStop() {
163164
bc.Unlock()
@@ -219,13 +220,15 @@ func (bc *broadcastClient) Disconnect() {
219220
}
220221

221222
type connection struct {
222-
*grpc.ClientConn
223223
sync.Once
224+
*grpc.ClientConn
225+
cancel context.CancelFunc
224226
}
225227

226228
func (c *connection) Close() error {
227229
var err error
228230
c.Once.Do(func() {
231+
c.cancel()
229232
err = c.ClientConn.Close()
230233
})
231234
return err

core/deliverservice/client_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ func testGreenPath(t *testing.T, bdc blocksDelivererConsumer) {
471471
}
472472

473473
func TestCloseWhileRecv(t *testing.T) {
474-
t.Skip()
475474
// Scenario: Recv is being called and after a while,
476475
// the connection is closed.
477476
// The Recv should return immediately in such a case
@@ -600,7 +599,6 @@ func TestProductionUsage(t *testing.T) {
600599
}
601600

602601
func TestDisconnect(t *testing.T) {
603-
t.Skip()
604602
// Scenario: spawn 2 ordering service instances
605603
// and a client.
606604
// Have the client try to Recv() from one of them,

0 commit comments

Comments
 (0)