nodejs / nodejs/node

Know when a process ended due to the `child_process.spawn()` `timeout` option

Aperta
#51,561 17 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

child_process feature request
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

What is the problem this feature will solve?

When a process ends due to the timeout option of child_process.spawn(), there is no way to know whether that termination was due to the timeout. A SIGTERM is sent to the process, but that's pretty much it.

import {spawn} from 'node:child_process'

const childProcess = spawn('node', ['-e', 'setTimeout(() => {}, 1e7)'], {
  timeout: 2e3, 
  stdio: 'inherit', // no stdout/stderr
})
childProcess.on('exit', (exitCode, signal) => {
  console.log({exitCode, signal}) // { exitCode: null, signal: 'SIGTERM' }
})

This makes debugging harder. Some users might be left wondering why the process ended if they don't pay attention to the timeout option.

What is the feature you are proposing to solve the problem?

Having a way to know whether the process ended due to the timeout option. Any implementation would work. Some potential ideas:

  • timeout event on childProcess
  • third boolean argument timedOut passed to the exit event
  • emit an error event with error.code: 'ETIMEDOUT' on childProcess. This is a breaking change since many users are currently listening for error events, so would probably not be a good idea.
  • childProcess.timedOut = true
  • childProcess.timedOut() returning a boolean
What alternatives have you considered?

One could possibly set a manual timeout like the following.

let timedOut = false
setTimeout(() => {
  timedOut = true
}, 2e3)

However, it defeats some of the convenience of the timeout option, since that one might as well call childProcess.kill() themselves then. Also, it is slightly unreliable since an unrelated SIGTERM could theoretically have been sent at the exact same time.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Iniziare dal comportamento di child_process.spawn() relativo a timeout ed exit event mostrato nell’esempio. Esaminare gli approcci proposti per timeout event, exit metadata, error e properties, quindi individuare un modo documentato che consenta agli utenti di distinguere una terminazione attivata da timeout da un SIGTERM non correlato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, node.js
Ambito
api, backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Da chiarire
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.