-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle badger iterator panics on app stop #1355
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package objectstore | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dgraph-io/badger/v4" | ||
|
||
"github.com/anyproto/anytype-heart/util/badgerhelper" | ||
|
@@ -35,6 +37,12 @@ func iterateKeysByPrefixBatchedTx( | |
batchSize int, | ||
processKeysFn func(keys [][]byte) error, | ||
) error { | ||
var err error | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("badger iterator panic: %v", r) | ||
} | ||
}() | ||
opts := badger.DefaultIteratorOptions | ||
opts.PrefetchValues = false | ||
opts.Prefix = prefix | ||
|
@@ -50,7 +58,7 @@ func iterateKeysByPrefixBatchedTx( | |
count++ | ||
|
||
if count == batchSize { | ||
err := processKeysFn(batch) | ||
err = processKeysFn(batch) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -60,7 +68,7 @@ func iterateKeysByPrefixBatchedTx( | |
} | ||
|
||
if count > 0 { | ||
err := processKeysFn(batch) | ||
err = processKeysFn(batch) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -70,6 +78,12 @@ func iterateKeysByPrefixBatchedTx( | |
} | ||
|
||
func iterateKeysByPrefixTx(txn *badger.Txn, prefix []byte, processKeyFn func(key []byte)) error { | ||
var err error | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("badger iterator panic: %v", r) | ||
} | ||
}() | ||
Comment on lines
+81
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure The defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("badger iterator panic: %v", r)
+ return
}
}()
|
||
opts := badger.DefaultIteratorOptions | ||
opts.PrefetchValues = false | ||
opts.Prefix = prefix | ||
|
@@ -80,5 +94,5 @@ func iterateKeysByPrefixTx(txn *badger.Txn, prefix []byte, processKeyFn func(key | |
key := iter.Item().KeyCopy(nil) | ||
processKeyFn(key) | ||
} | ||
return nil | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -93,9 +93,16 @@ func (s *dsObjectStore) getInjectedResults(details *types.Struct, score float64, | |||||||||||||||||||||||||||||||||||||
func (s *dsObjectStore) queryRaw(filter func(g *types.Struct) bool, order database.Order, limit int, offset int) ([]database.Record, error) { | ||||||||||||||||||||||||||||||||||||||
var ( | ||||||||||||||||||||||||||||||||||||||
records []database.Record | ||||||||||||||||||||||||||||||||||||||
err error | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
err := s.db.View(func(txn *badger.Txn) error { | ||||||||||||||||||||||||||||||||||||||
defer func() { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use keyed fields in struct literals. The - optionFilter := database.FilterAllIn{relationKey, listValue.GetListValue()}
+ optionFilter := database.FilterAllIn{
+ Key: relationKey,
+ Value: listValue.GetListValue(),
+} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
if r := recover(); r != nil { | ||||||||||||||||||||||||||||||||||||||
err = fmt.Errorf("badger iterator panic: %v", r) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
}() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Comment on lines
+96
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure The defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("badger iterator panic: %v", r)
+ return
}
}() Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
err = s.db.View(func(txn *badger.Txn) error { | ||||||||||||||||||||||||||||||||||||||
opts := badger.DefaultIteratorOptions | ||||||||||||||||||||||||||||||||||||||
opts.PrefetchValues = false | ||||||||||||||||||||||||||||||||||||||
opts.Prefix = pagesDetailsBase.Bytes() | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -238,6 +238,11 @@ func GetKeysByIndex(index Index, txn *badger.Txn, val interface{}, limit int) ([ | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
func GetKeys(txn *badger.Txn, prefix string, limit int) []string { | ||||||||||||||||||||||||||
defer func() { | ||||||||||||||||||||||||||
if r := recover(); r != nil { | ||||||||||||||||||||||||||
log.Errorf("badger iterator panic: %v", r) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
}() | ||||||||||||||||||||||||||
Comment on lines
+241
to
+245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure the panic handling is effective in The defer func() {
if r := recover(); r != nil {
log.Errorf("badger iterator panic: %v", r)
+ err = fmt.Errorf("badger iterator panic: %v", r)
+ return
}
}() Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
iter := txn.NewIterator(badger.IteratorOptions{ | ||||||||||||||||||||||||||
Prefix: []byte(prefix), | ||||||||||||||||||||||||||
PrefetchValues: false, | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,13 @@ func NewBadgerStorage[T Item](db *badger.DB, badgerPrefix []byte, factoryFunc Fa | |
|
||
func (s *badgerStorage[T]) List() ([]T, error) { | ||
var items []T | ||
err := s.db.View(func(txn *badger.Txn) error { | ||
var err error | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("badger iterator panic: %v", r) | ||
} | ||
}() | ||
Comment on lines
+37
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure The defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("badger iterator panic: %v", r)
+ return
}
}()
|
||
err = s.db.View(func(txn *badger.Txn) error { | ||
it := txn.NewIterator(badger.IteratorOptions{ | ||
PrefetchSize: 100, | ||
PrefetchValues: true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure
err
is returned if a panic occurs initerateKeysByPrefixBatchedTx
.The
defer
function correctly captures and wraps the panic in an error message, but theerr
variable should be returned if a panic is detected.defer func() { if r := recover(); r != nil { err = fmt.Errorf("badger iterator panic: %v", r) + return } }()
Committable suggestion