Skip to content

Commit 214899c

Browse files
committed
PtyMaster::drop should panic on EBADF
Fixes #659
1 parent da49f2a commit 214899c

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ test = true
4848
name = "test-mount"
4949
path = "test/test_mount.rs"
5050
harness = false
51+
52+
[[test]]
53+
name = "test-ptymaster-drop"
54+
path = "test/test_ptymaster_drop.rs"

src/pty.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ impl IntoRawFd for PtyMaster {
4545

4646
impl Drop for PtyMaster {
4747
fn drop(&mut self) {
48-
// Errors when closing are ignored because we don't actually know if the file descriptor
49-
// was closed. If we retried, it's possible that descriptor was reallocated in the mean
50-
// time and the wrong file descriptor could be closed.
51-
let _ = ::unistd::close(self.0);
48+
// On drop, we ignore errors like EINTR and EIO because there's no clear
49+
// way to handle them, we can't return anything, and (on FreeBSD at
50+
// least) the file descriptor is deallocated in these cases. However,
51+
// we must panic on EBADF, because it is always an error to close an
52+
// invalid file descriptor. That frequently indicates a double-close
53+
// condition, which can cause confusing errors for future I/O
54+
// operations.
55+
let e = ::unistd::close(self.0);
56+
if e == Err(Error::Sys(Errno::EBADF)) {
57+
panic!("Closing an invalid file descriptor!");
58+
};
5259
}
5360
}
5461

test/test_pty.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
use std::io::Write;
12
use std::path::Path;
23
use std::os::unix::prelude::*;
4+
use tempfile::tempfile;
35

46
use nix::fcntl::{O_RDWR, open};
57
use nix::pty::*;
68
use nix::sys::stat;
79
use nix::sys::termios::*;
810
use nix::unistd::{write, close};
911

12+
/// Regression test for Issue #659
13+
/// This is the correct way to explicitly close a PtyMaster
14+
#[test]
15+
fn test_explicit_close() {
16+
let mut f = {
17+
let m = posix_openpt(O_RDWR).unwrap();
18+
close(m.into_raw_fd()).unwrap();
19+
tempfile().unwrap()
20+
};
21+
// This should work. But if there's been a double close, then it will
22+
// return EBADF
23+
f.write(b"whatever").unwrap();
24+
}
25+
1026
/// Test equivalence of `ptsname` and `ptsname_r`
1127
#[test]
1228
#[cfg(any(target_os = "android", target_os = "linux"))]

test/test_ptymaster_drop.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate nix;
2+
3+
use nix::fcntl::O_RDWR;
4+
use nix::pty::*;
5+
use nix::unistd::close;
6+
use std::os::unix::io::AsRawFd;
7+
8+
/// Regression test for Issue #659
9+
/// PtyMaster should panic rather than double close the file descriptor
10+
/// This must run in its own test process because it deliberately creates a race
11+
/// condition.
12+
#[test]
13+
#[should_panic(expected = "Closing an invalid file descriptor!")]
14+
fn test_double_close() {
15+
let m = posix_openpt(O_RDWR).unwrap();
16+
close(m.as_raw_fd()).unwrap();
17+
drop(m); // should panic here
18+
}

0 commit comments

Comments
 (0)