piecereader: Fix parallel buffer access, close readers in readInto

This commit is contained in:
Łukasz Magiera 2023-05-19 18:47:51 +02:00
parent cd75ea0fe4
commit 587c3fde58
2 changed files with 12 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package sealer
import ( import (
"bufio" "bufio"
"context" "context"
pool "github.com/libp2p/go-buffer-pool"
"io" "io"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
@ -93,8 +94,6 @@ func (p *pieceProvider) tryReadUnsealedPiece(ctx context.Context, pc cid.Cid, se
return nil, nil return nil, nil
} }
buf := make([]byte, fr32.BufSize(pieceSize.Padded()))
pr, err := (&pieceReader{ pr, err := (&pieceReader{
ctx: ctx, ctx: ctx,
getReader: func(ctx context.Context, startOffset, readSize uint64) (io.ReadCloser, error) { getReader: func(ctx context.Context, startOffset, readSize uint64) (io.ReadCloser, error) {
@ -109,6 +108,8 @@ func (p *pieceProvider) tryReadUnsealedPiece(ctx context.Context, pc cid.Cid, se
return nil, xerrors.Errorf("getting reader at +%d: %w", startOffsetAligned, err) return nil, xerrors.Errorf("getting reader at +%d: %w", startOffsetAligned, err)
} }
buf := pool.Get(fr32.BufSize(pieceSize.Padded()))
upr, err := fr32.NewUnpadReaderBuf(r, pieceSize.Padded(), buf) upr, err := fr32.NewUnpadReaderBuf(r, pieceSize.Padded(), buf)
if err != nil { if err != nil {
r.Close() // nolint r.Close() // nolint
@ -129,6 +130,7 @@ func (p *pieceProvider) tryReadUnsealedPiece(ctx context.Context, pc cid.Cid, se
}{ }{
Reader: bir, Reader: bir,
Closer: funcCloser(func() error { Closer: funcCloser(func() error {
pool.Put(buf)
return r.Close() return r.Close()
}), }),
}, nil }, nil

View File

@ -276,11 +276,18 @@ func (p *pieceReader) readInto(b []byte, off int64) (n int, err error) {
} }
n, err = io.ReadFull(rd, b) n, err = io.ReadFull(rd, b)
cerr := rd.Close()
if err == io.ErrUnexpectedEOF { if err == io.ErrUnexpectedEOF {
err = io.EOF err = io.EOF
} }
if err != nil {
return n, err return n, err
}
return n, cerr
} }
var _ mount.Reader = (*pieceReader)(nil) var _ mount.Reader = (*pieceReader)(nil)