nodejs / nodejs/node

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

Offen
#51,561 17 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

child_process feature request
Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem im Beispiel gezeigten Verhalten von child_process.spawn() bei timeout und exit event. Prüfen Sie die vorgeschlagenen Ansätze für timeout event, exit metadata, error und properties und legen Sie anschließend eine dokumentierte Möglichkeit fest, mit der Benutzer eine durch timeout ausgelöste Beendigung von einem unabhängigen SIGTERM unterscheiden können.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, node.js
Bereich
api, backend
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.