Let openBox skip records it cannot decode instead of failing outright - #321
Open
LahaLuhem wants to merge 4 commits into
Open
Let openBox skip records it cannot decode instead of failing outright#321LahaLuhem wants to merge 4 commits into
LahaLuhem wants to merge 4 commits into
Conversation
This was referenced Aug 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #318.
Right now one record whose adapter throws takes the whole box with it.
openBoxdecodes every value before the box exists, so a single failure means no box at all and nothing else in there is reachable.This adds an opt-in way to skip that record and keep the rest.
Leave it off and nothing changes. Pass it and bad records are skipped, with the key handed to you so you can log it, delete it or refetch it.
Nice side effect: throwing from inside the handler aborts the open. So you can skip a
FormatExceptionand still let aHiveErrorbe fatal, which matters because a missing adapter registration and real corruption both surface as "unknown typeId" and only your app knows which it is. No second flag needed for that.How it works
readFramereads the frame length and checks the crc before it touches the value, so a record that fails to decode is structurally fine and its end is already known. Wrapping just the value-decode branch and falling back toFrame.deleted(key)drops that key, and the existing tail winds the reader to the exact frame end so everything after it still decodes.Web goes through a different path with no frames at all, so it gets the same treatment around its per-value decode.
Not included, on purpose
openLazyBoxget, scoped to that one keyIsolatedHiveConfirmed the second one rather than assumed it:
Testing
Six new tests. Four on the VM covering no-handler-still-throws, skip-and-report, throwing-aborts, and clean-box-never-called. Two on web built with
encodeValueso the bytes really are undecodable.Since these cover a new parameter, they cannot be red beforehand, they would not compile. So I broke the logic instead and checked the tests noticed.
What the mutations showed
readFramealways rethrows['b','c']instead of['b']That last one bit me for real while writing it.
List.map's iterator sets its current value before bumping the index, so a throwing element gets retried and the failure lands on the wrong key. That is why the first commit exists.One test passed under mutation at first, because when everything rethrows the open fails anyway.
f1e25719makes it count handler calls so it fails properly.Ran 591 on the VM and 495 on chrome under both dart2js and dart2wasm.
dart analyze --fatal-infosanddart format --set-exit-if-changedclean here and inhive_flutter,hive_generatorandhive_ce_inspector. Also checked a lazygeton a bad record still throws, so the handler does not leak into the read path.Needs more consideration
StorageBackendJshas grown a fifth optional positional parameter, so the manager now passesTypeRegistryImpl.nullImplexplicitly. Named parameters would read better but that is a wider change to an internal class, happy to do it if you want.getValues(cursor: true)dartifies instead of decoding. Pre-existing and untouched, but mygetRawValuessits next to it and makes the difference easier to spot. Looks like its own bug.