From dc53914e13b5436c75ecb1676609771c755789cb Mon Sep 17 00:00:00 2001 From: James Irvin Date: Wed, 14 Sep 2022 16:23:07 -0700 Subject: [PATCH] Fix 'ERR_STREAM_WRITE_AFTER_END' error in proxy 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()` --- .../react-dev-utils/WebpackDevServerUtils.js | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/react-dev-utils/WebpackDevServerUtils.js b/packages/react-dev-utils/WebpackDevServerUtils.js index d9eea34701c..764fdd273ac 100644 --- a/packages/react-dev-utils/WebpackDevServerUtils.js +++ b/packages/react-dev-utils/WebpackDevServerUtils.js @@ -285,17 +285,22 @@ function onProxyError(proxy) { if (res.writeHead && !res.headersSent) { res.writeHead(500); } - res.end( - 'Proxy error: Could not proxy request ' + - req.url + - ' from ' + - host + - ' to ' + - proxy + - ' (' + - err.code + - ').' - ); + + // In some cases `res` will already be finished. Trying to + // double end it can result in ERR_STREAM_WRITE_AFTER_END. + if (!res.finished) { + res.end( + 'Proxy error: Could not proxy request ' + + req.url + + ' from ' + + host + + ' to ' + + proxy + + ' (' + + err.code + + ').' + ); + } }; }