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

SSE close event is not being forwarded to the server when using Vite as Reverse Proxy #13522

Closed
7 tasks done
MajedMuhammad opened this issue Jun 14, 2023 · 4 comments · Fixed by #13578
Closed
7 tasks done
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@MajedMuhammad
Copy link
Contributor

Describe the bug

This is a duplicate of #12157. I am opening this bug due to it is closed and can't proceed with it.
As the title: SSE close event is not being forwarded to the server when using Vite as Reverse Proxy.
In the dev server and in the preview server, both are not forwarding the close event to the server.
After many hours of debugging, and traffic inspection, after calling sse.close(), the connection is closed between the client and the proxy, but still open between the proxy and the server!

Reproduction

#12157

Steps to reproduce

No response

System Info

Ubuntu.

Used Package Manager

pnpm

Logs

No response

Validations

@MajedMuhammad MajedMuhammad changed the title SSE close event is not being forwarded to the server when using Vite as Reverse Proxy. SSE close event is not being forwarded to the server when using Vite as Reverse Proxy Jun 14, 2023
@sapphi-red
Copy link
Member

I found this workaround.
https://github.com/chimurai/http-proxy-middleware/blob/cd58f962aec22c925b7df5140502978da8f87d5f/src/plugins/default/debug-proxy-errors-plugin.ts#L25-L37
If you try this out by configure option of server.proxy and works for you, I think we can include this by default.

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) labels Jun 15, 2023
@MajedMuhammad
Copy link
Contributor Author

MajedMuhammad commented Jun 15, 2023

res.on('close', () => { console.log(”closed”); })

I have tried this to listen to close events from the client using both res and req (i.e., proxyReq and proxyRes events). It is not listening to close events from the client when using EventSource, but it works when the connection is closed by the server. I will double-check for you and confirm.

@MajedMuhammad
Copy link
Contributor Author

I confirm that Vite proxy is not listening to close events when emitted from EventSource.

@MajedMuhammad
Copy link
Contributor Author

Important Update:
The solution provided is working fine.
I mistakenly confirmed because I was using it like:

proxy.on("proxyReq", (req) => {
    req.on("close", () => {
        proxy.destroy();
    });
});

It will never work due to the first parameter is just the Client Request, not the actual streaming request.
It works if three parameters are passed:

proxy.on("proxyReq", (proxyReq, req, res) => {
    res.on("close", () => {
        if (!res.writableEnded) { # This if statement to check if the server still writing
            proxyReq.destroy();
        }
    });
});

I think it will be better as you mentioned if it is enabled by default. Whenever the proxy gets a close event, then the related proxy should be destroyed.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants