fix(store): avoid panic when seeking with an empty prefix - #528
Open
memosr wants to merge 1 commit into
Open
Conversation
VersionedIterator.first() passes vi.prefix straight to pebble's SeekGE. When an
iterator is created with no prefix, that value is not nil: Txn.NewIterator builds
it with lib.Append(t.prefix, prefix), and lib.Append always returns
make([]byte, 0) for two empty inputs, which is a non-nil zero length slice.
Pebble guards its seek key with a nil check but not a length check, so a zero
length non-nil key gets past the guard and indexes into an empty slice:
panic: runtime error: index out of range [0] with length 0
pebble/v2.testingDisableSeekOpt(...)
pebble/v2@v2.1.6/iterator.go:1521
store.(*VersionedIterator).first()
store/versioned_store.go:369
This aborts the whole package under -race, so no store test could run with the
race detector enabled.
Normalize an empty seek key to nil before handing it to SeekGE. The semantics are
unchanged: an empty prefix already meant start from the beginning.
Before:
$ go test -race ./store/
--- FAIL: TestNestedTxnMergedIteration (0.00s)
panic: runtime error: index out of range [0] with length 0
FAIL
After:
$ go test -race ./store/
ok github.com/canopy-network/canopy/store 11.446s
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.
Problem
go test -race ./store/cannot complete. The package aborts with a panic beforemost tests get to run:
--- FAIL: TestNestedTxnMergedIteration (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered, repanicked]
github.com/cockroachdb/pebble/v2.testingDisableSeekOpt(...)
pebble/v2@v2.1.6/iterator.go:1521
github.com/cockroachdb/pebble/v2.(*mergingIter).seekGE(...)
pebble/v2@v2.1.6/merging_iter.go:958
github.com/canopy-network/canopy/store.(*VersionedIterator).first()
store/versioned_store.go:369
github.com/canopy-network/canopy/store.(*VersionedIterator).Valid()
store/versioned_store.go:321
github.com/canopy-network/canopy/store.(*TxnIterator).Valid()
store/txn.go:498
Root cause
VersionedIterator.first()handsvi.prefixdirectly to pebble'sSeekGE.When an iterator is created with no prefix, that value is not
nil.Txn.NewIteratorbuilds the parent prefix withlib.Append(t.prefix, prefix),and
lib.Appendallocates withmake([]byte, len(a)+len(b)). For two emptyinputs that returns a non-nil zero length slice:
Pebble's seek key guard checks
key != nilbut notlen(key) > 0, so a zerolength non-nil key passes the guard and then indexes
key[0]on an empty slice.The panic only surfaces under the invariants build that
-raceenables, so itdoes not hit a production node. The cost is that the race detector is effectively
unusable on this package, which means genuine races in
storestay invisible.Change
Normalize an empty seek key to
nilbefore passing it toSeekGE. Semantics areunchanged, an empty prefix already meant start from the beginning.
Verification
Before:
$ go test -race ./store/
--- FAIL: TestNestedTxnMergedIteration (0.00s)
panic: runtime error: index out of range [0] with length 0
FAIL
After:
$ go test -race ./store/
ok github.com/canopy-network/canopy/store 11.446s
The whole package now runs clean under the race detector.
gofmtclean.Notes
Only the
SeekGEpath is changed. The reverse path callsSeekLT(prefixEnd(vi.prefix)),and
prefixEndappendsendBytes, so its key is never empty.CONTRIBUTING.mdasks for PRs againstdevelopment, but no such branch exists onthe remote and recently merged PRs target
main, so this targetsmain.Happy to retarget.
Related: #527 fixes a separate data race in
lib/crypto. The two are independent.