Skip to content

Commit

Permalink
Backport perform unmap when failing to mlock or both meta pages corru…
Browse files Browse the repository at this point in the history
…pted.

Signed-off-by: James Blair <mail@jamesblair.net>
  • Loading branch information
jmhbnz committed Mar 30, 2023
1 parent ad36005 commit 7a13798
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (db *DB) hasSyncedFreelist() bool {

// mmap opens the underlying memory-mapped file and initializes the meta references.
// minsz is the minimum size that the new mmap can be.
func (db *DB) mmap(minsz int) error {
func (db *DB) mmap(minsz int) (err error) {
db.mmaplock.Lock()
defer db.mmaplock.Unlock()

Expand Down Expand Up @@ -459,17 +459,27 @@ func (db *DB) mmap(minsz int) error {
}

// Unmap existing data before continuing.
if err := db.munmap(); err != nil {
if err = db.munmap(); err != nil {
return err
}

// Memory-map the data file as a byte slice.
// gofail: var mapError string
// return errors.New(mapError)
if err := mmap(db, size); err != nil {
if err = mmap(db, size); err != nil {
return err
}

// Perform unmmap on any error to reset all data fields:
// dataref, data, datasz, meta0 and meta1.
defer func() {
if err != nil {
if unmapErr := db.munmap(); unmapErr != nil {
err = fmt.Errorf("%w; rollback unmap also failed: %v", err, unmapErr)
}
}
}()

if db.Mlock {
// Don't allow swapping of data file
if err := db.mlock(fileSize); err != nil {
Expand Down Expand Up @@ -553,13 +563,17 @@ func (db *DB) mmapSize(size int) (int, error) {
}

func (db *DB) munlock(fileSize int) error {
// gofail: var munlockError string
// return errors.New(munlockError)
if err := munlock(db, fileSize); err != nil {
return fmt.Errorf("munlock error: " + err.Error())
}
return nil
}

func (db *DB) mlock(fileSize int) error {
// gofail: var mlockError string
// return errors.New(mlockError)
if err := mlock(db, fileSize); err != nil {
return fmt.Errorf("mlock error: " + err.Error())
}
Expand Down

0 comments on commit 7a13798

Please sign in to comment.