Skip to content

Commit acfee68

Browse files
mvdanmasih
authored andcommitted
make Close safe, not letting Finalize block forever either
This commit was moved from ipld/go-car@c06b4f2
1 parent 9780e1f commit acfee68

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

ipld/car/v2/blockstore/readonly.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,18 @@ func (b *ReadOnly) Roots() ([]cid.Cid, error) {
330330
}
331331

332332
// Close closes the underlying reader if it was opened by OpenReadOnly.
333+
//
334+
// Note that this call may block if any blockstore operations are currently in
335+
// progress, including an AllKeysChan that hasn't been fully consumed or
336+
// cancelled.
333337
func (b *ReadOnly) Close() error {
338+
b.mu.Lock()
339+
defer b.mu.Unlock()
340+
341+
return b.closeWithoutMutex()
342+
}
343+
344+
func (b *ReadOnly) closeWithoutMutex() error {
334345
if b.carv2Closer != nil {
335346
return b.carv2Closer.Close()
336347
}

ipld/car/v2/blockstore/readwrite.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,12 @@ func (b *ReadWrite) Finalize() error {
343343
defer b.mu.Unlock()
344344
// TODO check if add index option is set and don't write the index then set index offset to zero.
345345
b.header = b.header.WithDataSize(uint64(b.dataWriter.Position()))
346-
defer b.Close()
346+
347+
// Note that we can't use b.Close here, as that tries to grab the same
348+
// mutex we're holding here.
349+
// TODO: should we check the error here? especially with OpenReadWrite,
350+
// we should care about close errors.
351+
defer b.closeWithoutMutex()
347352

348353
// TODO if index not needed don't bother flattening it.
349354
fi, err := b.idx.flatten()

0 commit comments

Comments
 (0)