Merge pull request #9602 from filecoin-project/steb/re-check-backing-store

fix: autobatch: remove potential deadlock when a block is missing
This commit is contained in:
Aayush Rajasekaran 2022-11-07 20:16:35 +00:00 committed by GitHub
commit 0fe4fad72c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -181,18 +181,22 @@ func (bs *AutobatchBlockstore) Get(ctx context.Context, c cid.Cid) (block.Block,
} }
bs.stateLock.Lock() bs.stateLock.Lock()
defer bs.stateLock.Unlock()
v, ok := bs.flushingBatch.blockMap[c] v, ok := bs.flushingBatch.blockMap[c]
if ok { if ok {
bs.stateLock.Unlock()
return v, nil return v, nil
} }
v, ok = bs.bufferedBatch.blockMap[c] v, ok = bs.bufferedBatch.blockMap[c]
if ok { if ok {
bs.stateLock.Unlock()
return v, nil return v, nil
} }
bs.stateLock.Unlock()
return bs.Get(ctx, c) // We have to check the backing store one more time because it may have been flushed by the
// time we were able to take the lock above.
return bs.backingBs.Get(ctx, c)
} }
func (bs *AutobatchBlockstore) DeleteBlock(context.Context, cid.Cid) error { func (bs *AutobatchBlockstore) DeleteBlock(context.Context, cid.Cid) error {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"testing" "testing"
ipld "github.com/ipfs/go-ipld-format"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -29,6 +30,10 @@ func TestAutobatchBlockstore(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, b2.RawData(), v2.RawData()) require.Equal(t, b2.RawData(), v2.RawData())
// Regression test for a deadlock.
_, err = ab.Get(ctx, b3.Cid())
require.True(t, ipld.IsNotFound(err))
require.NoError(t, ab.Flush(ctx)) require.NoError(t, ab.Flush(ctx))
require.NoError(t, ab.Shutdown(ctx)) require.NoError(t, ab.Shutdown(ctx))
} }

View File

@ -13,6 +13,7 @@ var (
b0 = blocks.NewBlock([]byte("abc")) b0 = blocks.NewBlock([]byte("abc"))
b1 = blocks.NewBlock([]byte("foo")) b1 = blocks.NewBlock([]byte("foo"))
b2 = blocks.NewBlock([]byte("bar")) b2 = blocks.NewBlock([]byte("bar"))
b3 = blocks.NewBlock([]byte("baz"))
) )
func TestUnionBlockstore_Get(t *testing.T) { func TestUnionBlockstore_Get(t *testing.T) {