Skip to content

Commit d7369a2

Browse files
fix: Revert stdin flags if AsyncStdin cannot be created
1 parent 6fc219d commit d7369a2

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

‎rust/stackablectl/src/cmds/debug.rs‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
io::{Read, Stdin},
3-
os::fd::AsRawFd,
3+
os::fd::{AsRawFd, RawFd},
44
task::{Poll, ready},
55
};
66

@@ -333,29 +333,44 @@ impl AsyncStdin {
333333
if status == -1 {
334334
return Err(std::io::Error::last_os_error()).context(AsyncifyStdinSnafu);
335335
}
336+
}
337+
338+
// From here on stdin is non-blocking, so every error path has to revert the flags itself.
339+
// `Drop` cannot do that for us, because it only runs once `Self` has been constructed, and
340+
// the flags are shared with the stdin of the shell we are returning to.
341+
// SAFETY: `stdin` is a handle to the process-wide standard input. Its file descriptor is
342+
// valid and open, and `Stdin` neither closes nor replaces it, so it keeps referring to the
343+
// same file description for the entire lifetime of the `AsyncFd`.
344+
let fd = match unsafe { AsyncFd::register(stdin) } {
345+
Ok(fd) => fd,
346+
Err(err) => {
347+
let (stdin, err) = err.into_parts();
348+
revert_stdin_flags(stdin.as_raw_fd(), old_flags);
349+
return Err(err).context(AsyncFdStdinSnafu);
350+
}
336351
};
337-
Ok(Self {
338-
// SAFETY: `stdin` is a handle to the process-wide standard input. Its file
339-
// descriptor is valid and open, and `Stdin` neither closes nor replaces it, so it
340-
// keeps referring to the same file description for the entire lifetime of the
341-
// `AsyncFd`.
342-
fd: unsafe { AsyncFd::register(stdin) }
343-
.map_err(|err| err.into_parts().1)
344-
.context(AsyncFdStdinSnafu)?,
345-
old_flags,
346-
})
352+
353+
Ok(Self { fd, old_flags })
354+
}
355+
}
356+
357+
/// Reverts the file status flags of the stdin file descriptor `fd` to `old_flags`.
358+
///
359+
/// A failure is only logged, because the callers either fail already or are being dropped. Panicking
360+
/// here would be counter-productive: while unwinding, it would abort the process and thereby skip
361+
/// restoring the terminal from raw mode, which is worse than a non-blocking stdin.
362+
fn revert_stdin_flags(fd: RawFd, old_flags: i32) {
363+
if unsafe { libc::fcntl(fd, libc::F_SETFL, old_flags) } == -1 {
364+
error!(
365+
"unable to revert stdin flags: {error}",
366+
error = std::io::Error::last_os_error()
367+
);
347368
}
348369
}
349370

350371
impl Drop for AsyncStdin {
351372
fn drop(&mut self) {
352-
let status = unsafe { libc::fcntl(self.fd.as_raw_fd(), libc::F_SETFL, self.old_flags) };
353-
if status == -1 {
354-
panic!(
355-
"unable to revert stdin flags: {error}",
356-
error = std::io::Error::last_os_error()
357-
);
358-
}
373+
revert_stdin_flags(self.fd.as_raw_fd(), self.old_flags);
359374
}
360375
}
361376

0 commit comments

Comments
 (0)