Add guards against invalid file descriptors#931
Add guards against invalid file descriptors#931joshvoigts wants to merge 1 commit intokkawakam:masterfrom
Conversation
|
I would prefer to fix the root cause instead of its consequences |
|
Could you please give #932 a try ? |
|
Sure, I'll give it a shot. It's pretty intermittent. I'll be using the library for awhile and then inexplicably hit the panic. I haven't quite narrowed down when it happens but it seems like it happens around terminal split pane resizes or maybe |
|
I'm seeing what looks like another issue with #932: It could be because in some circumstances rustyline editor is reinitialized. I'll try without the assert. |
You should not ! Index: src/tty/unix.rs
===================================================================
diff --git a/src/tty/unix.rs b/src/tty/unix.rs
--- a/src/tty/unix.rs (revision 12ea5243e4657b2d0ceb07075cc88102b654b412)
+++ b/src/tty/unix.rs (date 1774420169850)
@@ -163,6 +163,7 @@
#[expect(unused_must_use)]
impl Drop for TtyIn {
fn drop(&mut self) {
+ debug!(target: "rustyline", "TtyIn::drop");
if let Some(sig) = self.sig.take() {
sig.uninstall_sigwinch_handler();
}
@@ -1298,6 +1299,7 @@
impl Sig {
#[cfg(not(feature = "signal-hook"))]
fn install_sigwinch_handler() -> Result<Self> {
+ debug!(target: "rustyline", "install_sigwinch_handler");
assert!(unsafe { SIG_PIPE }.is_none());
use nix::sys::signal;
let (pipe, pipe_write) = UnixStream::pair()?;
@@ -1310,6 +1312,7 @@
);
let original_sigint = unsafe { signal::sigaction(signal::SIGINT, &sa)? };
let original_sigwinch = unsafe { signal::sigaction(signal::SIGWINCH, &sa)? };
+ debug!(target: "rustyline", "install_sigwinch_handler: {pipe:?}");
Ok(Self {
pipe,
original_sigint,
@@ -1328,6 +1331,7 @@
#[cfg(not(feature = "signal-hook"))]
fn uninstall_sigwinch_handler(self) -> Result<()> {
use nix::sys::signal;
+ debug!(target: "rustyline", "uninstall_sigwinch_handler: {self.pipe:?}");
let _ = unsafe { signal::sigaction(signal::SIGINT, &self.original_sigint)? };
let _ = unsafe { signal::sigaction(signal::SIGWINCH, &self.original_sigwinch)? };
close(self.pipe)?;Make sure a logger is initialized: fn main() ... {
env_logger::init();
...And something like: RUST_LOG=rustyline=debug cargo run --example example 2> debug.log |
|
unsafe { SIG_PIPE }.take()doesn't work as expected close(self.pipe)?; |
|
A minimal example to reproduce the issue: unsafe { SIG_PIPE }.take()cannot be used ! |
|
Could you please give #932 another try ? |
|
Sorry, I didn't see you force pushed, trying, sometimes it randomly comes up, so I might try it for a few days. It's working so far. |
|
I've been running #932 for a few days and it's working well for me. I'll close this pr. Appreciate the help. |
|
Thank for your time / feedback. |
|
Version 18.0.0 released |
This PR adds guards against invalid file descriptors (
-1) insrc/tty/unix.rsto prevent panics.The problem:
I was hitting this panic:
The issue seemed to come up when resizing terminal splits, and occasionally when quitting. After some digging, I found that certain code paths could end up passing an invalid file descriptor (
-1).The fix:
Add a few defensive checks.