Skip to content

Commit 29911ab

Browse files
authored
Do not assume process.stdout has a file descriptor (#1503)
* Do not assume process.stdout has a file descriptor Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]>
1 parent 90e728e commit 29911ab

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/tools.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ function createArgsNormalizer (defaultOptions) {
305305
if (enabled === false) opts.level = 'silent'
306306
if (!stream) {
307307
if (!hasBeenTampered(process.stdout)) {
308-
stream = buildSafeSonicBoom({ fd: process.stdout.fd })
308+
// If process.stdout.fd is undefined, it means that we are running
309+
// in a worker thread. Let's assume we are logging to file descriptor 1.
310+
stream = buildSafeSonicBoom({ fd: process.stdout.fd || 1 })
309311
} else {
310312
stream = process.stdout
311313
}

test/stdout-protection.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ test('do not use SonicBoom is someone has passed process.stdout to pino', async
2323
const logger = pino(process.stdout)
2424
equal(logger[pino.symbols.streamSym], process.stdout)
2525
})
26+
27+
test('do not crash if process.stdout has no fd', async ({ teardown }) => {
28+
const fd = process.stdout.fd
29+
delete process.stdout.fd
30+
teardown(function () { process.stdout.fd = fd })
31+
pino()
32+
})

test/transport/core.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const strip = require('strip-ansi')
1212
const execa = require('execa')
1313
const writer = require('flush-write-stream')
1414
const rimraf = require('rimraf')
15+
const { promisify } = require('util')
1516
const { tmpdir } = os
1617

18+
const immediate = promisify(setImmediate)
1719
const pid = process.pid
1820
const hostname = os.hostname()
1921

@@ -398,6 +400,7 @@ test('stdout in worker', async ({ not }) => {
398400
cb()
399401
}))
400402
await once(child, 'close')
403+
await immediate()
401404
not(strip(actual).match(/Hello/), null)
402405
})
403406

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

0 commit comments

Comments
 (0)