]> git.lizzy.rs Git - rust.git/commitdiff
Don't dup the stdio file descriptors.
authorAlex Crichton <alex@alexcrichton.com>
Wed, 4 Dec 2013 16:51:47 +0000 (08:51 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 4 Dec 2013 16:51:47 +0000 (08:51 -0800)
This is just an implementation detail of using libuv, so move the libuv-specific
logic into librustuv.

src/librustuv/tty.rs
src/libstd/io/native/mod.rs
src/libstd/io/stdio.rs

index 1cac5872ed37270cadd0b92882f4fb1dba8b1e44..fcad6296579960b5b3bd1bc6bf72ceb88b4f39c4 100644 (file)
@@ -44,6 +44,14 @@ pub fn new(loop_: &Loop, fd: libc::c_int, readable: bool)
             return Err(UvError(uvll::EBADF));
         }
 
+        // libuv was recently changed to not close the stdio file descriptors,
+        // but it did not change the behavior for windows. Until this issue is
+        // fixed, we need to dup the stdio file descriptors because otherwise
+        // uv_close will close them
+        let fd = if cfg!(windows) && fd <= libc::STDERR_FILENO {
+            unsafe { libc::dup(fd) }
+        } else { fd };
+
         // If this file descriptor is indeed guessed to be a tty, then go ahead
         // with attempting to open it as a tty.
         let handle = UvHandle::alloc(None::<TtyWatcher>, uvll::UV_TTY);
index cec0de00ec2b5f06cea114646be0279bbb22d66c..c92f480728e3bfd1a757fa01ee5d7c1f0cdc5eed 100644 (file)
@@ -206,7 +206,9 @@ fn pipe_open(&mut self, fd: c_int) -> IoResult<~RtioPipe> {
     }
     fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
         if unsafe { libc::isatty(fd) } != 0 {
-            Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
+            // Don't ever close the stdio file descriptors, nothing good really
+            // comes of that.
+            Ok(~file::FileDesc::new(fd, fd > libc::STDERR_FILENO) as ~RtioTTY)
         } else {
             Err(IoError {
                 kind: io::MismatchedFileTypeForOperation,
index fe0385c9a957198c476282d8a58cbe5574e008c7..466a65a227efe70cb31727fe7ac2fe52457ea5b5 100644 (file)
@@ -31,8 +31,7 @@
 use option::{Option, Some, None};
 use result::{Ok, Err};
 use io::buffered::LineBufferedWriter;
-use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
-               CloseAsynchronously};
+use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io, DontClose};
 use super::{Reader, Writer, io_error, IoError, OtherIoError,
             standard_error, EndOfFile};
 
@@ -71,18 +70,9 @@ enum StdSource {
 
 fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
     with_local_io(|io| {
-        let fd = unsafe { libc::dup(fd) };
         match io.tty_open(fd, readable) {
             Ok(tty) => Some(f(TTY(tty))),
-            Err(_) => {
-                // It's not really that desirable if these handles are closed
-                // synchronously, and because they're squirreled away in a task
-                // structure the destructors will be run when the task is
-                // attempted to get destroyed. This means that if we run a
-                // synchronous destructor we'll attempt to do some scheduling
-                // operations which will just result in sadness.
-                Some(f(File(io.fs_from_raw_fd(fd, CloseAsynchronously))))
-            }
+            Err(_) => Some(f(File(io.fs_from_raw_fd(fd, DontClose)))),
         }
     }).unwrap()
 }