From 818c231b78a0657494fe171a21971d773eb71302 Mon Sep 17 00:00:00 2001 From: CJ DiMaggio Date: Tue, 29 Aug 2017 19:28:00 -0400 Subject: [PATCH 1/2] Clean up file descriptors on failed database initialization Fixes an issue where failing to open a non-existent database in ReadOnly mode would make you unable to properly initialize it in ReadWrite mode afterwards due to a hanging lock. --- db.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db.go b/db.go index d5c53f4a2..964974ec2 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 { From 3130a60889f6f9f0ea0a79d6e35fdc81380b6d98 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Sat, 17 Mar 2018 17:00:29 -0700 Subject: [PATCH 2/2] db.go: clean up file descriptor on db.file.ReadAt fail Signed-off-by: Gyuho Lee --- db.go | 1 + 1 file changed, 1 insertion(+) diff --git a/db.go b/db.go index 964974ec2..ab97c6014 100644 --- a/db.go +++ b/db.go @@ -239,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 } }