Repository navigation
fix: await reset script across all regions in resetUsedTokens - #171
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 subsequentgetRemaining()orlimit()could observe stale counters and incorrectly report an identifier as still blocked.Root Cause
Both
resetTokensimplementations insrc/multi.ts(thefixedWindowandslidingWindowalgorithms) built their list of reset promises like this:The arrow function has a block body and no
return, somapproduces an array ofundefined.Promise.all([undefined, ...])settles on the next microtask while the realsafeEvalpromises run unawaited in the background. The existingresetUsedTokensintegration 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 insrc/single.tsdoes not have this bug because it awaitssafeEvaldirectly.Solution
Return each
safeEvalpromise from themapcallback soPromise.allactually awaits every region:The change is applied to both
MultiRegionRatelimit.fixedWindowandMultiRegionRatelimit.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 whoseevalshareturns controllable deferred promises, then asserts that:resetUsedTokens()stays pending while the reset scripts are still unresolved.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 findingsbunx prettier --check src/multi-reset.test.ts-> all matched files use Prettier code stylebunx tsc --noEmit ... src/multi.ts src/multi-reset.test.ts-> exit 0Notes on validation scope, reported honestly:
prettieris not a declared devDependency in this repo.bunxfetched the latest version, which reformats most ofsrc/multi.tsonmainas 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.bunx tsc --noEmitreports pre-existing errors underexamples/(missing next/react/deno dependencies) and insrc/deny-list/deny-list.test.ts. None are in the changed files..github/workflows/tests.yaml) runsbun run testand 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.