Skip to content

Commit 32ab610

Browse files
authored
Merge pull request #1078 from NgoQuocViet2001/fix-cli-ignored-signal-hang
fix(cli): exit when the command dies from SIGPIPE
2 parents c40119a + 7c67590 commit 32ab610

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cli.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ function run (argv) {
286286
} else {
287287
// Keep the event loop alive until the re-raised signal terminates us.
288288
// Otherwise Node can finish normally before delivery and report exit 0.
289-
setInterval(() => {}, 1000)
289+
// Node ignores some signals (SIGPIPE), so fall back to the shell's
290+
// 128 + signal number status instead of waiting forever.
291+
setTimeout(() => process.exit(128 + os.constants.signals[signal]), 1000)
290292
process.kill(process.pid, signal)
291293
}
292294
})

‎tests/test-cli-signals.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ t.test('POSIX signal forwarding', { skip: process.platform === 'win32' }, async
7575
st.same(await command.exited, { code: null, signal: 'SIGTERM' })
7676
})
7777

78+
await ct.test('exits when the command dies from a signal Node ignores', async st => {
79+
// Node ignores SIGPIPE, so re-raising it does not stop dotenv (`dotenv run -- cmd | head`).
80+
const result = cp.spawnSync(process.execPath, ['dist/index.cjs', 'run', '-q', '--', 'sh', '-c', 'kill -PIPE $$'], {
81+
cwd: path.resolve(__dirname, '..'),
82+
encoding: 'utf8',
83+
timeout: 5000,
84+
env: { ...process.env, NODE_OPTIONS: '' }
85+
})
86+
st.equal(result.error, undefined)
87+
st.equal(result.signal, null)
88+
st.equal(result.status, 128 + os.constants.signals.SIGPIPE)
89+
})
90+
7891
await ct.test('forwards termination to subprocesses too', async st => {
7992
const grandchild = "process.once('SIGTERM', () => { console.log('grandchild stopped'); process.exit(0) }); setInterval(() => {}, 1000); process.send('ready')"
8093
const command = await launch(st, `

0 commit comments

Comments
 (0)