diff --git a/v2/blockstore/readonly.go b/v2/blockstore/readonly.go index bbbba8f4e1ec17e7804d5bff1aa5910d138a70c5..34672cad77fc0f51c58fcec6b7c05e102572f1df 100644 --- a/v2/blockstore/readonly.go +++ b/v2/blockstore/readonly.go @@ -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() } diff --git a/v2/blockstore/readwrite.go b/v2/blockstore/readwrite.go index 693e52f9134597d115d7daa3558b0738f578cb88..665a65384e9555910bc64490dc22ad3898891fc3 100644 --- a/v2/blockstore/readwrite.go +++ b/v2/blockstore/readwrite.go @@ -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()