From c06b4f2ff65cc894d088d7d8ee55063834f12bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 16 Jul 2021 17:44:54 +0100 Subject: [PATCH] make Close safe, not letting Finalize block forever either --- v2/blockstore/readonly.go | 11 +++++++++++ v2/blockstore/readwrite.go | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/v2/blockstore/readonly.go b/v2/blockstore/readonly.go index bbbba8f..34672ca 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 693e52f..665a653 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() -- GitLab