Skip to content

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

Description

@ehmicky

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.

Activity

  1. aduh95 commented on Jan 25, 2024

    @aduh95
    Contributor

    Another alternative is to pass an AbortSIgnal:

    import {spawn} from 'node:child_process'
    
    const signal = AbortSignal.timeout(2e3)
    const childProcess = spawn('node', ['-e', 'setTimeout(() => {}, 1e7)'], {
      signal, 
      stdio: 'inherit', // no stdout/stderr
    })
    childProcess.on('error', (err) => {
      console.log({err, didTimeout: signal.aborted}) // { err: [AbortError], didTimeout: true }
    });
    childProcess.on('exit', (exitCode, killSignal) => {
      console.log({exitCode, killSignal, didTimeout: signal.aborted}) // { exitCode: null, killSignal: 'SIGTERM', didTimeout: true }
    })
  2. FranciscoMateusVG commented on Jan 30, 2024

    @FranciscoMateusVG

    Setting childProcess.timedOut = true is an easy and convenient implementation. We could go with AbortSignal, but just checking a flag keeps things really simple. Opened a PR implementing 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(childProcess.timedOut); // true
      console.log({ exitCode, signal }); // { exitCode: null, signal: 'SIGTERM' }
     });
  3. github-actions commented on Jul 29, 2024

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  4. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 29, 2024
  5. ehmicky commented on Jul 29, 2024

    @ehmicky
    Author

    There is a PR at #51608, waiting for a review.

  6. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 30, 2024
  7. github-actions commented on Jan 26, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jan 26, 2025
  9. sindresorhus commented on Jan 26, 2025

    @sindresorhus

    Please keep this open.

  10. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jan 27, 2025
  11. github-actions commented on Jul 26, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  12. 4 remaining items

  13. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jan 23, 2026
  14. sindresorhus commented on Jan 23, 2026

    @sindresorhus

    Please keep this open.

  15. FranciscoMateusVG commented on Jan 23, 2026

    @FranciscoMateusVG

    Is there a way to spped this merge? Its been a while since this was opened

  16. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jan 24, 2026
  17. FranciscoMateusVG commented on Mar 2, 2026

    @FranciscoMateusVG

    Sooonnnnn it will be merged

  18. github-actions commented on Jul 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  19. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 20, 2026
  20. sindresorhus commented on Jul 20, 2026

    @sindresorhus

    Please keep this open.

  21. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 21, 2026
  22. Curis-lab commented on Aug 7, 2026

    @Curis-lab

    Please keep this feature I'm testing on it.

  23. Curis-lab commented on Aug 14, 2026

    @Curis-lab

    I think I should also check for manual kill

  24. Curis-lab commented on Aug 15, 2026

    @Curis-lab

    oh! I got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions