From 85aa4cca9b60cb235e835d06b8b4312a0c2f3b1e Mon Sep 17 00:00:00 2001 From: Otto Janke Date: Mon, 9 Dec 2024 23:22:56 -0500 Subject: [PATCH] Throw error in set_terminal_attr when started without a TTY --- src/terminal/sys/unix.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 7129730a..4a162223 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -174,6 +174,14 @@ fn get_terminal_attr(fd: impl AsFd) -> io::Result { #[cfg(not(feature = "libc"))] fn set_terminal_attr(fd: impl AsFd, termios: &Termios) -> io::Result<()> { + // When stdin is not a tty, fd points to '/dev/tty', causing tcsetattr to hang on macOS. + if !rustix::termios::isatty(std::io::stdin()) { + return Err(io::Error::new( + io::ErrorKind::Other, + "Attempt to set terminal attributes with no terminal attached", + )) + } + rustix::termios::tcsetattr(fd, rustix::termios::OptionalActions::Now, termios)?; Ok(()) }