Skip to content

Commit 592617a

Browse files
joamakidylandreimerink
authored andcommitted
part: Fix returned watch channel in search()
If there was a leaf node with a key that was shorter than our search key we incorrectly returned that leaf node's watch channel instead of the channel of the parent non-leaf node. This caused the watch channel returned by GetWatch() to not close when expected. Signed-off-by: Jussi Maki <[email protected]>
1 parent 2c163b8 commit 592617a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

part/node.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,13 @@ func newNode4[T any]() *header[T] {
474474

475475
func search[T any](root *header[T], key []byte) (value T, watch <-chan struct{}, ok bool) {
476476
this := root
477+
watch = root.watch
477478
for {
478-
watch = this.watch
479-
480-
// Consume the prefix
481479
if !bytes.HasPrefix(key, this.prefix) {
482480
return
483481
}
482+
483+
// Consume the prefix
484484
key = key[len(this.prefix):]
485485

486486
if len(key) == 0 {
@@ -492,6 +492,12 @@ func search[T any](root *header[T], key []byte) (value T, watch <-chan struct{},
492492
return
493493
}
494494

495+
// Prefix matched. Remember this as the closest watch channel as we traverse
496+
// further.
497+
if !this.isLeaf() && this.watch != nil {
498+
watch = this.watch
499+
}
500+
495501
this = this.find(key[0])
496502
if this == nil {
497503
return

0 commit comments

Comments
 (0)