When a terminal application that spawns child processes doesn't exit cleanly after a Ctrl+C, the user is left with a corrupted terminal. Instead of a clean prompt, you get garbled output and a non-functional shell. This post covers how to solve these issues, with examples from the Moose CLI (for the PR that fixed many of these issues, see here).
In this post, you’ll read learnings from solving these issues in the Moose CLI— terminal application that manages multiple child processes, including Docker containers, TypeScript compilers, and background workers.
Child process output must be completely isolated from the terminal. Direct child process output to the terminal creates race conditions and corruption.
Interactive elements like spinners need careful coordination with child process output to prevent both from writing to the terminal simultaneously, which misformats characters in the terminal display.
Building robust terminal applications requires careful child process management. To provide a clean user experience, especially when handling Ctrl+C: