core: close BoltDB on failed root bucket creation

This commit is contained in:
Anna Shaleva 2022-10-10 09:20:11 +03:00
parent 343ce4d4bb
commit 63fddb3f1a
2 changed files with 9 additions and 6 deletions

View file

@ -56,7 +56,12 @@ func NewBoltDBStore(cfg dbconfig.BoltDBOptions) (*BoltDBStore, error) {
})
}
if err != nil {
return nil, fmt.Errorf("failed to initialize BoltDB instance: %w", err)
closeErr := db.Close()
err = fmt.Errorf("failed to initialize BoltDB instance: %w", err)
if closeErr != nil {
err = fmt.Errorf("%w, failed to close BoltDB instance: %v", err, closeErr)
}
return nil, err
}
return &BoltDBStore{db: db}, nil

View file

@ -7,7 +7,6 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
@ -47,10 +46,9 @@ func TestROBoltDB(t *testing.T) {
require.NoError(t, store.Close())
// Create the DB without buckets and try to open it in RO mode, an error is expected.
fileMode := os.FileMode(0600)
cfg.FilePath = filepath.Join(d, "clean_ro_bolt_db")
require.NoError(t, io.MakeDirForFile(cfg.FilePath, "BoltDB"))
db, err := bbolt.Open(cfg.FilePath, fileMode, nil)
tmp := t.TempDir()
cfg.FilePath = filepath.Join(tmp, "clean_ro_bolt")
db, err := bbolt.Open(cfg.FilePath, os.FileMode(0600), nil)
require.NoError(t, err)
require.NoError(t, db.Close())