sitegui / nodejs-websocket

A node.js module for websocket server and client
MIT License
736 stars 155 forks source link

Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) Emitted 'error' event at: at Socket #61

Open df257 opened 4 years ago

df257 commented 4 years ago

if not have listen 'error', when I colse page, server will down. image image

littleQing commented 3 years ago

I also encountered the same problem. Have you solved it ?

mepiazza commented 1 year ago

I encountered the same error for the same reason:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Connection instance at:
    at Socket.<anonymous> (C:\Users\Mike\WebstormProjects\masc_webserver\node_modules\nodejs-websocket\Connection.js:70:8)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read'
}

There are line number differences and a module name difference but that is probably due to the time difference between the previous comments and mine. Testing for this error I found that closing the browser window, in both Chrome and Edge, does trip my "close" event handler, however, it immediately generates the above error as well even though my code is within a try/catch block. Firefox didn't generate a "close" or react in any way so because of that I had disallow the use of Firefox for this specific application. (It's a closed environment we can limit what is available to be used) I traced through the stack and found the line in the events.js module as well as the line in this module Connection.js. I had expected to see a fairly straightforward error handler in events.js but, unfortunately, what I did find was overly cryptic and I didn't have the time to waste try to figure out what it was doing.

I dropped back to Connection.js and modified the following code:

From this:

    socket.on('error', function (err) {
        that.emit('error', err)
    })

To this:

    socket.on('error', function (err) {
        // Filter out webpage disconnect ECONNRESET 'error'
        // that crashes the application server process
        if (err.toString() != "Error: read ECONNRESET") {
            that.emit('error', err)
        }
    })

First of all, I don't understand why this ECONNRESET is NOT already being handled by now. They say the caller needs to handle with a try/catch code block, well my code already has that and it doesn't work. Closing the active web page by clicking on the "X" in the upper right hand corner of the window frame is a pretty standard User web interaction which should NOT be causing the nodejs web server apps to crash. This may not be the proper "fix" but in all our testing, prior to the change, there were no other issues with ECONNRESET so we made the decision to filter and ignore it in the localized app copy of Connection.js located in the app's node_modules directory and document the fix for a "next time".