redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.89k stars 2.34k forks source link

Scan command in pipeline cause infinite loop #2811

Open a791446794 opened 9 months ago

a791446794 commented 9 months ago

Scan command in pipeline cause infinite loop

Expected Behavior

Scan command return an iterator, then invoke iterator.Next(ctx) should forward scan cursor.

Current Behavior

block at iterator.Next()

Possible Solution

Steps to Reproduce

        ctx := context.Background()
    var cmd *redis.ScanCmd
    cmds, err := rdb.Pipelined(ctx, func(pipeliner redis.Pipeliner) error {
        _ = pipeliner.Scan(ctx, 0, "som:key:*", 1000)
        return nil
    })
    cmd = cmds[0].(*redis.ScanCmd)
    if err != nil {
        t.Error(cmd.Err())
        return
    }
    var cnt = 1
    iter := cmd.Iterator()
    var keys []string
    for {
        val := iter.Val()
        if val != "" {
            keys = append(keys, val)
        }
        if !iter.Next(ctx) { // infinite loop here
            break
        }
        if iter.Err() != nil {
            t.Error(iter.Err())
            return
        }
        cnt++
    }
    t.Logf("cnt:%d,result, len(%d):\n%s", cnt, len(keys), strings.Join(keys, "\n"))

Context (Environment)

redis/v9

Detailed Description

Possible Implementation