-
Notifications
You must be signed in to change notification settings - Fork 23
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(node): destroy socket #140
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #140 +/- ##
=======================================
Coverage ? 76.59%
=======================================
Files ? 9
Lines ? 735
Branches ? 147
=======================================
Hits ? 563
Misses ? 170
Partials ? 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@pi0 Can you review this when you get a chance please good Sir? |
Sure :) I have to double check |
Error: read ECONNRESET
on Windows when sending response
src/adapters/node.ts
Outdated
@@ -214,7 +214,18 @@ async function sendResponse(socket: Duplex, res: Response) { | |||
socket.write(chunk); | |||
} | |||
} | |||
if (socket.writable) { | |||
socket.end(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still little worried that we are not using end callback on socket.
This way, handleUpgrade
promise is resolved even before socket closes.
Do you have a stable reproduction that all this changes fixes it? (asking cause I tried closing socket on windows by enabling this condition => pnpm play:node
. i can't see errors on windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the code so that we destroy the socket in the socket.end() callback.
I've also updated the reproduction from the issue with the solution that fixes the issue https://github.com/eltigerchino/crossws-windows-socket-error-repro . There's a line you can uncomment to destroy the socket to fix it.
Strangely, I can reproduce this error on Windows by running pnpm play:node
then sending a WebSocket request through Hoppscotch at the URL http://localhost:3001/?unauthorized
.
PS C:\git-windows\crossws> pnpm play:node
> [email protected] play:node C:\git-windows\crossws
> jiti test/fixture/node.ts
Server running at http://localhost:3001/
node:events:495
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
Node.js v18.20.5
ELIFECYCLE Command failed with exit code 1.
fixes #139
This PR is an attempt to to avoid the error that's occurring on Windows from calling
socket.write(chunk)
by destroying the socket stream after all data has been written. It copies theSocket.destroySoon()
implementation from Node.js.Also worth noting that the
ws
examples usesocket.destroy()
to end their upgrade requests:And the duplex stream/socket class uses the
Writable.destroy()
implementation:https://nodejs.org/api/stream.html#writabledestroyerror
EDIT: seems like
ws
also callssocket.destroy()
when sending their responses too, according to their implementation