Skip to content

Commit 6ac3492

Browse files
committed
std: User a smaller stdin buffer on windows
Apparently windows doesn't like reading from stdin with a large buffer size, and it also apparently is ok with a smaller buffer size. This changes the reader returned by stdin() to return an 8k buffered reader for stdin rather than a 64k buffered reader. Apparently libuv has run into this before, taking a peek at their code, with a specific comment in their console code saying that "ReadConsole can't handle big buffers", which I presume is related to invoking ReadFile as if it were a file descriptor. Closes #13304
1 parent 00cbda2 commit 6ac3492

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/libstd/io/stdio.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
9999
///
100100
/// See `stdout()` for more notes about this function.
101101
pub fn stdin() -> BufferedReader<StdReader> {
102-
BufferedReader::new(stdin_raw())
102+
// The default buffer capacity is 64k, but apparently windows doesn't like
103+
// 64k reads on stdin. See #13304 for details, but the idea is that on
104+
// windows we use a slighly smaller buffer that's been seen to be
105+
// acceptable.
106+
if cfg!(windows) {
107+
BufferedReader::with_capacity(8 * 1024, stdin_raw())
108+
} else {
109+
BufferedReader::new(stdin_raw())
110+
}
103111
}
104112

105113
/// Creates a new non-blocking handle to the stdin of the current process.

src/test/run-pass/issue-13304.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-fast
12+
13+
extern crate green;
14+
extern crate rustuv;
15+
extern crate native;
16+
17+
use std::os;
18+
use std::io;
19+
use std::str;
20+
21+
#[start]
22+
fn start(argc: int, argv: **u8) -> int {
23+
green::start(argc, argv, rustuv::event_loop, main)
24+
}
25+
26+
fn main() {
27+
let args = os::args();
28+
if args.len() > 1 && args[1].as_slice() == "child" {
29+
if args[2].as_slice() == "green" {
30+
child();
31+
} else {
32+
let (tx, rx) = channel();
33+
native::task::spawn(proc() { tx.send(child()); });
34+
rx.recv();
35+
}
36+
} else {
37+
parent(~"green");
38+
parent(~"native");
39+
let (tx, rx) = channel();
40+
native::task::spawn(proc() {
41+
parent(~"green");
42+
parent(~"native");
43+
tx.send(());
44+
});
45+
rx.recv();
46+
}
47+
}
48+
49+
fn parent(flavor: ~str) {
50+
let args = os::args();
51+
let mut p = io::Process::new(args[0].as_slice(), [~"child", flavor]).unwrap();
52+
p.stdin.get_mut_ref().write_str("test1\ntest2\ntest3").unwrap();
53+
let out = p.wait_with_output();
54+
assert!(out.status.success());
55+
let s = str::from_utf8(out.output.as_slice()).unwrap();
56+
assert_eq!(s, "test1\n\ntest2\n\ntest3\n");
57+
}
58+
59+
fn child() {
60+
for line in io::stdin().lines() {
61+
println!("{}", line.unwrap());
62+
}
63+
}

0 commit comments

Comments
 (0)