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

Bug: setState updater called but not rendered, in Safari, in concurrent mode #22459

Closed
jamesknelson opened this issue Sep 29, 2021 · 17 comments · Fixed by #23111
Closed

Bug: setState updater called but not rendered, in Safari, in concurrent mode #22459

jamesknelson opened this issue Sep 29, 2021 · 17 comments · Fixed by #23111
Labels
Browser: Safari React 18 Bug reports, questions, and general feedback about React 18 Type: Bug

Comments

@jamesknelson
Copy link

jamesknelson commented Sep 29, 2021

React version: 18.0.0-alpha-9175f4d15-20210928

Steps To Reproduce

Minimal reproduction in this codesandbox.

This issue only appears in Safari, including mobile Safari. It works fine in Chrome and Firefox.

In order to reproduce, you'll need to:

  1. Render the app with createRoot in the latest 18.0 alpha
  2. Within an element's ref function, append an iframe to that element using appendChild

The following steps have been removed after simplifying the reproduction:

1. Make a state change originating in a message from a YouTube embed iframe. I assume the same issue would occur with messages from other cross domain iframes, although I have not tested this. The issue does not appear when making an async state change through setTimeout.
2. Render a tooltip using the "tippy.js" library. I haven't dug into what inside of Tippy is causing this, but commenting out the tippy element resolves the issue. I have confirmed that rendering a simple portal does not reproduce the issue.

Link to code example:

https://codesandbox.io/s/black-moon-ymw10?file=/src/index.js

The current behavior

In the example in safari, the updater function passed to setState is run (confirmed with the "run state updater" console.log)... but the new state is never passed to the component function, leaving the app hanging with no error message, and displaying "not done".

The expected behavior

In Chrome and Firefox, the App component updates after the state updater function is called, as expected, displaying "done".


Thanks for all the wonderful work on React, btw! It's been a lot of fun playing with the new concurrent React features. Can't wait to see them hit a stable release!

@jamesknelson jamesknelson added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Sep 29, 2021
@jamesknelson jamesknelson changed the title Bug: Bug: setState updater called but not rendered, in Safari, in concurrent mode Sep 29, 2021
@eps1lon eps1lon added Browser: Safari React 18 Bug reports, questions, and general feedback about React 18 labels Sep 29, 2021
@eps1lon
Copy link
Collaborator

eps1lon commented Sep 29, 2021

Confirmed with Safari 14.1.2.

Works with a production build though: https://csb-y2mb5-bjp2gai2h-eps1lon.vercel.app/

I would also open this against the tippy.js repository since they are more likely to know if they're doing something funky.

@jamesknelson
Copy link
Author

I've done a little more playing with this and it turns out it's not postMessage that's triggering the issue, but adding an iframe in the ref.

Here's another demo with an <iframe src="localhost"> added within the ref function: https://codesandbox.io/s/sharp-fermat-zvmoi?file=/src/index.js

@jamesknelson
Copy link
Author

jamesknelson commented Sep 29, 2021

I've played with this a little more and managed to reproduce the issue without any dependencies other than React: https://codesandbox.io/s/black-moon-ymw10?file=/src/index.js

The trigger seems to be appending an iframe to an element within its ref function. Appending a div does not cause the issue, and rendering a <iframe /> element using React (instead of manually appending with appendChild) also avoids the issue.

Will update the steps to reproduce.

@stale
Copy link

stale bot commented Jan 8, 2022

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 8, 2022
@jamesknelson
Copy link
Author

Confirmed this issue still exists on Safari 14.1.1, with react 18.0.0-rc.0

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Jan 10, 2022
@gaearon gaearon added Type: Bug and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Jan 10, 2022
@gaearon
Copy link
Collaborator

gaearon commented Jan 10, 2022

Seems like a bug. Have you tried debugging what's causing the difference in behavior? We haven't looked into this yet.

@jamesknelson
Copy link
Author

I haven't been able to figure it out myself. For now I've just disabled some of my app's functionality in safari as a workaround, although it would be nice to see a fix, as appending an iframe inside a ref callback is useful when dealing with the YouTube embed API.

@gaearon
Copy link
Collaborator

gaearon commented Jan 14, 2022

A bit of an investigation shows that we hit this in Safari. I don't know why.

if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
throw new Error('Should not already be working.');

I also don't know why the error end up silenced.

@gaearon
Copy link
Collaborator

gaearon commented Jan 14, 2022

Apparently calling appendChild with the iframe in Safari causes scheduled microtasks to run synchronously. This shows that our userland ref callback has caused flushSyncCallbacks to run during commit phase which React probably doesn't expect and can't handle.

Screenshot 2022-01-14 at 18 31 01

@gaearon
Copy link
Collaborator

gaearon commented Jan 14, 2022

#23111

@gaearon
Copy link
Collaborator

gaearon commented Jan 14, 2022

I also don't know why the error end up silenced.

likely this:

https://bugs.webkit.org/show_bug.cgi?id=224315

@gaearon
Copy link
Collaborator

gaearon commented Jan 14, 2022

Related issue in Preact preactjs/preact#2814

@gaearon
Copy link
Collaborator

gaearon commented Jan 18, 2022

Filed platform bug as https://bugs.webkit.org/show_bug.cgi?id=235322.

@gaearon
Copy link
Collaborator

gaearon commented Jan 19, 2022

Fixed in @next.

https://codesandbox.io/s/silly-curran-6kk1y

@dangkhoipro
Copy link

dangkhoipro commented Jun 15, 2022

I still encountered the bug in Safari iOS (14, 15.3.1) with a slightly different scenario.
Here is the simply reproduced codesandbox with React 18.1
https://codesandbox.io/s/pedantic-albattani-b3elrk?file=/src/App.js

@gaearon
Copy link
Collaborator

gaearon commented Jun 15, 2022

That one's fixed in 18.2 which was released yesterday.

@gaearon
Copy link
Collaborator

gaearon commented Jul 2, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser: Safari React 18 Bug reports, questions, and general feedback about React 18 Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants