Skip to content

Commit 35f12b1

Browse files
committed
embedded: prevent double-close of pty fd
Signed-off-by: Manos Pitsidianakis <[email protected]>
1 parent eda6620 commit 35f12b1

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

meli/src/terminal/embedded.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use std::os::fd::OwnedFd;
2424
use std::{
2525
ffi::{CString, OsStr},
26+
mem::ManuallyDrop,
2627
os::unix::{
2728
ffi::OsStrExt,
2829
io::{AsRawFd, FromRawFd, IntoRawFd},
@@ -178,7 +179,8 @@ pub fn create_pty(width: usize, height: usize, command: &str) -> Result<Arc<Mute
178179
};
179180

180181
let stdin = unsafe { std::fs::File::from_raw_fd(frontend_fd.as_raw_fd()) };
181-
let mut embedded_pty = Terminal::new(stdin, child_pid);
182+
// We will let the spawned thread close frontend_fd
183+
let mut embedded_pty = Terminal::new(ManuallyDrop::new(stdin), child_pid);
182184
embedded_pty.set_terminal_size((width, height));
183185
let pty = Arc::new(Mutex::new(embedded_pty));
184186
let pty_ = pty.clone();

meli/src/terminal/embedded/terminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ enum CodepointBuf {
7070
#[derive(Debug)]
7171
pub struct Terminal {
7272
pub grid: EmbeddedGrid,
73-
stdin: std::fs::File,
73+
stdin: std::mem::ManuallyDrop<std::fs::File>,
7474
/// Pid of the embedded process
7575
pub child_pid: nix::unistd::Pid,
7676
}
@@ -114,7 +114,7 @@ impl std::io::Write for Terminal {
114114
}
115115

116116
impl Terminal {
117-
pub fn new(stdin: std::fs::File, child_pid: nix::unistd::Pid) -> Self {
117+
pub fn new(stdin: std::mem::ManuallyDrop<std::fs::File>, child_pid: nix::unistd::Pid) -> Self {
118118
Self {
119119
grid: EmbeddedGrid::new(),
120120
stdin,

0 commit comments

Comments
 (0)