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

bugfix: useSyncExternalStore() seems to be not solid in avoiding tearing #27180

Closed
wants to merge 3 commits into from

Conversation

JSerZANP
Copy link

@JSerZANP JSerZANP commented Aug 2, 2023

Summary

Currently the update branch of useSyncExternalStore() - updateSyncExternalStore() sets up consistency check conditionally, while for mountSyncExternalStore() it is unconditionally.

This results in tearing still being possible when store mutates during rendering in concurrent mode,
if the mutation is not with emitting(which is possible considering the throttling of continuous events).

Here is the repro on stackblitz

Screenshot 2023-08-03 at 02 24 57

Proposed Fix

Same as mountSyncExternalStore(), we should add consistency check without condition.

How did you test this change?

Added one more test case, which didn't pass before the fix

Screenshot 2023-08-03 at 02 27 06

we see 'A0B0C1' being flushed - tearing happened

@facebook-github-bot
Copy link

Hi @JSerZANP!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

// Unless we're rendering a blocking lane, schedule a consistency check.
// Right before committing, we will walk the tree and check if any of the
// stores were mutated.
const root: FiberRoot | null = getWorkInProgressRoot();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved the consistency check out from the if condition, nothing more.

@react-sizebot
Copy link

react-sizebot commented Aug 2, 2023

Comparing: 493f72b...edd152a

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 164.38 kB 164.38 kB = 51.77 kB 51.77 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 171.79 kB 171.79 kB = 53.99 kB 53.98 kB
facebook-www/ReactDOM-prod.classic.js = 567.40 kB 567.41 kB +0.03% 100.10 kB 100.13 kB
facebook-www/ReactDOM-prod.modern.js = 551.20 kB 551.21 kB +0.04% 97.26 kB 97.30 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against edd152a

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@JSerZANP JSerZANP changed the title fix: useSyncExternalStore() seems to be not solid in avoiding tearing bugfix: useSyncExternalStore() seems to be not solid in avoiding tearing Aug 3, 2023
@rickhanlonii
Copy link
Member

I don't think I understand the use case for the store not emitting? I believe uSES requires that when the store changes the change is emitted.

@JSerZANP
Copy link
Author

JSerZANP commented Aug 10, 2023

@rickhanlonii thank you for taking a look!

Maybe this was not a good example, but I was thinking about some throttled event stream like scroll or mouse movement.

I believe uSES requires that when the store changes the change is emitted.

If we have this assumption, then I have a question on the necessity of consistency check.

Since inside the subscribe callback from uSES, React schedules SyncLane update on target fiber nodes.
This will be flushed synchronously, so the tearing issue is actually already avoided even without consistency check?

shaku (9)
https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberHooks.js#L1686-L1719

If I use set() rather than setWithoutEmit and remove the consistency check from updateSynExternalStore(),
the test actually passes, meaning consistency check might not be needed at all.

@rickhanlonii
Copy link
Member

rickhanlonii commented Aug 10, 2023

The consistency check is needed only when getSnapshot or subscribe changes during render. Check out this test, which fails when you comment out the consistency check, even though the subscriptions are emitted: https://github.com/facebook/react/blob/62cd5af08e2ac8b1d4691e75252487083cf7a4aa/packages/react-reconciler/src/__tests__/useSyncExternalStore-test.js#L83C1-L85

Notice that without the consistency check, React would commit the torn values with A0, B0, and C1. With the check, React restarts the render and renders A1 B1 and C1 together.

@JSerZANP
Copy link
Author

@rickhanlonii thanks for the reply.
looks like I misunderstood what consistency check does!

@JSerZANP JSerZANP closed this Aug 11, 2023
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.

None yet

4 participants