write ECONNRESET error and socket connection checking in node.js - javascript

I get this error occasionally when running my node.js script.
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: write ECONNRESET
at errnoException (net.js:768:11)
at Object.afterWrite (net.js:592:19)
What causes this error? I read someplace that this is caused due to attempting to write data to a closed socket. Is that right?
If that is the case, How do I check if a socket connection is active?
I found this SO question here, but no one had answered there.
And thirdly, is a simple try catch around the socket.write statement, enough to handle this error? Or does it emit error events which I must handle?

we've seen it with http-proxy and the issue was reported here: https://github.com/nodejitsu/node-http-proxy/issues/331
nodejs would throw if there "error" (and only "error") event is not handled.

Related

Getting this socket error in discord.js and can't debug it [duplicate]

This error randomly appears When leaving my discord bot online for too long, around 3-4 hours but sometimes the error occurs sooner and sometimes later. It's really bothering me.
events.js:188
throw err;
^
Error: Unhandled "error" event. ([object Object])
at Client.emit (events.js:186:19)
at WebSocketConnection.onError (D:\BasementMonster\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:374:17)
at WebSocket.onError (D:\BasementMonster\node_modules\ws\lib\event-target.js:128:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
at _receiver.cleanup (D:\BasementMonster\node_modules\ws\lib\websocket.js:211:14)
at Receiver.cleanup (D:\BasementMonster\node_modules\ws\lib\receiver.js:557:13)
at WebSocket.finalize (D:\BasementMonster\node_modules\ws\lib\websocket.js:206:20)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
I wrestled with this problem for a while in my own code. The main problem is that the trace is completely unhelpful, and that the error occurs so infrequently as to make "run it in the terminal and wait" a futile task. Eventually, I was able to figure out that the Discord.js client itself was throwing an error -- this wasn't mentioned in any of the documentation I read, so I had no handler for it -- and since this oopsie happens in the Discord.js package itself, there was no line in my code for it to point to in the trace.
What needs to exist in the code somewhere is something along the lines of
client.on('error', (err) => {
console.log(err.message)
});
This is adjusted, of course, for whatever your Client object is, and whatever you want the error handling to be.
This specific trace, as far as I can tell, comes from losing Internet connection, which is something I want to have a record of, so instead of console.log() I called my own custom function that writes the event out to a logfile with a timestamp.
Testing this by running an instance of the bot and killing my Internet connection revealed that not only did the logging function work, but that Discord.js automatically restored the bot's session after it came back. (Your mileage may vary.)
You need to add an error handling event, like this:client.on('error', console.error);

Problematic socket.parser.resume() error in node.js

This bug has been crashing my node.js server for the past one week. On average it seems to occur every 12 hours. Normally when an error occurs in node.js, node.js will tell you the line number and your source file that lead to the error. However, this error is so weird that it does not let me know where the error originates from in my source codes. I can't even find _http_server.js within the entire project and node modules. As such, I have been unable to pinpoint and eliminate the bug.
Without the stack trace module 'longjohn', I would get the following error message:
socket.parser.resume()
cannot read property resume of null (_http_server.js:442)
......
After I installed 'longjohn', I obtained the following error message:
TypeError: Cannot read property 'resume' of null
at socketOnDrain (_http_server.js:442:20)
at ServerResponse.updateOutgoingData [as _onPendingData] (_http_server.js:270:14)
at ServerResponse.OutgoingMessage._send (_http_outgoing.js:128:14)
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:470:16)
at ReadStream.ondata (_stream_readable.js:525:20)
at emitOne (events.js:77:13)
at ReadStream.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at ReadStream.Readable.push (_stream_readable.js:110:10)
at onread (fs.js:1744:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:576:17)
---------------------------------------------
at ReadStream.Readable.on (_stream_readable.js:655:33)
at ReadStream.Readable.pipe (_stream_readable.js:522:7)
at SendStream.stream (C:\XXXX\node_modules\express\node_modules\send\lib\send.js:533:10)
at SendStream.send (C:\XXXX\node_modules\express\node_modules\send\lib\send.js:480:8)
at C:\XXXX\node_modules\express\node_modules\send\lib\send.js:392:10
at FSReqWrap.oncomplete (fs.js:82:15)
worker 1760 died
It is worthwhile mentioning that I am using socket.io. I am also using an external service called Global Counter that displays the number of online users. I am running setInterval() in one of the workers to remove expired records.
Could someone tell me the meaning of these error messages, how the error occurs, and how I should solve it?
according to https://github.com/nodejs/node/issues/3508, you may fix this problem after upgrading nodejs to v4.2.3+ or v5.1.1+

Javascript error event using meteor starter files

I'm building an app that uses the Meteor starter files and sometimes get this error which I think is related to Node.js:
the error is:
events.js:72
throw er; // Unhandled 'error' event
Error: write EOF
at errnoException (net.js:905:11)
at Object.afterWrite (net.js:721:19)
I wanted to check if anyone has ever come across it before and if you have and recommendations on how to fix it?
Any assistance would be appreciated.
Thank you.

How can I catch an EHOSTDOWN socket error when creating an http request with iojs?

I have a simple iojs http server which communicates with another http backend on my development machine. Now my ip has changed and the backend request won't work due to a wrong ip. I have (or at least thought so) error management in place however the server crashes in some situations due to an unhandled exception:
When doing 2 subsequent requests the first one "hangs" and then the second request crashes the server:
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect EHOSTDOWN 192.168.1.11:80 - Local (192.168.1.10:54125)
at Object.exports._errnoException (util.js:846:11)
at exports._exceptionWithHostPort (util.js:869:20)
at connect (net.js:840:14)
at lookupAndConnect (net.js:933:5)
at Socket.connect (net.js:902:5)
at Agent.exports.connect.exports.createConnection (net.js:61:35)
at Agent.createSocket (_http_agent.js:177:16)
at Agent.addRequest (_http_agent.js:147:23)
at new ClientRequest (_http_client.js:132:16)
at Object.exports.request (http.js:30:10)
The error in question won't trigger backRequest.on("error", errFn) and it is not a standard error as in function(err, response, body). It can't be catched using try...catch.
How can i catch and gracefully handle this error?
I have dugged a bit into net.js/http.js. When emitting a socket error manually:
backendRequest.on("socket", function(socket) {
socket.emit("error", "DIE!");
});
it is handled by the normal error handler. The EHOSTDOWN error however is not. The reason for this seems to be that the socket error handler is only installed on the next tick after Socket.connect() (see https://github.com/joyent/node/blob/master/lib/_http_client.js#L498).
"My" error however is triggered directly upon connect() (must have something to do with the first connection still trying to find the host).
I can gracefully catch that error using a custom Agent.createConnection:
var socket = net.createConnection(options);
// install error handler immediately
socket.on("error", function(err) {
console.log("AHA", err);
});
return socket;
however that seems very verbose and bulky. Is that truly the best way to do that? The error seems to be not that special and seems to be unhandled in the core libs. Why is that so and why is the socket error handler only installed upon nextTick?
How would you properly catch that error? Is the agent the correct way?
A "full" example to play with can be found here: http://pastebin.com/jaCUPaHX
As pointed out by the commenters: It was a bug https://github.com/nodejs/io.js/pull/2054

Occassional ECONNREFUSED errors

I just got started with nodejs and using it to create a server. Clients connect to it using socket.io, receive jobs to proceess, and send the results back to the nodejs server.
However the server will crash occassionally with the following error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:614:11)
at Object.afterConnect [as oncomplete] (net.js:605:18)
I have no idea what is causing this.
ECONNREFUSED means you tried to make a connection to another machine but your connection was refused - either no one was listening or a firewall blocked you.
I have seen this using http, but I think it could also happen using straight sockets.
You are probably struggling with node.js dying whenever the server you are calling refuses to connect. Try this:
process.on('uncaughtException', function (err) {
console.log(err);
});
It will just log the errors and keep your service up and running.

Categories

Resources