-
Notifications
You must be signed in to change notification settings - Fork 47k
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] Passive effects triggered by synchronous renders in a multi-root app #17347
Conversation
The bug ------- In a multi-root app, certain passive effects (`useEffect`) are never fired. See facebook#17066. The underlying problem ---------------------- The implicit contract of `flushPassiveEffects` is that, right after calling it, there should be no pending passive effects. In the normal case, in concurrent mode, this is true. But the current implementation fails to account for the case where a passive effect schedules synchronous work, which in turn schedules additional passive effects. This led to `rootWithPendingPassiveEffects` being overwritten in the commit phase, because an assignment that assumed it was replacing null was actually replacing a reference to another root, which has the consequence of dropping passive effects on that root. The fix ------- The fix I've chosen here is, at the beginning of the commit phase, keep flushing passive effects in a loop until there are no more. This doesn't not change the "public" implementation of `flushPassiveEffects`, though it arguably should work this way, too. I say "public" because it's only used by implementation layers on top of React which we control: mainly, the legacy version of `act` that does not use the mock Scheduler build. So there's probably still a bug in that `act` implementation. I will address `act` in a follow-up. The ideal solution is to replace the legacy `act` with one implemented directly in the renderer, using a special testing-only build of React DOM. Since that requires a breaking change, we'll need an interim solution. We could make the "public" `act` recursively flush effects in a loop, as I've done for the commit phase. However, I think a better solution is to stop automatically flushing the synchronous update queue at the end of `flushPassiveEffects`, and instead require the caller to explicitly call `flushSyncUpdateQueue` (or the equivalent) if needed. This follows the same pattern we use internally in the work loop, which is designed to avoid factoring hazards like the one that resulted in this bug.
Details of bundled changes.Comparing: 2c6ea0b...c386a55 react-reconciler
react-dom
react-art
react-test-renderer
react-native-renderer
ReactDOM: size: 0.0%, gzip: 🔺+0.1% Size changes (stable) |
Details of bundled changes.Comparing: 2c6ea0b...c386a55 react-test-renderer
react-native-renderer
react-dom
react-art
react-reconciler
ReactDOM: size: 0.0%, gzip: 0.0% Size changes (experimental) |
Do we know when this regressed? |
Not sure, I'll have to check. I'm going to cut a minor regardless, so I'll do that first. If enough people complain we can consider cutting some patches, too. |
Can we close #17066 now? Can you verify its repro works now? |
Verified with https://codesandbox.io/s/react-typescript-2thgm |
The bug
In a multi-root app, certain passive effects (
useEffect
) are never fired. See #17066.The underlying problem
The implicit contract of
flushPassiveEffects
is that, right after calling it, there should be no pending passive effects. In the normal case, in concurrent mode, this is true. But the current implementation fails to account for the case where a passive effect schedules synchronous work, which in turn schedules additional passive effects.This led to
rootWithPendingPassiveEffects
being overwritten in the commit phase, because an assignment that assumed it was replacing null was actually replacing a reference to another root, which has the consequence of dropping passive effects on that root.The fix
The fix I've chosen here is, at the beginning of the commit phase, keep flushing passive effects in a loop until there are no more.
This doesn't not change the "public" implementation of
flushPassiveEffects
, though it arguably should work this way, too. I say "public" because it's only used by implementation layers on top of React which we control: mainly, the legacy version ofact
that does not use the mock Scheduler build. So there's probably still a bug in thatact
implementation.I will address
act
in a follow-up. The ideal solution is to replace the legacyact
with one implemented directly in the renderer, using a special testing-only build of React DOM. Since that requires a breaking change, we'll need an interim solution. We could make the "public"act
recursively flush effects in a loop, as I've done for the commit phase. However, I think a better solution is to stop automatically flushing the synchronous update queue at the end of
flushPassiveEffects
, and instead require the caller to explicitly callflushSyncUpdateQueue
(or the equivalent) if needed. This follows the same pattern we use internally in the work loop, which is designed to avoid factoring hazards like the one that resulted in this bug.