fix: route raw socket 'error' events through the errorHandler option - #373
Merged
Tony133 merged 3 commits intoJul 21, 2026
Merged
Conversation
The listener attached in handleUpgrade logged socket errors unconditionally at error level via fastify.log.error, so the errorHandler option could never see errors emitted on the established connection (e.g. ERR_STREAM_PREMATURE_CLOSE / ECONNRESET from unclean client disconnects) and applications had no supported seam to classify expected disconnect errors. Attach the listener in the route handler instead, where the fastify request and reply are in scope, and invoke errorHandler with the same (error, socket, request, reply) signature used for wsHandler failures. The socket still always has an 'error' listener attached before the user handler runs, preserving the crash protection the old listener provided. Closes fastify#371
'Should invoke the correct handler depending on the headers' asserted on the raw client's first 'data' chunk, assuming the 101 handshake response and the frame from socket.send() (two separate writes) always arrive coalesced. On CI (macOS arm64, Node 24) they arrived in separate segments, so the first chunk contained only the 101 headers and the match failed; the thrown assertion also skipped wsClient.end(), leaving teardown to wait out the 30s ws closeTimeout. Accumulate received data across 'data' events before asserting instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Tony133
approved these changes
Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Errors emitted on the established websocket (e.g.
ERR_STREAM_PREMATURE_CLOSE/ECONNRESETfrom a client disconnecting uncleanly) were caught by a listener hardcoded inhandleUpgradethat logged unconditionally viafastify.log.error, so theerrorHandlerplugin option could never see them and applications had no supported way to classify expected disconnect errors.This PR removes the hardcoded listener and attaches it in the route handler instead, where the fastify
request/replyare in scope, invokingerrorHandlerwith the same(error, socket, request, reply)signature already used when awsHandlerthrows or rejects. The listener is still attached before the user's handler runs, so the socket always has an'error'listener and the existing Node.js crash protection is preserved. The defaulterrorHandlerbehavior (log +socket.terminate()) is safe for an already-errored socket.Checklist
errorHandlersection updatedCloses #371