Skip to content

fix: keep outer drafts out of current() in nested producers - #1405

Open
YouKnowChatta wants to merge 1 commit into
immerjs:mainfrom
YouKnowChatta:fix/current-nested-produce
Open

YouKnowChatta wants to merge 1 commit into
immerjs:mainfrom
YouKnowChatta:fix/current-nested-produce

Conversation

@YouKnowChatta

Copy link
Copy Markdown

current() inside a nested producer can return a snapshot that still contains drafts of the outer producer. Before 11.2.14 this happened for children the inner recipe had read; since 11.2.14 it also happens for children it never touched. The outer producer revokes those drafts when it finishes, and reading the snapshot then throws:

import {produce, current, isDraft} from "immer"

let snapshot
produce({user: {name: "a", tags: {x: 1}}}, draft => {
	draft.user = produce(draft.user, user => {
		user.name = "b"
		snapshot = current(user)
	})
	isDraft(snapshot.tags) // true in 11.2.14, false in 11.2.13
})

JSON.stringify(snapshot)
// 11.2.13: {"name":"b","tags":{"x":1}}
// 11.2.14: TypeError: Cannot perform 'get' on a proxy that has been revoked

If the inner recipe reads user.tags.x before it calls current(), the same JSON.stringify call throws on 10.0.3, 10.1.1, 11.0.0, 11.1.0 and 11.2.13 as well.

The nested producer is the pattern that the "Always use the result of nested producers" section of the pitfalls page recommends. The current page says its result is not a Proxy and can be safely leaked from a produce function.

#1394 made current() reuse the children that a draft still shares with its base, because those children hold no drafts. Inside a nested producer that does not always hold. When the base there is a draft of the outer producer, or a plain object that holds one, the reused children are outer drafts. Array and Map drafts behave the same, and so does a producer whose base is a draft from createDraft.

The older failure has a separate cause. For an unmodified draft, current() returns state.base_. A child that the inner recipe reads becomes an inner draft whose base is the outer draft of that child, so the snapshot gets the outer draft.

With this change, an unmodified draft whose base is a draft returns the snapshot of that base. current() reuses base children only when the scope has no parent and the base is not a draft. Finalize uses the same scope.parent_ check to skip freezing in nested producers. In the other cases current() copies the children, as it did in 11.2.13. The change also fixes three nested producers and an outer draft kept in a Set of a nested producer, which fail on 11.2.13 too.

Top-level producers on plain data keep the #1394 behavior. On a top-level draft that holds 10,000 untouched objects, 50 current() calls take the same time with this change as on main, within run-to-run noise. Inside a nested producer they are faster than on main.

This change does not fix a top-level produce on plain data that holds a live draft from createDraft or from another Immer instance. 11.2.13 handles that case, and 11.2.14 and this change leave the draft in the snapshot.

Six tests in __tests__/current.js cover object, array and Map drafts passed to a nested producer, with and without the inner recipe reading a child first. They also cover three nested producers, a nested producer whose plain base holds an outer draft in an object or in a Set, and a producer on a createDraft draft. All six fail on main (b80ac34) and pass with the fix.

Validation (Node 24.23.0, Yarn 1.22.22, Linux):

  • yarn test: 4,295 source tests and 3,744 production-build tests passed, with 8 existing skips in each suite. Build and Flow passed.
  • yarn coverage, yarn test:perf, Prettier on both changed files, and git diff --check passed.

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