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?]: /_server request gets stuck with async code inside middleware #1165

Closed
2 tasks done
JesseVelden opened this issue Dec 21, 2023 · 6 comments
Closed
2 tasks done
Labels
bug Something isn't working vinxi related to vinxi

Comments

@JesseVelden
Copy link

JesseVelden commented Dec 21, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

With the new @solid/start, the /_server request gets stuck and shows (pending) as status in Chrome's Network Inspector when using async code inside the middleware. When not using async code, it works fine.

Expected behavior 🤔

The request resolves

Steps to reproduce 🕹

Steps:

  1. Go to https://codesandbox.io/p/devbox/solid-start-middleware-for-server-with-promise-session-not-working-6kz3tm?file=%2Fsrc%2Fmiddleware.ts%3A31%2C57
  2. Run the dev server
  3. Click on Generate New button in the preview window, and see the that the code actually executes the console.log but the request hangs as can be seen Network Inspector
  4. If you uncomment the async code inside the middleware.ts, you can see it does work as expected

Context 🔦

Middleware is needed for checking if a user is logged in. It is uses async functions to do that

Your environment 🌎

Node.js v20.9.0

    "@solidjs/router": "^0.10.5",
    "@solidjs/start": "^0.4.1",
    "solid-js": "^1.8.7",
    "vinxi": "0.0.54"
@JesseVelden JesseVelden added the bug Something isn't working label Dec 21, 2023
@nksaraf
Copy link
Member

nksaraf commented Dec 22, 2023

Yup very peculiar. Will have to debug this carefully.

@nksaraf nksaraf added the vinxi related to vinxi label Dec 27, 2023
@edivados
Copy link
Contributor

edivados commented Jan 1, 2024

@nksaraf I think the issue is this.

The middleware in front of the server function creats a FetchEvent which calls toWebRequest creating a ReadableStream from the request body.

function wrapRequestMiddleware(onRequest: RequestMiddleware) {
return async (h3Event: H3Event) => {
const fetchEvent = getFetchEvent(h3Event);
const response = await onRequest(fetchEvent);

If I understand it correctly the unreleated await gives node a chance to read chunks and later we get stuck trying to read a request body that has already been read.

parsed = fromJSON(await readBody(event), {
plugins: [

I verified this by replacing toWebRequest with a Proxy and not reading the body.

export function createFetchEvent(event: H3Event<EventHandlerRequest>): FetchEvent {
return new Proxy(
{
request: toWebRequest(event),
clientAddress: getRequestIP(event),
locals: {},

@nksaraf
Copy link
Member

nksaraf commented Jan 1, 2024

Ohh I think I faced something similar here: unjs/h3#570

Okay I think we should do the proxy approach anyway so that nothing is called lazily.

@edivados
Copy link
Contributor

edivados commented Jan 3, 2024

ah interessting... you already ran into that.

So we would proxy the WebRequest and make sure the body is not read eagerly right?
While testing I just left url in tact because something was relying on it.

ryansolid added a commit that referenced this issue Jan 5, 2024
@nksaraf
Copy link
Member

nksaraf commented Jan 6, 2024

I wonder if we should push this upwards to vinxi's server utils as well

@nksaraf
Copy link
Member

nksaraf commented Jan 6, 2024

Pulling this upstream into vinxi: nksaraf/vinxi#101

edivados added a commit to edivados/solid-start that referenced this issue Jan 8, 2024
edivados added a commit to edivados/solid-start that referenced this issue Jan 8, 2024
edivados added a commit to edivados/solid-start that referenced this issue Jan 8, 2024
edivados added a commit to edivados/solid-start that referenced this issue Jan 8, 2024
edivados added a commit to edivados/solid-start that referenced this issue Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vinxi related to vinxi
Projects
None yet
Development

No branches or pull requests

3 participants