Skip to content

Commit

Permalink
make Close safe, not letting Finalize block forever either
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan authored and masih committed Jul 16, 2021
1 parent ae4ddd4 commit c06b4f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions v2/blockstore/readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
7 changes: 6 additions & 1 deletion v2/blockstore/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c06b4f2

Please sign in to comment.