Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to on-exit-leak-free v2 #1474

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,17 @@ const { createRequire } = require('module')
const getCallers = require('./caller')
const { join, isAbsolute } = require('path')
const sleep = require('atomic-sleep')

let onExit

/* istanbul ignore next */
if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
// This require MUST be top level otherwise the transport would
// not work from within Jest as it hijacks require.
onExit = require('on-exit-leak-free')
}

const onExit = require('on-exit-leak-free')
const ThreadStream = require('thread-stream')

function setupOnExit (stream) {
/* istanbul ignore next */
if (onExit) {
// This is leak free, it does not leave event handlers
onExit.register(stream, autoEnd)
// This is leak free, it does not leave event handlers
onExit.register(stream, autoEnd)
onExit.registerBeforeExit(stream, flush)

stream.on('close', function () {
onExit.unregister(stream)
})
} else {
const fn = autoEnd.bind(null, stream)
process.once('beforeExit', fn)
process.once('exit', fn)

stream.on('close', function () {
process.removeListener('beforeExit', fn)
process.removeListener('exit', fn)
})
}
stream.on('close', function () {
onExit.unregister(stream)
})
}

function buildStream (filename, workerData, workerOpts) {
Expand Down Expand Up @@ -86,6 +66,10 @@ function autoEnd (stream) {
})
}

function flush (stream) {
stream.flushSync()
}

function transport (fullOptions) {
const { pipeline, targets, levels, options = {}, worker = {}, caller = getCallers() } = fullOptions

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"dependencies": {
"atomic-sleep": "^1.0.0",
"fast-redact": "^3.1.1",
"on-exit-leak-free": "^1.0.0",
"on-exit-leak-free": "^2.1.0",
"pino-abstract-transport": "v0.5.0",
"pino-std-serializers": "^5.0.0",
"process-warning": "^2.0.0",
Expand Down
9 changes: 9 additions & 0 deletions test/exit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ test('sync false logs everything when calling flushSync', async ({ not }) => {
not(actual.match(/hello/), null)
not(actual.match(/world/), null)
})

test('transports exits gracefully when logging in exit', async ({ equal }) => {
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'transport-with-on-exit.js')])
child.stdout.resume()

const code = await once(child, 'close')

equal(code, 0)
})
12 changes: 12 additions & 0 deletions test/fixtures/transport-with-on-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
const pino = require('../..')
const log = pino({
transport: {
target: 'pino/file',
options: { destination: 1 }
}
})
log.info('hello world!')
process.on('exit', (code) => {
log.info('Exiting peacefully')
})