Skip to content

Commit f478fa5

Browse files
Saad Karimsykesm
Saad Karim
authored andcommitted
[FAB-9127] Address race in blockledger/json
Data race between reading and updating the signal channel. Change-Id: Iee8818b1298b5583b3f79e63045072416cdfd64d Signed-off-by: Saad Karim <skarim@us.ibm.com> Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent e8cc36f commit f478fa5

File tree

1 file changed

+16
-2
lines changed
  • common/ledger/blockledger/json

1 file changed

+16
-2
lines changed

common/ledger/blockledger/json/impl.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ type cursor struct {
5757
type jsonLedger struct {
5858
directory string
5959
height uint64
60-
signal chan struct{}
6160
lastHash []byte
6261
marshaler *jsonpb.Marshaler
62+
63+
mutex sync.Mutex
64+
signal chan struct{}
6365
}
6466

6567
// readBlock returns the block or nil, and whether the block was found or not, (nil,true) generally indicates an irrecoverable problem
@@ -99,13 +101,21 @@ func (cu *cursor) Next() (*cb.Block, cb.Status) {
99101
cu.blockNumber++
100102
return block, cb.Status_SUCCESS
101103
}
102-
<-cu.jl.signal
104+
105+
// copy the signal channel under lock to avoid race
106+
// with new signal channel in append
107+
cu.jl.mutex.Lock()
108+
signal := cu.jl.signal
109+
cu.jl.mutex.Unlock()
110+
<-signal
103111
}
104112
}
105113

106114
// ReadyChan supplies a channel which will block until Next will not block
107115
func (cu *cursor) ReadyChan() <-chan struct{} {
116+
cu.jl.mutex.Lock()
108117
signal := cu.jl.signal
118+
cu.jl.mutex.Unlock()
109119
if _, err := os.Stat(cu.jl.blockFilename(cu.blockNumber)); os.IsNotExist(err) {
110120
return signal
111121
}
@@ -151,8 +161,12 @@ func (jl *jsonLedger) Append(block *cb.Block) error {
151161
jl.writeBlock(block)
152162
jl.lastHash = block.Header.Hash()
153163
jl.height++
164+
165+
// Manage the signal channel under lock to avoid race with read in Next
166+
jl.mutex.Lock()
154167
close(jl.signal)
155168
jl.signal = make(chan struct{})
169+
jl.mutex.Unlock()
156170
return nil
157171
}
158172

0 commit comments

Comments
 (0)