Skip to content

Commit

Permalink
[FAB-9124] Fix race in nextBlock
Browse files Browse the repository at this point in the history
Send result over chan instead of using shared, named return value.

Change-Id: I3c5edbdbd792f7787a21f6de7f2fde2df5ffd9b5
Signed-off-by: Matthew Sykes <[email protected]>
  • Loading branch information
sykesm committed Apr 13, 2018
1 parent 265a43f commit 708c493
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions common/deliver/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,21 @@ func (h *Handler) validateChannelHeader(ctx context.Context, chdr *cb.ChannelHea
return nil
}

func nextBlock(cursor blockledger.Iterator, cancel <-chan struct{}) (block *cb.Block, status cb.Status) {
done := make(chan struct{})
func nextBlock(cursor blockledger.Iterator, cancel <-chan struct{}) (*cb.Block, cb.Status) {
type result struct {
block *cb.Block
status cb.Status
}

resultCh := make(chan *result, 1)
go func() {
defer close(done)
block, status = cursor.Next()
block, status := cursor.Next()
resultCh <- &result{block, status}
}()

select {
case <-done:
return
case r := <-resultCh:
return r.block, r.status
case <-cancel:
logger.Warningf("Aborting deliver for request because of background error")
return nil, cb.Status_SERVICE_UNAVAILABLE
Expand Down

0 comments on commit 708c493

Please sign in to comment.