Skip to content

fix: avoid extra 100ms sleep after deadline in blockUntilReady - #177

Open
Tyagiquamar wants to merge 1 commit into
upstash:mainfrom
Tyagiquamar:fix/block-until-ready-deadline
Open

Tyagiquamar wants to merge 1 commit into
upstash:mainfrom
Tyagiquamar:fix/block-until-ready-deadline

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Prevent Ratelimit.blockUntilReady from sleeping an extra 100ms (or running an unnecessary retry) once the timeout deadline has been reached.

Root Cause & Problem

In src/ratelimit.ts, blockUntilReady computed wait = Math.min(res.reset, deadline) - Date.now() and slept for wait > 0 ? wait : 100 before checking if (Date.now() > deadline):

  1. If this.limit(identifier) itself took longer than timeout (or Date.now() reached deadline), wait <= 0, so blockUntilReady unconditionally slept 100ms even though the deadline had already expired.
  2. If res.reset <= Date.now() (past reset / clock skew) while remainingTimeout < 100, the 100ms fallback sleep overslept past deadline.
  3. Checking Date.now() > deadline instead of Date.now() >= deadline allowed an extra iteration (and another limit() call + 100ms sleep) when waking up exactly at deadline.

Technical Solution

  • Check remainingTimeout = deadline - Date.now() before sleeping and break immediately when remainingTimeout <= 0.
  • Cap the past-reset fallback sleep at Math.min(100, remainingTimeout) so it never oversleeps past deadline.
  • Use Date.now() >= deadline for the post-sleep deadline check.
  • Add regression tests in src/block-until-ready-deadline.test.ts.

Test Plan

  • bun test src/block-until-ready-deadline.test.ts src/block-until-ready-denylist.test.ts: 5 pass, 0 fail (19 expect() calls).
  • bunx eslint src/ratelimit.ts src/block-until-ready-deadline.test.ts --quiet: passed with 0 errors.
  • bun run build: CJS, ESM, and DTS build succeeded.

@vercel

vercel Bot commented Nov 25, 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