Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Context pollutes mutable global state (with "thread local storage") #13877

Closed
wants to merge 2 commits into from

Conversation

leebyron
Copy link
Contributor

Fixes #13874

Alternative to #13875

This straw-man approach also avoids mutating global context by giving each ReactPartialRenderer its own mutable state and using context instances as keys into that mutable state.

The new context API stores the provided values on the shared context instance. When used in a synchronous context, this is not an issue. However when used in an concurrent context this can cause a "push provider" from one react render to have an effect on an unrelated concurrent react render.

I've encountered this bug in production when using renderToNodeStream, which asks ReactPartialRenderer for bytes up to a high water mark before yielding. If two Node Streams are created and read from in parallel, the state of one can polute the other.

I wrote a failing test to illustrate the conditions under which this happens.

I'm also concerned that the experimental concurrent/async React rendering on the client could suffer from the same issue.
…storage")

Fixes facebook#13874

Alternative to facebook#13875

This straw-man approach also avoids mutating global context by giving each ReactPartialRenderer it's own mutable state and using context instances as keys into that mutable state.
@sizebot
Copy link

sizebot commented Oct 17, 2018

Details of bundled changes.

Comparing: 4f0bd45...1e5100e

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom-server.browser.development.js +0.5% +0.6% 106.56 KB 107.14 KB 28.3 KB 28.48 KB UMD_DEV
react-dom-server.browser.production.min.js 🔺+1.2% 🔺+0.9% 15.53 KB 15.72 KB 5.95 KB 6 KB UMD_PROD
react-dom-server.browser.development.js +0.6% +0.7% 102.69 KB 103.28 KB 27.3 KB 27.48 KB NODE_DEV
react-dom-server.browser.production.min.js 🔺+1.2% 🔺+0.9% 15.44 KB 15.63 KB 5.88 KB 5.93 KB NODE_PROD
react-dom-server.node.development.js +0.6% +0.7% 104.61 KB 105.2 KB 27.85 KB 28.04 KB NODE_DEV
react-dom-server.node.production.min.js 🔺+1.2% 🔺+0.9% 16.24 KB 16.43 KB 6.18 KB 6.24 KB NODE_PROD
ReactDOMServer-dev.js +0.6% +0.7% 102.16 KB 102.79 KB 26.59 KB 26.79 KB FB_WWW_DEV
ReactDOMServer-prod.js 🔺+0.7% 🔺+0.8% 34 KB 34.25 KB 8.17 KB 8.23 KB FB_WWW_PROD

scheduler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
scheduler.development.js n/a n/a 0 B 19.17 KB 0 B 5.74 KB UMD_DEV
scheduler.production.min.js n/a n/a 0 B 3.16 KB 0 B 1.53 KB UMD_PROD

Generated by 🚫 dangerJS

@leebyron leebyron changed the title [RFC] FIX: Context pollutes mutable global state (with "thread local storage") FIX: Context pollutes mutable global state (with "thread local storage") Oct 18, 2018
@leebyron
Copy link
Contributor Author

Hey @sebmarkbage, how are you all thinking of the next version cut. Is there any chance of getting this into a patch release?

@sebmarkbage
Copy link
Collaborator

Landed #14182 instead but would be good to perf test both to validate the theory.

@sebmarkbage sebmarkbage closed this Nov 9, 2018
@leebyron leebyron deleted the fix2-node-stream-bug branch November 12, 2018 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants