Fork/spawn detached child in Node, with IPC and parent stdio, until child disconnects

26 Views Asked by At

I like to have a parent Node process launch a child Node process. The child process should output its STD out and STD error to the parent process, and it should keep TTY support (so using "pipe" for stdio doesn't work here, as the child's std is not TTY). Once the child process gets to a certain stage it should disconnect from the parent (like via IPC process.disconnect()), which should make the parent process to shut down cleanly.

I can get the process to shut down cleanly by forking the child with stdio set to inherit+ipc: unref() the child in the parent, and then in the child call process.disconnect(), overwrite the stderr/stdout.write with NoOps (as the child may still try to write after end()ing the std streams - which isn't allowed), and call .end() on them. But the terminal acts strangely once the child finally exits sometime later; for example, pressing the up arrow causes the control sequence/code ^[[A to appear, instead of the previous command.

So the questions: 1) if my goal is possible what am I missing, and 2) what about the setup here is causing the escape sequences to be printed (at least in a bash shell, which is what I've been testing in)?

I can provide code if needed, but I'm mostly looking for an understanding of how everything is connected.

Node version: v18.19.0

0

There are 0 best solutions below