diff --git a/db.go b/db.go index d5c53f4a2..ab97c6014 100644 --- a/db.go +++ b/db.go @@ -213,10 +213,13 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { // Initialize the database if it doesn't exist. if info, err := db.file.Stat(); err != nil { + _ = db.close() return nil, err } else if info.Size() == 0 { // Initialize new files with meta pages. if err := db.init(); err != nil { + // clean up file descriptor on initialization fail + _ = db.close() return nil, err } } else { @@ -236,6 +239,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { db.pageSize = int(m.pageSize) } } else { + _ = db.close() return nil, ErrInvalid } }