-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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: Fix crash when suspending in shell during useSES re-render #27199
Merged
acdlite
merged 2 commits into
facebook:main
from
acdlite:bugfix-suspend-after-store-mutation
Aug 8, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ let forwardRef; | |
let useImperativeHandle; | ||
let useRef; | ||
let useState; | ||
let use; | ||
let startTransition; | ||
let waitFor; | ||
let waitForAll; | ||
|
@@ -41,6 +42,7 @@ describe('useSyncExternalStore', () => { | |
forwardRef = React.forwardRef; | ||
useRef = React.useRef; | ||
useState = React.useState; | ||
use = React.use; | ||
useSyncExternalStore = React.useSyncExternalStore; | ||
startTransition = React.startTransition; | ||
|
||
|
@@ -212,4 +214,84 @@ describe('useSyncExternalStore', () => { | |
}); | ||
assertLog(['value:initial']); | ||
}); | ||
|
||
test( | ||
'regression: suspending in shell after synchronously patching ' + | ||
'up store mutation', | ||
async () => { | ||
// Tests a case where a store is mutated during a concurrent event, then | ||
// during the sync re-render, a synchronous render is triggered. | ||
|
||
const store = createExternalStore('Initial'); | ||
|
||
let resolve; | ||
const promise = new Promise(r => { | ||
resolve = r; | ||
}); | ||
|
||
function A() { | ||
const value = useSyncExternalStore(store.subscribe, store.getState); | ||
|
||
if (value === 'Updated') { | ||
try { | ||
use(promise); | ||
} catch (x) { | ||
Scheduler.log('Suspend A'); | ||
throw x; | ||
} | ||
} | ||
|
||
return <Text text={'A: ' + value} />; | ||
} | ||
|
||
function B() { | ||
const value = useSyncExternalStore(store.subscribe, store.getState); | ||
return <Text text={'B: ' + value} />; | ||
} | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<span> | ||
<A /> | ||
</span> | ||
<span> | ||
<B /> | ||
</span> | ||
</> | ||
); | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
await act(async () => { | ||
// A and B both read from the same store. Partially render A. | ||
startTransition(() => root.render(<App />)); | ||
// A reads the initial value of the store. | ||
await waitFor(['A: Initial']); | ||
|
||
// Before B renders, mutate the store. | ||
store.set('Updated'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the fix, the test would crash here with an internal error ("Unknown root status") |
||
assertLog([ | ||
// B reads the updated value of the store. | ||
'B: Updated', | ||
// This should a synchronous re-render of A using the updated value. In | ||
// this test, this causes A to suspend. | ||
'Suspend A', | ||
]); | ||
// Nothing has committed, because A suspended and no fallback | ||
// was provided. | ||
expect(root).toMatchRenderedOutput(null); | ||
|
||
// Resolve the data and finish rendering. | ||
await act(() => resolve()); | ||
assertLog(['A: Updated', 'B: Updated']); | ||
expect(root).toMatchRenderedOutput( | ||
<> | ||
<span>A: Updated</span> | ||
<span>B: Updated</span> | ||
</>, | ||
); | ||
}, | ||
); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just for clarity, right? As far as I can tell, it's not read anymore at this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah :D in case a
continue
gets added later in the loop or something