Get the latest tech news
Fixing Ctrl+C in Rust terminal apps: Child process management
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.
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. Proxy to logging system: Forward child process output to your logging system instead of directly to terminal Handle I/O errors gracefully: child process streams can fail; don't let that crash your proxy Wait for completion: Ensure all output is read before proceeding with cleanup Direct child process output to terminal: Always proxy through your logging system Forgetting stdin: Set stdin(Stdio::null()) to prevent child processes from reading terminal input Not waiting for threads: Always join/await background threads before cleanup Ignoring partial failures: Handle cases where some processes fail to stop Platform-specific assumptions: Use cross-platform libraries like crossterm Blocking cleanup: Keep cleanup operations non-blocking where possible
Or read this on Hacker News