fix(combineSlices): scope stateProxyMap per instance to prevent cross-instance proxy collisions#5319
Merged
EskiMojo14 merged 1 commit intoJun 23, 2026
Conversation
…-instance proxy collisions Two separate combineSlices() instances both sharing the same emptyObject reference as their initial state (from the module-level noopReducer) would have their selectors use the wrong proxy when the same state object was passed to both. The module-level stateProxyMap WeakMap meant the second selector call got the cached proxy from the first call, carrying the first instance's reducerMap and initialStateCache. Fix: move stateProxyMap inside combineSlices so each instance owns its own proxy cache. Add stateProxyMap as an explicit parameter to createStateProxy so the call site passes the per-instance map. > AI-assisted fix (GitHub Copilot / Claude)
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
commit: |
Collaborator
|
seems edge case-y, but nonetheless a legitimate bug with a simple fix - thanks! (and thank you for disclosing that you're using AI for these, the honesty is appreciated :P) |
aryaemami59
added a commit
to aryaemami59/redux-toolkit
that referenced
this pull request
Jun 29, 2026
…es-per-instance-stateProxyMap
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.
Problem
combineSlices.tsdeclaresstateProxyMapat module level:This WeakMap is shared across all
combineSlices()instances. The module-levelnoopReduceralways returns the sameemptyObjectconstant as initial state when no slices are given, so two emptycombineSlices()calls both produce the same object reference as their initial state.When a selector from instance A is called first with that shared state, a proxy is cached in the module-level
stateProxyMapkeyed by the state reference. A subsequent selector call from instance B with the same state reference retrieves the cached proxy from instance A, which closes over instance A'sreducerMapandinitialStateCache— so instance B's injected reducer's initial value is never consulted and the wrong value is returned.Fix
Move
stateProxyMapinsidecombineSlices(one WeakMap per instance) and thread it as an explicit parameter throughcreateStateProxy. Each instance now has an isolated proxy cache, so selecting against a shared state object from a different instance creates a fresh proxy with the correctreducerMapandinitialStateCache.Testing
A new unit test in
combineSlices.test.tsreproduces the bug (fails without this fix, passes with it). All 88 existing test files continue to pass.