Commit c06b4f2f authored by Daniel Martí's avatar Daniel Martí Committed by Masih H. Derkani

make Close safe, not letting Finalize block forever either

parent ae4ddd41
......@@ -330,7 +330,18 @@ func (b *ReadOnly) Roots() ([]cid.Cid, error) {
}
// Close closes the underlying reader if it was opened by OpenReadOnly.
//
// Note that this call may block if any blockstore operations are currently in
// progress, including an AllKeysChan that hasn't been fully consumed or
// cancelled.
func (b *ReadOnly) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
return b.closeWithoutMutex()
}
func (b *ReadOnly) closeWithoutMutex() error {
if b.carv2Closer != nil {
return b.carv2Closer.Close()
}
......
......@@ -343,7 +343,12 @@ func (b *ReadWrite) Finalize() error {
defer b.mu.Unlock()
// TODO check if add index option is set and don't write the index then set index offset to zero.
b.header = b.header.WithDataSize(uint64(b.dataWriter.Position()))
defer b.Close()
// Note that we can't use b.Close here, as that tries to grab the same
// mutex we're holding here.
// TODO: should we check the error here? especially with OpenReadWrite,
// we should care about close errors.
defer b.closeWithoutMutex()
// TODO if index not needed don't bother flattening it.
fi, err := b.idx.flatten()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment