piecereader: Fix double buffer free

This commit is contained in:
Łukasz Magiera 2023-05-23 16:45:46 +02:00
parent f0fec13ec9
commit 5e58f64380

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"io" "io"
"sync"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
pool "github.com/libp2p/go-buffer-pool" pool "github.com/libp2p/go-buffer-pool"
@ -124,13 +125,19 @@ func (p *pieceProvider) tryReadUnsealedPiece(ctx context.Context, pc cid.Cid, se
} }
} }
var closeOnce sync.Once
return struct { return struct {
io.Reader io.Reader
io.Closer io.Closer
}{ }{
Reader: bir, Reader: bir,
Closer: funcCloser(func() error { Closer: funcCloser(func() error {
// this close can be called more than once - double close signals to the paths.Reader that we are done with the piece
closeOnce.Do(func() {
pool.Put(buf) pool.Put(buf)
})
return r.Close() return r.Close()
}), }),
}, nil }, nil