Skip to content

Commit 8e868b8

Browse files
committed
fix DeliverService stop
Currently when peer stopes, DeliverService.stop will be blocked. So you can't use "ctrl+c" or "kill" to interrupt or stop peer. Because DeliverService use a unbuffered channel stopChan to send stop signal. When peer is a gossip.orgLeader, DeliverService don't receive from stopChan. So DeliverService.stop will block at "d.stopChan <- true". Fix the block bug and use a atomic flag to distinguash unexpected connection error and initiative stop. Change-Id: If2afd226c5b074e3b78157d84e2f267e741208aa Signed-off-by: jiangyaoguo <[email protected]>
1 parent 2c22539 commit 8e868b8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

core/committer/noopssinglechain/client.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package noopssinglechain
1818

1919
import (
20+
"sync/atomic"
2021
"time"
2122

2223
"github.com/golang/protobuf/proto"
@@ -60,6 +61,7 @@ type DeliverService struct {
6061
gossip gossip.Gossip
6162
conn *grpc.ClientConn
6263

64+
stopFlag int32
6365
stopChan chan bool
6466
}
6567

@@ -172,6 +174,8 @@ func (d *DeliverService) Start() {
172174

173175
// Stop all service and release resources
174176
func (d *DeliverService) Stop() {
177+
atomic.StoreInt32(&d.stopFlag, 1)
178+
d.stopDeliver()
175179
d.stopChan <- true
176180
d.stateProvider.Stop()
177181
d.gossip.Stop()
@@ -211,13 +215,23 @@ func (d *DeliverService) seekLatestFromCommitter(height uint64) error {
211215
})
212216
}
213217

218+
// Internal function to check whenever we need to finish listening
219+
// for new messages to arrive
220+
func (d *DeliverService) isDone() bool {
221+
222+
return atomic.LoadInt32(&d.stopFlag) == 1
223+
}
224+
214225
func (d *DeliverService) readUntilClose() {
215226
for {
216227
msg, err := d.client.Recv()
217228
if err != nil {
229+
logger.Warningf("Receive error: %s", err.Error())
230+
if d.isDone() {
231+
<-d.stopChan
232+
}
218233
return
219234
}
220-
221235
switch t := msg.Type.(type) {
222236
case *orderer.DeliverResponse_Error:
223237
if t.Error == common.Status_SUCCESS {

0 commit comments

Comments
 (0)