GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix streaming SSR in `react-dom/server.browser` by jplhomer · Pull Request #22889 · react/react

Conversation

@jplhomer

@jplhomer jplhomer commented Dec 9, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary

In #22772, I document an issue we've discovered while using react-dom/server.browser in both a web browser and a Workers runtime (like Cloudflare Workers or Shopify Oxygen).

The root of the problem appears to be related to the fact that the reader of a ReadableStream can call pull whenever it pleases, which triggers flushCompletedQueues. This conflicts with Suspense boundaries which enqueue additional calls to flushCompletedQueues via pingTask.

This problem has not yet surfaced in existing fixtures (fizz-ssr-browser) because await response.blob() is called, which results in pull being called exactly once more after the stream is locked.

This PR adds a new status to Request called FLUSHING which gets set at the top of flushCompletedQueues and returned back to OPEN in the finally block.

I have no idea if this is the correct solution, but it seems to solve the problem in the test fixture I've provided in this PR.

How did you test this change?

To replicate this problem, comment out the changes made to flushCompletedQueues in this PR and run:

yarn build react/index,react-dom --type=UMD
open fixtures/fizz-ssr-browser-streaming/index.html

This should open a page in your web browser which results in the following:

Screen Shot 2021-12-08 at 9 47 41 PM

You can get a slightly different error if you add an additional Suspense boundary! Uncomment the second boundary in the fixtures/fizz-ssr-browser-streaming/index.html file and reload the page:

Screen Shot 2021-12-08 at 9 52 29 PM

To resolve this issue, you can uncomment the changes made in this PR to flushCompletedQueues and run this again:

yarn build react/index,react-dom --type=UMD
open fixtures/fizz-ssr-browser-streaming/index.html

Now you should see the streaming resolve fine without competing calls to flushCompletedQueues (for a single and for multiple Suspense boundaries):

Screen Shot 2021-12-08 at 9 57 20 PM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jplhomer added 6 commits December 1, 2021 12:28
@jplhomer
Add fizz-ssr-browser-streaming fixture to repro errors
6caff6e
@jplhomer
Update title
f77ae81
@jplhomer
fix: Prevent infinite spinning in fixture
b7040b6
@jplhomer
fix: prevent concurrent flushing issues by tracking flushing status.
4c5d67c
RE: "The stream is not in a state that permits close" in `renderToReadableStream` react#22772
@jplhomer
Merge remote-tracking branch 'origin/main' into jl-fizz-ssr-streaming…
33be3eb
…-browser
@jplhomer
fix: wrap fixture in try/catch for more understandable repro
1eb23c3
@facebook-github-bot

facebook-github-bot commented Dec 9, 2021

Copy link
Copy Markdown
Contributor

Hi @jplhomer!

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 [email protected]. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sizebot

sizebot commented Dec 9, 2021

Copy link
Copy Markdown

Comparing: f2a59df...1eb23c3

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 = 129.54 kB 129.54 kB = 41.51 kB 41.51 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 134.68 kB 134.68 kB = 43.02 kB 43.02 kB
facebook-www/ReactDOM-prod.classic.js = 427.78 kB 427.78 kB = 78.55 kB 78.55 kB
facebook-www/ReactDOM-prod.modern.js = 417.47 kB 417.47 kB = 77.07 kB 77.07 kB
facebook-www/ReactDOMForked-prod.classic.js = 427.78 kB 427.78 kB = 78.55 kB 78.55 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-www/ReactDOMServer-prod.modern.js +0.40% 76.22 kB 76.52 kB +0.17% 16.30 kB 16.33 kB
oss-stable-semver/react-server/cjs/react-server.production.min.js +0.20% 19.65 kB 19.69 kB +0.19% 6.86 kB 6.87 kB
oss-stable/react-server/cjs/react-server.production.min.js +0.20% 19.65 kB 19.69 kB +0.19% 6.86 kB 6.87 kB
oss-experimental/react-server/cjs/react-server.production.min.js +0.20% 19.83 kB 19.87 kB +0.19% 6.91 kB 6.93 kB

Generated by 🚫 dangerJS against 1eb23c3

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jplhomer

jplhomer commented Dec 9, 2021

Copy link
Copy Markdown
Contributor Author

FYI: this PR looks super similar, so I decided to test my fixture against it: #22726

I'm still getting the same error, so unfortunately that wasn't the fix, either.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@facebook-github-bot facebook-github-bot added the CLA Signed label Dec 9, 2021
@facebook-github-bot

facebook-github-bot commented Dec 9, 2021

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bvaughn
bvaughn requested a review from sebmarkbage December 9, 2021 14:47
@sebmarkbage

sebmarkbage commented Dec 9, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This seems similar to the hack I removed in #22446

I was able to work around that specific issue in another way. Ideally we should try working around it here too.

It's a bit annoying because we'd like to not have these hacks - and especially in the core implementation rather than a specific runtime.

It's extra annoying because the browser implementation doesn't follow the spec here and so it's not covered by the non-browser test. Is this also a case where Workers aren't following the spec?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jplhomer

jplhomer commented Dec 9, 2021

Copy link
Copy Markdown
Contributor Author

@sebmarkbage it looks like the workaround in the PR you linked was to prevent calling startFlowing until stream.locked was true, but it's already behind stream.locked in FizzServer.

Could you share what specific thing the browser implementation isn't following in the spec here? Happy to dig in further and maybe add a workaround in the *Browser config instead.

So far, we've observed this behavior in:

  • The browser
  • Shopify's Oxygen runtime (response.body.getReader reading chunks into v8go)
  • Node's new Web Streams API
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jplhomer jplhomer mentioned this pull request Jan 19, 2022
[Flight] add failing test to demonstrate bug in ReadableStream implementation #23140
Closed
sebmarkbage added a commit to sebmarkbage/react that referenced this pull request Feb 23, 2022
@sebmarkbage
Re-add reentrancy avoidance
dbd4eac
I removed the equivalency of this in react#22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better.

I believe this fixes react#22772.

This includes the test from react#22889 but should ideally have one for Fizz.
sebmarkbage added a commit to sebmarkbage/react that referenced this pull request Feb 23, 2022
@sebmarkbage
Re-add reentrancy avoidance
8ecc536
I removed the equivalency of this in react#22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better
now.

The spec is not actually recursive. It won't call pull again inside of a
pull. It might not call it inside startWork neither which the original
issue avoided. However, it will call pull if you enqueue to the controller
without filling up the desired size outside any call.

We could avoid that by returning a Promise from pull that we wait to
resolve until we've performed all our pending tasks. That would be the
more idiomatic solution. That's a bit more involved but since we know
understand it, we can readd the reentrancy hack since we have an easy place
to detect it. If anything, it should probably throw or log here otherwise.

I believe this fixes react#22772.

This includes the test from react#22889 but should ideally have one for Fizz.
@sebmarkbage sebmarkbage mentioned this pull request Feb 23, 2022
Re-add reentrancy avoidance #23342
Merged
sebmarkbage added a commit that referenced this pull request Feb 23, 2022
@sebmarkbage @jplhomer
Re-add reentrancy avoidance (#23342)
2c693b2
* tests: add failing test to demonstrate bug in ReadableStream implementation

* Re-add reentrancy avoidance

I removed the equivalency of this in #22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better
now.

The spec is not actually recursive. It won't call pull again inside of a
pull. It might not call it inside startWork neither which the original
issue avoided. However, it will call pull if you enqueue to the controller
without filling up the desired size outside any call.

We could avoid that by returning a Promise from pull that we wait to
resolve until we've performed all our pending tasks. That would be the
more idiomatic solution. That's a bit more involved but since we know
understand it, we can readd the reentrancy hack since we have an easy place
to detect it. If anything, it should probably throw or log here otherwise.

I believe this fixes #22772.

This includes the test from #22889 but should ideally have one for Fizz.

Co-authored-by: Josh Larson <[email protected]>
@jplhomer

jplhomer commented Feb 23, 2022

Copy link
Copy Markdown
Contributor Author

Fixed by #23342

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jplhomer jplhomer closed this Feb 23, 2022
@jplhomer
jplhomer deleted the jl-fizz-ssr-streaming-browser branch February 23, 2022 16:33
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
@sebmarkbage @jplhomer @zhengjitf
Re-add reentrancy avoidance (react#23342)
16a5c9d
* tests: add failing test to demonstrate bug in ReadableStream implementation

* Re-add reentrancy avoidance

I removed the equivalency of this in react#22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better
now.

The spec is not actually recursive. It won't call pull again inside of a
pull. It might not call it inside startWork neither which the original
issue avoided. However, it will call pull if you enqueue to the controller
without filling up the desired size outside any call.

We could avoid that by returning a Promise from pull that we wait to
resolve until we've performed all our pending tasks. That would be the
more idiomatic solution. That's a bit more involved but since we know
understand it, we can readd the reentrancy hack since we have an easy place
to detect it. If anything, it should probably throw or log here otherwise.

I believe this fixes react#22772.

This includes the test from react#22889 but should ideally have one for Fizz.

Co-authored-by: Josh Larson <[email protected]>
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
@sebmarkbage @jplhomer
Re-add reentrancy avoidance (react#23342)
4104c78
* tests: add failing test to demonstrate bug in ReadableStream implementation

* Re-add reentrancy avoidance

I removed the equivalency of this in react#22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better
now.

The spec is not actually recursive. It won't call pull again inside of a
pull. It might not call it inside startWork neither which the original
issue avoided. However, it will call pull if you enqueue to the controller
without filling up the desired size outside any call.

We could avoid that by returning a Promise from pull that we wait to
resolve until we've performed all our pending tasks. That would be the
more idiomatic solution. That's a bit more involved but since we know
understand it, we can readd the reentrancy hack since we have an easy place
to detect it. If anything, it should probably throw or log here otherwise.

I believe this fixes react#22772.

This includes the test from react#22889 but should ideally have one for Fizz.

Co-authored-by: Josh Larson <[email protected]>
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@sebmarkbage @jplhomer
Re-add reentrancy avoidance (react#23342)
4b4a30f
* tests: add failing test to demonstrate bug in ReadableStream implementation

* Re-add reentrancy avoidance

I removed the equivalency of this in react#22446. However, I didn't fully
understand the intended semantics of the spec but I understand this better
now.

The spec is not actually recursive. It won't call pull again inside of a
pull. It might not call it inside startWork neither which the original
issue avoided. However, it will call pull if you enqueue to the controller
without filling up the desired size outside any call.

We could avoid that by returning a Promise from pull that we wait to
resolve until we've performed all our pending tasks. That would be the
more idiomatic solution. That's a bit more involved but since we know
understand it, we can readd the reentrancy hack since we have an easy place
to detect it. If anything, it should probably throw or log here otherwise.

I believe this fixes react#22772.

This includes the test from react#22889 but should ideally have one for Fizz.

Co-authored-by: Josh Larson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sebmarkbage sebmarkbage Awaiting requested review from sebmarkbage

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA Signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@jplhomer @facebook-github-bot @sizebot @sebmarkbage
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.