Skip to content

Commit e0264ff

Browse files
committed
Don't dup the stdio file descriptors.
This is just an implementation detail of using libuv, so move the libuv-specific logic into librustuv.
1 parent 9169579 commit e0264ff

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/librustuv/tty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ impl TtyWatcher {
4444
return Err(UvError(uvll::EBADF));
4545
}
4646

47+
// libuv was recently changed to not close the stdio file descriptors,
48+
// but it did not change the behavior for windows. Until this issue is
49+
// fixed, we need to dup the stdio file descriptors because otherwise
50+
// uv_close will close them
51+
let fd = if cfg!(windows) && fd <= libc::STDERR_FILENO {
52+
unsafe { libc::dup(fd) }
53+
} else { fd };
54+
4755
// If this file descriptor is indeed guessed to be a tty, then go ahead
4856
// with attempting to open it as a tty.
4957
let handle = UvHandle::alloc(None::<TtyWatcher>, uvll::UV_TTY);

src/libstd/io/native/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ impl rtio::IoFactory for IoFactory {
206206
}
207207
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
208208
if unsafe { libc::isatty(fd) } != 0 {
209-
Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
209+
// Don't ever close the stdio file descriptors, nothing good really
210+
// comes of that.
211+
Ok(~file::FileDesc::new(fd, fd > libc::STDERR_FILENO) as ~RtioTTY)
210212
} else {
211213
Err(IoError {
212214
kind: io::MismatchedFileTypeForOperation,

src/libstd/io/stdio.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use libc;
3131
use option::{Option, Some, None};
3232
use result::{Ok, Err};
3333
use io::buffered::LineBufferedWriter;
34-
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
35-
CloseAsynchronously};
34+
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io, DontClose};
3635
use super::{Reader, Writer, io_error, IoError, OtherIoError,
3736
standard_error, EndOfFile};
3837

@@ -71,18 +70,9 @@ enum StdSource {
7170

7271
fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
7372
with_local_io(|io| {
74-
let fd = unsafe { libc::dup(fd) };
7573
match io.tty_open(fd, readable) {
7674
Ok(tty) => Some(f(TTY(tty))),
77-
Err(_) => {
78-
// It's not really that desirable if these handles are closed
79-
// synchronously, and because they're squirreled away in a task
80-
// structure the destructors will be run when the task is
81-
// attempted to get destroyed. This means that if we run a
82-
// synchronous destructor we'll attempt to do some scheduling
83-
// operations which will just result in sadness.
84-
Some(f(File(io.fs_from_raw_fd(fd, CloseAsynchronously))))
85-
}
75+
Err(_) => Some(f(File(io.fs_from_raw_fd(fd, DontClose)))),
8676
}
8777
}).unwrap()
8878
}

0 commit comments

Comments
 (0)