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

fix(server-runtime: fix request signal not aborting after request is garbage collected #10030

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

jvaill
Copy link

@jvaill jvaill commented Sep 26, 2024

Closes: #9976 (comment) Edit: There appears to be a different bug with signals not working at all in production.

Update (Sep 26)

In development, it appears that globals aren't being automatically installed and Request with node ends up coming from undici. undici currently has a bug where parent signals aren't properly followed: nodejs/node#55428

In production, globals are installed and Request comes from web-fetch. This version of Request does not have the above issue.

It seems like globals should be installed in development as well? Manually invoking installGlobals() changes the Request object and solves this issue.

Original:

Testing Strategy:

Add remix-utils and the following routes. Start the dev server and open /time in a bunch of different tabs. Refresh the tabs randomly, close them, create new ones, etc, and eventually you'll trigger the GC in the right way where SSE connection closed ${number of open conns} will be logged to the console with the number of open connections being greater than the number of open tabs. A good way to see if you've triggered this is just to close all of them and see if the logged connection count is greater than 0.

routes/sse.time.ts

import { LoaderFunctionArgs } from "@remix-run/node"
import { eventStream } from "remix-utils/sse/server"

let conns = 0;

export async function loader({ request }: LoaderFunctionArgs) {
  conns++;
  return eventStream(request.signal, function setup(send) {
    const timer = setInterval(() => {
      send({ event: "time", data: new Date().toLocaleTimeString() })
    }, 1000)

    return function clear() {
      conns--;
      console.log("SSE connection closed", conns);
      clearInterval(timer)
    }
  })

routes/time.tsx

import { useEventSource } from "remix-utils/sse/react"

export default Route(){
  let time = useEventSource("/sse/time", { event: "time" })
  if (!time) {
    time = new Date().toLocaleTimeString()
  }

return <p>{time}</p>

Copy link

changeset-bot bot commented Sep 26, 2024

🦋 Changeset detected

Latest commit: aaab009

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@remix-run/server-runtime Patch
@remix-run/cloudflare Patch
@remix-run/deno Patch
@remix-run/dev Patch
@remix-run/node Patch
@remix-run/react Patch
@remix-run/testing Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/architect Patch
@remix-run/express Patch
@remix-run/serve Patch
create-remix Patch
remix Patch
@remix-run/css-bundle Patch
@remix-run/eslint-config Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Sep 26, 2024

Hi @jvaill,

Welcome, and thank you for contributing to Remix!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run.

Thanks!

- The Remix team

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Sep 26, 2024

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@jvaill
Copy link
Author

jvaill commented Sep 26, 2024

Fyi - the underlying problem appears to be a bug in undici:

nodejs/node#55428

@MichaelDeBoey MichaelDeBoey changed the title Fix request signal not aborting after request is garbage collected fix(server-runtime: fix request signal not aborting after request is garbage collected Sep 29, 2024
@brophdawg11
Copy link
Contributor

It seems like globals should be installed in development as well? Manually invoking installGlobals() changes the Request object and solves this issue.

FWIW we are deprecating the @remix-run/web-fetch package in favor of using the built-in fetch on Node 20 and above - something you can opt-into via installGlobals({ nativeFetch: true }) and is usually done when you opt into Single Fetch (see https://remix.run/docs/en/main/guides/single-fetch#breaking-changes).

For now, it looks like there is some traction on the node/undici end so do we think they'll be getting a fix out anytime soon? It would be nice not to have to put this type of workaround/hack into the Remix codebase to fix a bug in undici/node...

@jvaill
Copy link
Author

jvaill commented Oct 30, 2024

@brophdawg11 Another update on this - (IIRC, been a few weeks) at first I thought this was going to fix signals in production, but it seems like that's an entirely different bug altogether. When signals are working (i.e., in development), this fixes signals sporadically not being aborted due to GC.

I gave up on getting signals to work and ended up switching from SSE to websockets 😅

@Eiley2
Copy link

Eiley2 commented Oct 30, 2024

@brophdawg11 Another update on this - (IIRC, been a few weeks) at first I thought this was going to fix signals in production, but it seems like that's an entirely different bug altogether. When signals are working (i.e., in development), this fixes signals sporadically not being aborted due to GC.

I gave up on getting signals to work and ended up switching from SSE to websockets 😅

Did you implement your own server with Express and Remix? Or what did you use? Considering to do the same tbh

@mkromann
Copy link

mkromann commented Nov 1, 2024

Is it correct to say that SSEs are not working right now in Remix? I have been trying the last few days to find a workaround to this but been unable. We are starting up a new project and this is a curveball. It would be sad to go another route than Remix!

These issues are also reported here:
sergiodxa/remix-utils#317
dan-cooke/remix-sse#27

@mkromann
Copy link

mkromann commented Nov 8, 2024

@brophdawg11 Have you considered the path forward regarding this issue? Is this a waiting thing, or something you can/will patch?

@jvaill
Copy link
Author

jvaill commented Nov 11, 2024

@brophdawg11 Another update on this - (IIRC, been a few weeks) at first I thought this was going to fix signals in production, but it seems like that's an entirely different bug altogether. When signals are working (i.e., in development), this fixes signals sporadically not being aborted due to GC.
I gave up on getting signals to work and ended up switching from SSE to websockets 😅

Did you implement your own server with Express and Remix? Or what did you use? Considering to do the same tbh

I ended up adding a separate service to handle websockets. Basically, it uses createServer (node:http) and upgrade the connection to a websocket via WebSocketServer (ws). I use redis pub/sub to trigger messages from the remix app that get picked up by the service and proxied to the client. Might be a bit overly complex depending on your needs.

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.

5 participants