fix(children): close iterators when React.Children.map callback throws - #37463
Open
mturac wants to merge 1 commit into
Open
fix(children): close iterators when React.Children.map callback throws#37463mturac wants to merge 1 commit into
mturac wants to merge 1 commit into
Conversation
React.Children manually iterates via next() but never calls return() when the map callback or recursive traversal throws. This leaves generator cleanup and custom iterable resource release pending. Wrap the iteration loop in try/finally so return() is called on any abrupt exit while preserving the original error.
mturac
force-pushed
the
fix/issue-37427
branch
from
August 30, 2026 21:02
fdd7b6a to
11aa633
Compare
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.
Summary
Fixes #37427 —
React.Children.map(andforEach,count,toArray) manually iterates viaiterator.next()but never callsiterator.return()when the map callback or recursive traversal throws.Root cause:
mapIntoArrayinReactChildren.jsconsumes the iterator in a while loop. If the user-suppliedcallbackor the recursivemapIntoArraycall throws, the function exits without signaling the iterator. Per the iterator protocol, a consumer that stops early must callreturn()so the producer can run cleanup (finallyblocks in generators, resource release in custom iterables).Fix: Wrap the iteration loop in
try/finally. A tracking flag (iteratorDone) distinguishes normal completion from abrupt exit. Thefinallyblock callsreturn()only when the iterator did not run to completion, and swallows any error fromreturn()itself to preserve the original error.Changed files
packages/react/src/ReactChildren.js— try/finally around iterator consumption inmapIntoArraypackages/react/src/__tests__/ReactChildren-test.js— regression test: custom iterable with areturn()spy, map callback throws on second element, verifiesreturn()was called exactly onceTest plan
return()spy called when map callback throws — passedReactChildren-test.jssuite — 43/43 passed