Skip to content

Commit

Permalink
fix: handle a BadgerDB panic related to incorrectly set permissions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovae committed Oct 14, 2021
1 parent 3df5d9d commit 840cf03
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"os"
"path/filepath"
"runtime/debug"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -65,10 +66,9 @@ type Storage struct {
wg sync.WaitGroup
}

func (s *Storage) newBadger(name string) (*badger.DB, error) {
func (s *Storage) newBadger(name string) (db *badger.DB, err error) {
badgerPath := filepath.Join(s.config.StoragePath, name)
err := os.MkdirAll(badgerPath, 0o755)
if err != nil {
if err = os.MkdirAll(badgerPath, 0o755); err != nil {
return nil, err
}
badgerOptions := badger.DefaultOptions(badgerPath)
Expand All @@ -81,7 +81,17 @@ func (s *Storage) newBadger(name string) (*badger.DB, error) {
badgerLevel = l
}
badgerOptions = badgerOptions.WithLogger(badgerLogger{name: name, logLevel: badgerLevel})
db, err := badger.Open(badgerOptions)
defer func() {
if r := recover(); r != nil {
// BadgerDB may panic because of file system access permissions. In particular,
// if is running in kubernetes with incorrect/unset fsGroup security context:
// https://github.com/pyroscope-io/pyroscope/issues/350.
err = fmt.Errorf("failed to open database\n\n"+
"Please make sure Pyroscope Server has write access permissions to %s directory.\n\n"+
"Recovered from panic: %v\n%v", badgerPath, r, string(debug.Stack()))
}
}()
db, err = badger.Open(badgerOptions)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 840cf03

Please sign in to comment.