Handled 'Uncaught Exception' is still somehow crashing the app - node.js

Looking for ideas on how to debug an app that is crashing randomly without being caught by:
process.on('uncaughtException', function(err){});
I've also had no luck trying Winston (the log file remains empty) to catch whatever is causing the app to crash:
winston.handleExceptions(new winston.transports.File({ filename: 'exceptions.log' }))
What kind of program faults can even get past these?

It isn't pretty but the following was enough to do the job for me:
setInterval(function(){winston.logger.info(moment().format('YYYY-MM-DD HH:mm:ss.SSS')+' ' + process.memoryUsage().rss)}, dt_timer);
Also, one answer to the question regarding what type of program fault can crash a node app that is handling 'uncaughtException' - Memory Leaks!
Cheers.

Related

Nodejs debugging - auto generation of error message

I am new to nodejs (async) debugging. I would appreciate some help regarding error messages -
var l, m, opiton= {};
// option has actually been mis-spelt here as opiton
.....
// some more code
option.name= "new name";
Now, at the point of assignment: option.name= "new name";
nodejs server freezes without any indication, that it has encountered an error.
It would be great if nodejs could "auto generate" an error message, and say -
undefined object "option" in line 5178 or something similar.
Figuring out that option was actually mis-spelt as opiton takes a lot of time, especially in a large code base, with multiple callbacks per request. And it would be great, if that time can be saved.
Question - Is there a generic solution for locating run-time errors in nodejs servers?
(In a large server code base, adding a ton of logging is useful for running analytics in production environment, but not the most optimum solution for a test environment)
You can set an uncaught error handler:
process.on('uncaughtException', function (err) {
console.log('UncaughtException: message: ' + err.message);
console.log('UncaughtException: stack: ' + err.stack);
});
I would be very hesitant to use this pattern in production code since at this point the program is in an unknown state. It's best to exit at this point and (potentially) restart.

Is there a Node.js equivalent of Java UncaughtExceptionHandler?

I had some problematic code in a Node.js application which caused an error (manifested as strange output) but not a total crash. It displayed nothing in the console to indicate an error occurred, and I only identified the root cause by a lot of trial and error in commenting out lines selectively.
To save time in future, is there anything like Java's UncaughtExceptionHandler in Node that will catch anything that's causing errors and display them in the console so I can pinpoint bug(s) immediately?
Yes. You can listen for that event by doing this
process.on('uncaughtException', (err) => {
});
It will override the default behaviour of exiting.
Documentation

Socket.IO server hangs up

I am new to socket.io. So my socket.io server sometimws crashes giving this below error
timers.js:103
if (!process.listeners('uncaughtException').length) throw e;
^
Error: socket hang up
at createHangUpError (http.js:1360:15)
at ServerResponse.OutgoingMessage._writeRaw (http.js:507:26)
at ServerResponse.OutgoingMessage._send (http.js:476:15)
at ServerResponse.OutgoingMessage.write (http.js:749:16)
at XHRPolling.doWrite (E:\sitesroot\0\node_modules\socket.io\lib\transports\
xhr-polling.js:67:17)
at XHRPolling.HTTPPolling.write (E:\sitesroot\0\node_modules\socket.io\lib\t
ransports\http-polling.js:132:8)
at XHRPolling.Transport.packet (E:\sitesroot\0\node_modules\socket.io\lib\tr
ansport.js:515:15)
at Object.<anonymous> (E:\sitesroot\0\node_modules\socket.io\lib\transports\
http-polling.js:79:12)
at Timer.list.ontimeout (timers.js:101:19)
It doesnt show where or why the error is happening so pretty sure its nothing to do with the code i have written. Could be something with the transports? I dont have much knowledge on it. Any suggestions on how to stop it from crashing would be highly appreciated. Thanks
The problem is as #miktam stated.
To fix this you need to add an error listener to your code.
Add this code to your application:
//Error handler
process.on('uncaughtException', function (exception) {
// handle or ignore error
console.log(exception);
});
When ever there is an error it will console.log it instead of crashing it. I had the exact same problem and this fixed it.
Check this issue
Quoting Isaac Schlueter:
The good news is that you're no longer leaking memory. The bad news is
that you do indeed need to add error listeners to your objects.
In most apps, you can usually treat ECONNRESET as roughly the same as
a graceful close. Just make sure that you actually stop using that
socket, since it's now closed. However, it's not a graceful close,
since it's not in any sense "graceful". Prior to v0.8.20, node would
happily buffer all writes to reset sockets, causing memory explosion
death.
For us new to websocket, you might be missing some parameters that are required by socket.io library.
What you might be using is ws://localhost which works for Websocket library, but might need to use ws://localhost:3000/socket.io/?EIO=4&transport=websocket when using socket.io

node.js debugging with source line numbers

Similar questions has been asked, I went through 'how to debug node' threads, but
those are however either old or not about the problem i got.
Problem:
I'm writing some small tools in node.js stack - and my debugging experience is quite frustrating: when an exception is thrown, in many cases I get very annoying messages like the one here:
TypeError: Bad argument
wtf? it's neither verbose or useful - no source line number, no information in which file this exception was thrown.
Question:
How do I get my console to output usefull information when exceptions/errors are thrown and console.log function has something to say. would be great to have a simple console.log call where it actually puts a line number and maybe a file name where the message happens.
in nodejs i use this function to see error stack:
process.on('uncaughtException', function(err) {
console.log(err.stack);
})
Use the --stack option to see stack traces. Such as grunt task --stack

How to handle exceptions thrown in Isolates?

I'm experimenting with Dart and using the new streamSpawnFunction to create a new isolate.
I'm running my code in Dartium but i've noticed that if some kind of unrecoverable error occurs in the isolate i get no error message on the console. Because breakpoints in Isolate code are not working debugging is really painful.
The old Port based Isolate spawn function (spawnFunction) has a callback function for handling errors. I wonder why this is not available with streamSpawnFunction. Is there a new way to subscribe to the error events of an Isolate?
The missing functionality of streamSpawnFunction is just an oversight. I filed http://dartbug.com/9208 and I will try to fix it next week.
I'm not sure if it is a known problem that breakpoints don't work in isolates. I will let you file a bug-report (http://dartbug.com) so the devs can ask you questions and you are kept informed on the process.

Resources