Bird
0
0

How can you combine error event handling with a timeout to kill a child process if it hangs?

hard📝 Application Q9 of 15
Node.js - Child Processes
How can you combine error event handling with a timeout to kill a child process if it hangs?
AListen only to 'error' event and kill the process there
BUse try-catch around spawn and kill the process in catch block
CSet a timer after spawn; if timer fires before 'exit', call child.kill(); listen for 'error' to handle kill errors
DSpawn the process with a timeout option that kills automatically
Step-by-Step Solution
Solution:
  1. Step 1: Implement timeout logic

    Start a timer after spawning the child process to detect hanging.
  2. Step 2: Kill process if timeout triggers before exit

    If the timer fires first, call child.kill() to stop the process.
  3. Step 3: Listen for 'error' event to catch kill failures

    Handle any errors from killing the process by listening to 'error'.
  4. Final Answer:

    Set a timer after spawn; if timer fires before 'exit', call child.kill(); listen for 'error' to handle kill errors -> Option C
  5. Quick Check:

    Timeout + kill + 'error' listener = safe hang handling [OK]
Quick Trick: Use timer + kill + 'error' listener to handle hangs [OK]
Common Mistakes:
  • Using try-catch for async kill errors
  • Ignoring 'error' event after kill
  • Assuming spawn has timeout option

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes