vercel / vercel/storage

`kv.scanIterator()` not terminating in `for await` loop after updating to version 2.0.0

Open
#706 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
595
Forks
101
Avg merge
2h 24m
Merged PRs (30d)
1

Description

After updating from version 1.0.1 to version 2.0.0, the function kv.scanIterator() no longer works correctly. When using it in a for await loop, the loop never exits. It is not an infinite loop as it processes keys correctly, but it never reaches the return statement of the function.

Here is the code demonstrating the issue:

export async function startRun() {
  console.log("Iniciando for");

  for await (const key of kv.scanIterator()) {
    const value = await kv.get(key);
    console.log({ key, value });

    console.log("ultima iteración del for");
  }

  return "Finalizando y retornando";
}

The console.log statements execute correctly for each key found, but the loop never terminates, preventing the function from returning.

I attempted to resolve the issue by adding the following configuration:

const kv = createClient({
  url: process.env.KV_REST_API_URL,
  token: process.env.KV_REST_API_TOKEN,
  enableAutoPipelining: false,
});

However, the problem persists. As a temporary workaround, I have reverted to version 1.0.1, where the function works as expected.

Steps to Reproduce:

  1. Update @vercel/kv from version 1.0.1 to version 2.0.0.
  2. Use kv.scanIterator() in a for await loop.
  3. Observe that the loop never exits.

Expected Behavior:
The for await loop should terminate after processing all keys and the function should return the expected result.

Actual Behavior:
The for await loop processes keys correctly but never exits, preventing the function from returning.

Environment:

  • @vercel/kv version: 2.0.0
  • Node.js version: 20.15.0
  • Operating System: Windows 11

Thank you for your assistance in resolving this issue.


Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with @vercel/kv 2.0.0 using the provided startRun function and compare its scanIterator behavior with version 1.0.1. Trace the iterator's termination path; done means the for-await loop processes all keys, exits, and reaches the return statement without requiring auto-pipelining to be disabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.