Skip to content

Scan does not return all records when using the count param and deleting records while iterating #413

@johnmaguire

Description

@johnmaguire
package main

import (
	"context"
	"fmt"
	"log"
	"testing"

	"github.com/alicebob/miniredis/v2"
	"github.com/redis/go-redis/v9"
)

func TestSomething(t *testing.T) {
	s := miniredis.RunT(t)
	c := redis.NewClient(&redis.Options{Addr: s.Addr()})
	for i := range 10 {
		c.Set(context.Background(), fmt.Sprintf("foo%d", i), "bar", 0)
	}

	// iterate 10 records with page size 5
	dbSize := c.DBSize(context.Background()).Val()
	iter := c.Scan(context.Background(), 0, "foo*", 5).Iterator()
	scanned := 0
	for iter.Next(context.Background()) {
		scanned++
	}
	log.Printf("%d values, page size 5: %d\n", dbSize, scanned)

	// iterate 10 records with page size 5 while deleting records
	dbSize = c.DBSize(context.Background()).Val()
	iter = c.Scan(context.Background(), 0, "foo*", 5).Iterator()
	scanned = 0
	for iter.Next(context.Background()) {
		c.Del(context.Background(), iter.Val())
		scanned++
	}
	log.Printf("%d values, page size 5, deleting each record: %d\n", dbSize, scanned)

    // second iteration is necessary to visit all records
	dbSize = c.DBSize(context.Background()).Val()
	iter = c.Scan(context.Background(), 0, "foo*", 5).Iterator()
	scanned = 0
	for iter.Next(context.Background()) {
		c.Del(context.Background(), iter.Val())
		scanned++
	}
	log.Printf("%d values, page size 5, deleting each record: %d\n", dbSize, scanned)
}
❯ go test -v .
=== RUN   TestSomething
2025/09/12 01:50:47 10 values, page size 5: 10
2025/09/12 01:50:47 10 values, page size 5, deleting each record: 5
2025/09/12 01:50:47 5 values, page size 5, deleting each record: 5
--- PASS: TestSomething (0.00s)
PASS
ok  	github.com/johnmaguire/miniredis-bug-poc	0.168s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions