diff --git a/lib/levels.js b/lib/levels.js index a9e0e555f..c4ebbdce2 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -24,7 +24,11 @@ const levelMethods = { const stream = this[streamSym] logFatal.call(this, ...args) if (typeof stream.flushSync === 'function') { - stream.flushSync() + try { + stream.flushSync() + } catch (e) { + // https://github.com/pinojs/pino/pull/740#discussion_r346788313 + } } }, error: genLog(levels.error), diff --git a/test/levels.test.js b/test/levels.test.js index 4643ad853..b7ac1cc80 100644 --- a/test/levels.test.js +++ b/test/levels.test.js @@ -417,3 +417,16 @@ test('fatal method sync-flushes the destination if sync flushing is available', instance.fatal('this is fatal') }) }) + +test('fatal method should call async when sync-flushing fails', ({ equal, fail, doesNotThrow, plan }) => { + plan(2) + const messages = [ + 'this is fatal 1' + ] + const stream = sink((result) => equal(result.msg, messages.shift())) + stream.flushSync = () => { throw new Error('Error') } + stream.flush = () => fail('flush should be called') + + const instance = pino(stream) + doesNotThrow(() => instance.fatal(messages[0])) +})