fix(store): remove eager state mutation in batch, fix race window#1632
Conversation
|
Warning Review limit reached
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 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
@Karanjot786 Please review this |
Description
In
batch(),setStateeagerly assignsstate = nextStateat line 310 before the microtask commit runs. This creates a race window where any synchronous non-batchedsetState()ormutate()call that readsgetState()sees the intermediate batched value and writes changes that are later silently overwritten by the batch'scommit().The same bug exists in
mutate()at line 389.Fix
state = nextStateinside the batch path insetState(line 310) — onlycommit()sets final statestate = nextStateinside the batch path inmutate(line 389) — moved to non-batch path onlygetState()to return pending batchnextStatewhen_batchDepth > 0, so in-batch reads see correct intermediate statesetStatefunction updaters to receive pending batch state when inside a batchmutateto produce from pending batch state when inside a batchTests
setState outside batch after batch call is not overwritten by commitgetState inside batch returns pending batch stateCloses #1627
Summary by CodeRabbit
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.