Home:ALL Converter>Async code inside node uncaughtException handler

Async code inside node uncaughtException handler

Ask Time:2018-08-03T02:50:36         Author:d512

Json Formatter

I have a node app (version 8.11.1 using Typescript 2.8.1) that catches uncaught exceptions using the uncaughtException hook like this:

process.on('uncaughtException', (err) => {
    await sendEmail(recipient, subject, body);
});

I'm calling an asynchronous method inside the handler to send out an email and the code isn't working. It appears that the node process dies before the async call can finish and I never receive the email.

It looks like you may not be able to successfully use async methods inside this handler. The documentation doesn't say that outright but it implies it stating

The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated resources

I'm not trying resume operation or do anything funky. All I want to do is send out and email stating that the system crashed. I am not able to find any libraries that will synchronously send emails so I'm at a bit of a loss on how to handle this.

I've seen one suggestion to synchronously write the data to a file (or database) and have some outside processing polling for the existence of that file and sending the email if it exists. I suppose that would work but it's pretty hacky. Any better solutions?

Update:

Okay well after running some more tests it looks like you can actually run async code from inside the uncaughtException handler just fine. The following works:

const mailer = require('nodemailer');

process.on('uncaughtException', async err => {
    const transport = mailer.createTransport({
        service: 'gmail',
        auth: {
            user: '[email protected]',
            pass: 'abc'
        }
    });

    const mailOptions = {
        from: '[email protected]',
        to: '[email protected]',
        subject: 'hi',
        text: 'there'
    };

    transport.sendMail(mailOptions, (error, info) => {
        if (error) {
            console.log(error);
        }
        else {
            console.log(info);
        }
    });
});

throw new Error('boom');

The above code works fine as a standalone app, but if I copy it into my codebase, it doesn't work. The code executes but I don't get an email (presumably the app dies before it can finish sending). So there must be something else going on in my environment that is preventing it from working. I'll keep digging.

Author:d512,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51660355/async-code-inside-node-uncaughtexception-handler
yy