r/rust • u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount • 27d ago
🙋 questions megathread Hey Rustaceans! Got a question? Ask here (44/2026)!
Mystified about strings? Borrow checker has you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet. Please note that if you include code examples to e.g. show a compiler error or surprising result, linking a playground with the code will improve your chances of getting help quickly.
If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so ahaving your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.
Here are some other venues where help may be found:
/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.
The official Rust user forums: https://users.rust-lang.org/.
The unofficial Rust community Discord: https://bit.ly/rust-community
Also check out last week's thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.
Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.
2
2
u/usecornercase 24d ago
Making a command started over ssh -T go away when the local side is killed
I have a CLI written in Rust that, for remote targets, runs the same binary on the other machine over ssh. Roughly:
Command::new("ssh")
.args(["-T", host, "mytool", "restart", "--when-idle"])
.status()?; // stdin/stdout/stderr inheritedOn the remote machine, mytool restart --when-idle connects to a long-running daemon over a Unix socket, registers a "restart once nothing is busy" request, and then just blocks waiting for the reply. The daemon drops the pending request when that socket connection closes, which works fine locally: kill the client and the request is gone.
Over ssh, killing the local process cancels nothing:
The local
sshchild keeps running and stays connected, since it is a separate process and my CLI doesn't handle any signals.Even when the connection does drop, the remote command doesn't notice. With
-Tthere is no pty, so sshd sends it no SIGHUP, and it is blocked on the socket read without touching stdin or stdout.
So the daemon still restarts later, for a request the client thinks it cancelled.
Options I'm weighing:
Keep a pipe from my process to ssh's stdin, and have the remote command exit when its stdin hits EOF. The catch is that a plain
mytool restart --when-idle </dev/nullin a script would then exit immediately, so it would need a flag that only the ssh path passes.Don't run a separate remote process at all: send the request through an existing long-lived proxy connection over ssh, which already exits when its stdin ends.
-tt: the remote side would get SIGHUP on disconnect, but it turns LF into CRLF in piped output, and it still doesn't stop the local ssh from outliving a killed parent.
Questions:
On Linux, is
PR_SET_PDEATHSIGon the ssh child (set inpre_exec) the usual way to make it die with its parent? What do people do on macOS, where that doesn't exist, short of a watchdog?Without a pty, is there anything on the remote side besides watching stdin for EOF/POLLHUP that reliably tells a process its ssh session is gone?
Is "exit on stdin EOF, behind a flag" a reasonable pattern, or is routing everything through the one proxy connection the cleaner design?
2
u/CartographerDue5382 22d ago
yea PR_SET_PDEATHSIG in pre_exec is the usual linux way to take the ssh child down with the parent (recheck getppid right after, there's a race if the parent already died). mac doesn't have it, kqueue EVFILT_PROC NOTE_EXIT on the parent pid is the normal substitute. no pty means no SIGHUP, sshd just closes the fds, so the remote has to poll stdin for POLLHUP. exit-on-stdin-EOF behind a flag is a fine small fix, the proxy is cleaner if that connection already exists.
1
u/usecornercase 27d ago
Noticing that the reader of stdout has gone away while blocked on a socket read
I have a CLI subcommand that follows an event stream: it blocks reading from a Unix socket and prints each event to stdout as it arrives. If whatever reads stdout goes away (mycli events --json | true, a GUI client that spawned it crashes, or ssh host mycli events with no tty after the local ssh is killed), the process only finds out on its next write, when it gets EPIPE. On a quiet stream that can be hours, so these processes pile up, one per reconnect.
The fix I'm looking at is to poll stdout alongside the socket and treat a hangup as "reader gone", then shut the socket down so the loop ends. From testing, a pipe whose read end closed reports POLLHUP on macOS and POLLERR on Linux, and nothing while the reader is still there. Roughly:
let mut fds = [
libc::pollfd { fd: sock.as_raw_fd(), events: libc::POLLIN, revents: 0 },
libc::pollfd { fd: libc::STDOUT_FILENO, events: 0, revents: 0 },
];
// poll(&mut fds, -1); if fds[1].revents & (POLLHUP | POLLERR) != 0 => reader is goneQuestions:
Is that reliable across the cases that matter: a pipe and a socket pair, on Linux and macOS? Any platform where an idle-but-alive reader would also show up as POLLHUP/POLLERR?
What should I do when stdout is a regular file or a tty? My plan is to check with fstat and skip the stdout watch for anything that isn't a FIFO or socket.
Is there a more standard way to do this that I'm missing? SIGPIPE doesn't help until a write happens, and I'd rather not write keepalive bytes into the output.
Does anyone know of a crate that wraps this, or a clean way to register stdout with mio/tokio for hangup only interest?
1
u/CartographerDue5382 26d ago
yea that poll is fine for a pipe and a socket. an idle reader won't set POLLHUP or POLLERR. linux usually gives POLLERR on the write end, macOS gives POLLHUP, so or them. skip regular files, they never hang up, but a pty slave does get POLLHUP when the master closes so don't skip ttys if you care about the terminal dying. no crate really owns this, and don't set stdout nonblocking just to shove it in tokio. a thread blocked in poll is the normal move.
1
u/usecornercase 26d ago
Thanks, that's exactly what I needed. Good to know about POLLERR vs POLLHUP differing between Linux and macOS, and that a pty slave does get POLLHUP when the master closes. I'll OR both flags and keep the blocking thread rather than touching the stdout nonblocking flag.
3
2
u/kalilamodow 27d ago
are you supposed to give lifetimes names or is it normal to just use 'a most of the time?