Skip to content

fix(store): remove eager state mutation in batch, fix race window#1632

Merged
Karanjot786 merged 3 commits into
Karanjot786:mainfrom
ionfwsrijan:fix/1627-setstate-batch-race
Jun 21, 2026
Merged

fix(store): remove eager state mutation in batch, fix race window#1632
Karanjot786 merged 3 commits into
Karanjot786:mainfrom
ionfwsrijan:fix/1627-setstate-batch-race

Conversation

@ionfwsrijan

@ionfwsrijan ionfwsrijan commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Description

In batch(), setState eagerly assigns state = nextState at line 310 before the microtask commit runs. This creates a race window where any synchronous non-batched setState() or mutate() call that reads getState() sees the intermediate batched value and writes changes that are later silently overwritten by the batch's commit().

The same bug exists in mutate() at line 389.

Fix

  1. Removed state = nextState inside the batch path in setState (line 310) — only commit() sets final state
  2. Removed state = nextState inside the batch path in mutate (line 389) — moved to non-batch path only
  3. Updated getState() to return pending batch nextState when _batchDepth > 0, so in-batch reads see correct intermediate state
  4. Updated setState function updaters to receive pending batch state when inside a batch
  5. Updated mutate to produce from pending batch state when inside a batch

Tests

  • setState outside batch after batch call is not overwritten by commit
  • getState inside batch returns pending batch state

Closes #1627

Summary by CodeRabbit

  • Bug Fixes
    • Improved batching behavior for state updates and reads. getState() now correctly reflects pending updates during an active batch, and state mutations within batches compute from the in-flight state rather than the previously committed state, ensuring more consistent behavior when batching multiple updates together.

@ionfwsrijan ionfwsrijan requested a review from Karanjot786 as a code owner June 19, 2026 06:17
@github-actions github-actions Bot added type:bug +10 pts. Bug fix. type:testing +10 pts. Tests. and removed type:bug +10 pts. Bug fix. labels Jun 19, 2026
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@ionfwsrijan, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 14 minutes and 35 seconds. Learn how PR review limits work.

To continue reviewing without waiting, enable usage-based billing in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 76195843-dcc7-4bf8-88bb-0f949eb5a0e2

📥 Commits

Reviewing files that changed from the base of the PR and between ecaaeb8 and 8c06994.

📒 Files selected for processing (1)
  • packages/store/src/store.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the type:bug +10 pts. Bug fix. label Jun 19, 2026
@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@Karanjot786 Please review this

@Karanjot786 Karanjot786 merged commit e262837 into Karanjot786:main Jun 21, 2026
6 checks passed
@Karanjot786 Karanjot786 added gssoc:approved Approved PR. Earns +50 base points. level:intermediate +35 pts. Moderate task. area:store @termuijs/store quality:clean x 1.2 multiplier. Clean implementation. labels Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:store @termuijs/store gssoc:approved Approved PR. Earns +50 base points. level:intermediate +35 pts. Moderate task. quality:clean x 1.2 multiplier. Clean implementation. type:bug +10 pts. Bug fix. type:testing +10 pts. Tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] setState eagerly mutates state inside batch(), creating race window where non-batched updates are silently overwritten

2 participants