Live data from Hacker News

Fixing Ctrl+C in Rust terminal apps: Child process management

fiveonefour.com

31–40 of 62 posts

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#31
post #11

Earlier quoted context omitted.

Nothing, that is, except for the examples, the source code, the libraries, and the linked references. But nothing else.

Are you suggesting that the examples, source, libraries, and references for Rust make these mistakes, but other languages don't?

the title: "fixing ... in rust". The problems aren't necessarily rust only problems, the solutions are rust solutions.

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#32
post #4

Earlier quoted context omitted.

I had no idea what a garden path sentence was, too, but wiki to the rescue: https://en.wikipedia.org/wiki/Garden-path_sentence

The old man the boat

The horse raced past the barn fell

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#33
post #22

The graceful shutdown stuff is good, but always piping the output of child processes is not necessarily the right thing to do. Some processes need stdin (what if it's a shell?) and some processes will be checking if stdout is a tty. What you should do (and Rust doesn't make this easy) is allocate a new pty for your child processes if your own stdout is a tty. Some programs default to this (eg: ssh), others make it co…

>Also, if the terminal is in raw mode then you'll never get ctrl+C. The process/thread/task won't receive SIGINT, true. But I believe it will see the character ETX (ASCII 3). Programs that use raw mode input need to do their own keystroke processing.

If you're in raw mode, whether ^C is SIGINT is open for interpretation

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#36
post #24

I hate to be the guy, but I could barely see the code snippets. Is contrast an issue for anyone else? Reader mode improves thins slightly but at the cost of code being unhighlighted and wrapping like crazy.

The highlighting is clearly designed for a dark background but has been given a light background in light mode. Change the bg-neutral-100 to bg-neutral-900 and it’s fine—still not magnificent, but fine.

(But as for barely… if you don’t run JS, then you just don’t see the code snippets, because for some inscrutable reason, unlike the rest of the document, they’re only rendered client-side.)

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#37
post #35

It seems wrong that the app would be responsible for cleanup... Shouldn't this be solved in the shell / terminal ? What if kill -9?

When you use ctrl+c, you are not killing the program, you are sending it a SIGTERM signal which essentially means « could you please stop yourself ? » so the program have a chance to clean things before exiting.

kill -9 is sending a SIGKILL signal which, well, kills the program immediately.

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#38

Are you saying that after the main process has exited, child processes can still run and write to stdout/stderr?

Child processes are created using, generally, 2 syscalls: fork, then exec. When you fork, all file descriptors the main process has open are copied, and are now open in two places. Then, when the child calls exec (to transform itself into the target program), all file descriptors stay open in the new process (unless a specific fd is explicitly configured otherwise, FD_CLOEXEC). Standard output are just file descripto…

> Conceptually, you think of "spawning a child" as something that is in some kind of container (the parent process), but the underlying mechanics are not like this at all,

That is not quite right either, the newly created child processes generally go to the same process group as the parent, the process groups (and sessions) forming those "containers".

Tbh this is one of the many murky areas of UNIX.

Re: Fixing Ctrl+C in Rust terminal apps: Child process management

#39
post #18
post #12

Fearless concurrency with Rust unless you are worried about lifecycle management, threads/co-operation and general ergonomics. Even modern c++ might be better at this (gasp!) with std::jthread

Are there any languages that provide for or care about lifecycle management across address space boundaries? After fork() you're usually fucked and need explicit controls.

Pythons multiprocessing comes to mind
Post reply on HN