Skip to content

fix: await reset script across all regions in resetUsedTokens - #171

Open
Tyagiquamar wants to merge 1 commit into
upstash:mainfrom
Tyagiquamar:fix/multi-region-reset-await
Open

Tyagiquamar wants to merge 1 commit into
upstash:mainfrom
Tyagiquamar:fix/multi-region-reset-await

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

MultiRegionRatelimit.resetUsedTokens() now waits until the reset Lua script has finished deleting keys in every configured region before it resolves. Previously it returned immediately, so a subsequent getRemaining() or limit() could observe stale counters and incorrectly report an identifier as still blocked.

Root Cause

Both resetTokens implementations in src/multi.ts (the fixedWindow and slidingWindow algorithms) built their list of reset promises like this:

await Promise.all(
  ctx.regionContexts.map((regionContext) => {
    safeEval(regionContext, RESET_SCRIPT, [pattern], [null]);
  })
);

The arrow function has a block body and no return, so map produces an array of undefined. Promise.all([undefined, ...]) settles on the next microtask while the real safeEval promises run unawaited in the background. The existing resetUsedTokens integration test masks this race with a fixed 300 ms sleep after the reset, which hides the problem on a fast connection but not under real network latency. The single-region implementation in src/single.ts does not have this bug because it awaits safeEval directly.

Solution

Return each safeEval promise from the map callback so Promise.all actually awaits every region:

await Promise.all(
  ctx.regionContexts.map((regionContext) =>
    safeEval(regionContext, RESET_SCRIPT, [pattern], [null])
  )
);

The change is applied to both MultiRegionRatelimit.fixedWindow and MultiRegionRatelimit.slidingWindow. No public API changes.

Test Plan

Added src/multi-reset.test.ts, a pure unit test that needs no Redis credentials. It creates two mocked regional clients whose evalsha returns controllable deferred promises, then asserts that:

  1. Both regions receive the reset script call.
  2. resetUsedTokens() stays pending while the reset scripts are still unresolved.
  3. It resolves only after both scripts complete.

The test fails against the original code (where resetUsedTokens() resolves early, before the deferreds are released) and passes with the fix.

Validation executed locally (Windows, Bun 1.4.4, Node 24):

  • bun test src/multi-reset.test.ts -> 1 pass, 0 fail (4 assertions)
  • bunx eslint "src/multi.ts" "src/multi-reset.test.ts" --quiet -> exit 0, no findings
  • bunx prettier --check src/multi-reset.test.ts -> all matched files use Prettier code style
  • scoped bunx tsc --noEmit ... src/multi.ts src/multi-reset.test.ts -> exit 0

Notes on validation scope, reported honestly:

  • prettier is not a declared devDependency in this repo. bunx fetched the latest version, which reformats most of src/multi.ts on main as well. To keep the review diff minimal I did not reformat the whole file; I verified that the two edited lines match Prettier's own output.
  • The repo-wide bunx tsc --noEmit reports pre-existing errors under examples/ (missing next/react/deno dependencies) and in src/deny-list/deny-list.test.ts. None are in the changed files.
  • CI (.github/workflows/tests.yaml) runs bun run test and the example builds; it does not gate on prettier or tsc. The full suite and the real multi-region integration tests are left to GitHub Actions.

@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@Tyagiquamar is attempting to deploy a commit to the Upstash Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant