Skip to content

Commit

Permalink
Call .end() on thread-stream before exiting the process. (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Jan 10, 2022
1 parent bc3dd5d commit 8dceecd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function buildStream (filename, workerData, workerOpts) {
return
}
stream.flushSync()
stream.end()
}

return stream
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/to-file-transport-with-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const fs = require('fs')
const { once } = require('events')
const { Transform } = require('stream')

async function run (opts) {
if (!opts.destination) throw new Error('kaboom')
const stream = fs.createWriteStream(opts.destination)
await once(stream, 'open')
const t = new Transform({
transform (chunk, enc, cb) {
setImmediate(cb, null, chunk.toString().toUpperCase())
}
})
t.pipe(stream)
return t
}

module.exports = run
16 changes: 16 additions & 0 deletions test/fixtures/transport-exit-immediately-with-async-dest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const pino = require('../..')
const transport = pino.transport({
target: './to-file-transport-with-transform.js',
options: {
destination: process.argv[2]
}
})
const logger = pino(transport)

logger.info('Hello')

logger.info('World')

process.exit(0)
1 change: 1 addition & 0 deletions test/fixtures/transport-exit-immediately.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const transport = pino.transport({
const logger = pino(transport)

logger.info('Hello')

process.exit(0)
14 changes: 14 additions & 0 deletions test/transport/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,20 @@ test('log and exit before ready', async ({ not }) => {
not(strip(actual).match(/Hello/), null)
})

test('log and exit before ready with async dest', async ({ not }) => {
const destination = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-immediately-with-async-dest.js'), destination])

await once(child, 'exit')

const actual = await readFile(destination, 'utf8')
not(strip(actual).match(/HELLO/), null)
not(strip(actual).match(/WORLD/), null)
})

test('string integer destination', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-string-stdout.js')])
Expand Down

0 comments on commit 8dceecd

Please sign in to comment.