nodejs / nodejs/node

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

Ouverte
#51,561 17 commentaires 4 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

child_process feature request
Langage dominant
JavaScript
Étoiles
122k
Forks
37.3k
Merge moyen
4 j 2 h
PR mergées (30 j)
283

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le comportement de child_process.spawn() concernant timeout et exit event présenté dans l’exemple. Examinez les approches proposées pour timeout event, exit metadata, error et properties, puis déterminez une manière documentée permettant aux utilisateurs de distinguer une terminaison déclenchée par timeout d’un SIGTERM sans rapport.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, node.js
Domaine
api, backend
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
Calme
Clarté
À clarifier
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.