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 'ERR_STREAM_WRITE_AFTER_END' error in proxy #12731

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

Conversation

jamirvin
Copy link

@jamirvin jamirvin commented Sep 14, 2022

In my project when using the CRA proxy configuration my team has started to hit transient ERR_STREAM_WRITE_AFTER_END errors that crash the dev server. This would result in an stack trace like the following:

[wds] Proxy error: Could not proxy request /api/graphql from localhost:3000 to http://localhost:8080/.
[wds] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).
[wds]
[wds] events.js:377
[wds]       throw er; // Unhandled 'error' event
[wds]       ^
[wds]
[wds] Error [ERR_STREAM_WRITE_AFTER_END]: write after end
[wds]     at new NodeError (internal/errors.js:322:7)
[wds]     at writeAfterEnd (_http_outgoing.js:694:15)
[wds]     at ServerResponse.end (_http_outgoing.js:815:7)
[wds]     at ServerResponse.end (project_root/node_modules/compression/index.js:107:21)
[wds]     at ProxyServer.<anonymous> (project_root/node_modules/react-dev-utils/WebpackDevServerUtils.js:288:9)
[wds]     at ProxyServer.emit (project_root/http-proxy/node_modules/eventemitter3/index.js:210:27)
[wds]     at ClientRequest.proxyError (project_root/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:165:18)
[wds]     at ClientRequest.emit (events.js:400:28)
[wds]     at TLSSocket.socketErrorListener (_http_client.js:475:9)
[wds]     at TLSSocket.emit (events.js:400:28)
[wds] Emitted 'error' event on ServerResponse instance at:
[wds]     at writeAfterEndNT (_http_outgoing.js:753:7)
[wds]     at processTicksAndRejections (internal/process/task_queues.js:83:21) {
[wds]   code: 'ERR_STREAM_WRITE_AFTER_END'
[wds] }

I've not been able to determine what is ending the request before it gets to the 'onError' function (it seems to correspond with making changes that trigger a reload at the same time as a network error), but I've confirmed that reproducing the CRA proxy using setupProxy.js with this change resolves the crash.

In some cases the proxy `onError` function can be called
with a response object that has already been ended. Trying
to end it with a message will result in a Node error that
crashes the server.

Fix this by checking if the stream has been finished before
calling `.end()`
@jamirvin jamirvin requested a review from mrmckeb as a code owner September 14, 2022 23:43
@facebook-github-bot
Copy link

Hi @jamirvin!

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!

@facebook-github-bot
Copy link

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

@facebook-github-bot
Copy link

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

@tsiory
Copy link

tsiory commented Jul 7, 2023

Hello, any news about how to fix this issues ?

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.

3 participants