winston-cloudwatch
winston-cloudwatch copied to clipboard
Process may hang unless kthxbye is called twice
- A WinstonCloudWatch transport is configured to handle uncaught exceptions
- An unhandled exception occurs immediately before closing the logger via the kthxbye method. Here's one way to throw an unhandled exception:
setTimeout(()=>{throw new Error();}, 1);
- While closing the logger, Winston calls add() on the CloudWatch transport with the log entry for the unhandled exception, turning on the transport's background queue processor
- The process will hang instead of exiting due to the transport's queue processor
The method for avoiding the hang is:
- Call kthxbye() on the CloudWatch transport
- Close the logger in exactly the right way
- Call kthxbye() again on the CloudWatch transport
// This array should contain WinstonCloudWatch objects. Search issues herein for
// 'kthxbye' to locate code that finds these transports via winston.loggers
const transports = [];
// Flush CloudWatch transports; otherwise, the logger's finish handler will not fire
// if an error occurred earlier while sending data to CloudWatch (test this by
// disconnecting from the Internet)
await Promise.all(transports.map(transport=>new Promise(resolve => {
// @todo: Fix this when WinstonCloudWatch makes this an option
transport.flushTimeout = Date.now()+30000; // 30 seconds
transport.kthxbye(
error => {
if (error) console.error(error);
resolve();
});
})));
// Close loggers
await Promise.all(winston.loggers.map(logger => new Promise(resolve => {
logger.on('finish', ()=>{logger.close(); resolve();});
logger.end();
})));
// Flush CloudWatch transports again because unhandled exceptions are sent to
// transports while closing the loggers
await Promise.all(transports.map(transport=>new Promise(resolve => {
// @todo: Fix this when WinstonCloudWatch makes this an option
transport.flushTimeout = Date.now()+30000; // 30 seconds
transport.kthxbye(
error => {
if (error) console.error(error);
resolve();
});
})));
Thanks for the detailed report.
I will reopen this, and either improve our README or fix the bug.
See also https://github.com/winstonjs/winston/issues/1250#issuecomment-622969996
Just wanted to check whether a fix is applied. I am using 6.1.1 version