Skip to content

fix(scheduled): don't leave throttle stuck when the callback throws - #1115

Open
bardrop wants to merge 1 commit into
solidjs-community:mainfrom
bardrop-oss:fix-throttle-stuck-after-throw
Open

bardrop wants to merge 1 commit into
solidjs-community:mainfrom
bardrop-oss:fix-throttle-stuck-after-throw

Conversation

@bardrop

@bardrop bardrop commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

throttle sets isThrottled = false only after callback(...lastArgs) returns. So if the callback throws once, the flag stays true and every later call returns early. The throttled function is dead for the rest of the component's life, with no error after the first one:

const save = throttle((v: string) => api.save(v), 500); // api.save throws once (e.g. offline)
save("a"); // throws in the timer
save("b"); // ignored, forever

The same line also drops a call made from inside the callback (for example a callback that schedules the next poll), because the flag is still true at that moment. scheduleIdle has the same code. leadingAndTrailing builds on these and also keeps its own state at Trailing when the trailing call throws, so its next leading call is skipped too.

Change:

  • throttle / scheduleIdle: reset the flag before calling the callback.
  • leadingAndTrailing: reset state in a finally. I kept the order there as is, so a call made from inside its trailing callback doesn't turn into a sync recursive leading call.

Tests: 3 new cases in throttle.test.ts (throw in throttle, call from inside throttle, throw in leadingAndTrailing(throttle)) and a new schedule-idle.test.ts that stubs requestIdleCallback before import (jsdom doesn't have it, so the module picks the fallback otherwise). The 5 new cases fail on main and pass here. scheduled client (34) + ssr (5) tests, memo and bounds tests (they use throttle), eslint, prettier and tsc are clean. Patch changeset included.

Summary by CodeRabbit

  • Bug Fixes
    • Throttled and idle-scheduled callbacks can continue processing calls after a callback throws.
    • Calls made during a callback are now scheduled correctly, and leading-and-trailing throttling can recover after a trailing callback throws.

throttle and scheduleIdle reset their flag only after the callback
returned. If the callback threw once, every later call was ignored. A
call made from inside the callback was also dropped, since the flag was
still set. Reset the flag before calling the callback. leadingAndTrailing
now resets its state in a finally block for the same reason.
@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7ec3181

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solid-primitives/scheduled Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The scheduled primitives now reset callback state before invocation or in a finally block. Regression tests cover thrown callbacks, calls made from callbacks, and latest-argument coalescing. A patch changeset records the updates.

Changes: Scheduled callback state

Layer / File(s) Summary
Reset state before callbacks
packages/scheduled/src/index.ts, packages/scheduled/test/schedule-idle.test.ts, packages/scheduled/test/throttle.test.ts, .changeset/scheduled-throttle-reset-before-callback.md
throttle and scheduleIdle clear their scheduled-state flags before invoking callbacks. Tests cover callback errors, reentrant calls, and scheduleIdle argument coalescing. The changeset records a patch release.
Reset trailing-throttle state
packages/scheduled/src/index.ts, packages/scheduled/test/throttle.test.ts
leadingAndTrailing resets its state in a finally block around the trailing callback. A test covers a later call after the trailing callback throws.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: thetarnav

Merge Risk: Minimal · up to 7ec31

This fixes throttled and deferred callbacks staying stuck after a callback throws, and lets calls made from inside a callback run. The change is small and has regression tests, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: throttle no longer stays stuck after the callback throws. The changeset also covers scheduleIdle, leadingAndTrailing, and reentrant calls, but the title still refers t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: throttle no longer stays stuck after the callback throws. The changeset also covers scheduleIdle, leadingAndTrailing, and reentrant calls, but the title still refers to the primary fix. It uses the conventional fix(scheduled) scope and is specific enough for a reader scanning history.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a43460ba-c45f-4d3b-b72f-6fe97693632a
Commits

Reviewing files that changed from the base of the PR and between e936941 and 7ec3181.

Files selected for processing: 4

  • .changeset/scheduled-throttle-reset-before-callback.md
  • packages/scheduled/src/index.ts
  • packages/scheduled/test/schedule-idle.test.ts
  • packages/scheduled/test/throttle.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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