3

In the documentation for Node's Child Processes, there is this sentence in the section on child_process.spawn():

On Windows, setting options.detached to true makes it possible for the child process to continue running after the parent exits.

That makes it sound like (at least on Windows) when you leave options.detached to the default value of false, spawn()'d processes will automatically be killed. That's actually the behavior I want in my application, and in fact I was calling myChildProcess.kill( "SIGINT" ) in my code, but commented it out, and the child processes still went away when my app quit. So that's great, but:

(1) My understanding is that it's necessary to do some tricky stuff with "job objects" as discussed here in order to make this work on Windows. Do you know if Node is doing something tricky like that to make child processes go away? Or perhaps it's more simple than that and Node just keeps a list of the spawned process IDs and kills any of them that are still around when shutting down? Which leads to the closely related question...

(2) If Node is indeed doing something special to kill child processes, do you know if there are cases (e.g., some kind of app crash) that would defeat what it's doing and leave the child processes running?

UPDATE: To clarify, the child processes I'm launching in my case are Python web server processes, not other Node processes. I don't know if there's a difference in behavior between a Node child process and some other child process for the purpose of this question.

M Katz
  • 5,098
  • 3
  • 44
  • 66

2 Answers2

-1

A Node instance will quit as long as there is nothing left in the event queue (and no async code pending), so as long as you aren't leaving anything open then naturally a Node process will quit when it's done.

In terms of the process hanging on a crash, unless you are explicitly handling uncaught exceptions the the process will exit immediately.

James
  • 80,725
  • 18
  • 167
  • 237
  • you can spawn by cmd - not always a node instance – Estradiaz Jan 18 '20 at 12:02
  • I'm asking about child processes, not the main Node process itself. – M Katz Jan 18 '20 at 12:02
  • @MKatz are you not spawning other Node instances though? That's what I picked up from your question – James Jan 18 '20 at 12:41
  • @Estradiaz true, however I was assuming based on the question the OP was spawning Node instances – James Jan 18 '20 at 12:42
  • @James, no, sorry, these are not other Node instances that I'm spawning. I'll updated that in the post. But also, even if they were other Node instances, they could be forever-running processes, right? So there would still be the question of whether the parent quitting automatically kills them. – M Katz Jan 18 '20 at 20:33
  • @MKatz why do you think they would be forever running if they were Node instances? As per my answer, a Node process will only hang about if it's still got stuff to do... – James Jan 18 '20 at 20:38
  • @James, I'm saying two things. (1) I don't happen to be starting Node child processes. (2) Your comment about Node processes finishing automatically when they have nothing left to do is fine, but *some* Node processes that I could start might never run out of things to do. For instance, the child processes I'm actually starting are Python web server processes. They could just as easily have been Node web server processes. Those processes would continue running forever, right? They would always have a timer event in their event queue because their nature is to wait indefinitely for requests. – M Katz Jan 18 '20 at 20:46
  • @MKatz correct, as I said my answer was geared around starting Node processes as unfortunately your question *appeared* to allude to that. If it's a Python server process then yes it's very likely that these process *will not* be killed and run indefinitely. – James Jan 18 '20 at 20:52
  • @James, but actually what I'm seeing is that when I quit the main Node application, even though my code does not kill the child processes explicitly, they *do* in fact go away, as implied by the Node Child Process documentation I quote. So my question is asking whether Node is doing some (tricky?) stuff on my behalf to kill those forever-running child processes. Because I thought, at you say, that they would *not* die by default just because the parent exits. – M Katz Jan 18 '20 at 20:56
  • @MKatz there is some information in the [docs](https://nodejs.org/api/child_process.html#child_process_options_detached) that suggests that child processes *may* be killed when the parent process is killed if it's still connected to it's `stdio` (that may be the issue, you can try the example in the docs). Also as per the docs you can see that if it's a non-Windows system you are running on then it's very much an implementation detail of whether it gets killed or not. – James Jan 18 '20 at 21:00
-1

If you want a child process to be long-running and to survive the termination of the node process itself, as you know you set options.detached = true.

This business of stopping a child process when a parent process stops is operating-system behavior. A parent process (running any programming language system, not just node) owns a non-detached child process. The OS cleans up child processes upon the termination of their parent.

Detaching a process tells the OS to make it no longer a child process, so the OS won't clean it up automatically.

A good practice for node child processes: whenever possible, have them do their assigned task and then exit. In other words, in most cases you should not need to rely on this child / detached behavior.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 2
    I don't believe that's true, at least on Windows. If you look at the link that in give in (1) in the original post, it's all about figuring out a way to make the child process get killed when the parent dies/crashes, no? (The case of the parent crashing is the main case, because obviously the parent can kill the children explicitly at the end of its run when it terminates normally.) – M Katz Jan 18 '20 at 20:37
  • As for whether child processes should do their job and then quit, that depends on the nature of the child process. Sometimes they have a finite job to do. In my case the child processes are Python web servers that are serving various ajax calls from my Electron client (the main Node application). So those child processes keep going until someone tells them to quit or kills them. – M Katz Jan 18 '20 at 20:39