Skip to content

Commit

Permalink
prevent panic after mmap failure
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Sep 29, 2017
1 parent 3eac9d3 commit d38f16c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 8 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
},
}

// Memory map the data file.
if err := db.mmap(options.InitialMmapSize); err != nil {
_ = db.close()
return nil, err
// Memory-map the data file as a byte slice.
if err := mmap(db, size); err != nil {
// mmap failed; the system may have run out of space. Fallback to
// mapping the bare minimum needed for the current db size.
if err2 := mmap(db, db.datasz); err2 != nil {
panic(fmt.Sprintf("failed to revert db size after failed mmap: %v", err2))
}
return MmapError(err.Error())
}

if db.readOnly {
Expand Down
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ var (
// non-bucket key on an existing bucket key.
ErrIncompatibleValue = errors.New("incompatible value")
)

// MmapError represents an error resulting from a failed mmap call. Typically,
// this error means that no further database writes will be possible. The most
// common cause is insufficient disk space.
type MmapError string

func (e MmapError) Error() string { return string(e) }

0 comments on commit d38f16c

Please sign in to comment.