Skip to content

Commit

Permalink
Do not assume process.stdout has a file descriptor (#1503)
Browse files Browse the repository at this point in the history
* Do not assume process.stdout has a file descriptor

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jul 24, 2022
1 parent 90e728e commit 29911ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/tools.js
Expand Up @@ -305,7 +305,9 @@ function createArgsNormalizer (defaultOptions) {
if (enabled === false) opts.level = 'silent'
if (!stream) {
if (!hasBeenTampered(process.stdout)) {
stream = buildSafeSonicBoom({ fd: process.stdout.fd })
// If process.stdout.fd is undefined, it means that we are running
// in a worker thread. Let's assume we are logging to file descriptor 1.
stream = buildSafeSonicBoom({ fd: process.stdout.fd || 1 })
} else {
stream = process.stdout
}
Expand Down
7 changes: 7 additions & 0 deletions test/stdout-protection.test.js
Expand Up @@ -23,3 +23,10 @@ test('do not use SonicBoom is someone has passed process.stdout to pino', async
const logger = pino(process.stdout)
equal(logger[pino.symbols.streamSym], process.stdout)
})

test('do not crash if process.stdout has no fd', async ({ teardown }) => {
const fd = process.stdout.fd
delete process.stdout.fd
teardown(function () { process.stdout.fd = fd })
pino()
})
4 changes: 4 additions & 0 deletions test/transport/core.test.js
Expand Up @@ -12,8 +12,10 @@ const strip = require('strip-ansi')
const execa = require('execa')
const writer = require('flush-write-stream')
const rimraf = require('rimraf')
const { promisify } = require('util')
const { tmpdir } = os

const immediate = promisify(setImmediate)
const pid = process.pid
const hostname = os.hostname()

Expand Down Expand Up @@ -398,6 +400,7 @@ test('stdout in worker', async ({ not }) => {
cb()
}))
await once(child, 'close')
await immediate()
not(strip(actual).match(/Hello/), null)
})

Expand All @@ -410,6 +413,7 @@ test('log and exit on ready', async ({ not }) => {
cb()
}))
await once(child, 'close')
await immediate()
not(strip(actual).match(/Hello/), null)
})

Expand Down

0 comments on commit 29911ab

Please sign in to comment.