Skip to content

Commit 94eaae2

Browse files
committed
Flush stdout on read from stdin again
This is possible now since we have reentrant mutexes. Fixes #25555.
1 parent 84b1e08 commit 94eaae2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/libstd/io/stdio.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ impl Stdin {
165165
#[stable(feature = "rust1", since = "1.0.0")]
166166
impl Read for Stdin {
167167
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
168+
// Flush stdout so that weird issues like a print!'d prompt not being
169+
// shown until after the user hits enter.
170+
drop(stdout().flush());
168171
self.lock().read(buf)
169172
}
170173
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::env;
2+
use std::io;
3+
use std::io::{Read, Write};
4+
use std::process::{Command, Stdio};
5+
6+
fn main(){
7+
if env::args().count() > 1 && env::args().nth(1) == Some("child".to_string()) {
8+
child()
9+
} else {
10+
let mut p = Command::new(env::args().nth(0).unwrap())
11+
.arg("child")
12+
.stdin(Stdio::piped())
13+
.stdout(Stdio::piped())
14+
.stderr(Stdio::inherit())
15+
.spawn().unwrap();
16+
{
17+
let mut buf = [0; 1];
18+
assert!(p.stdout.as_mut().unwrap().read(&mut buf).unwrap() >= 1);
19+
assert_eq!(buf[0], b'>');
20+
assert!(p.stdin.as_mut().unwrap().write(b"abcd\n").unwrap() >= 1);
21+
}
22+
// FIXME: timeout and fail on timeout
23+
assert!(p.wait().unwrap().success());
24+
}
25+
}
26+
27+
fn child(){
28+
let stdout = io::stdout();
29+
let lstdout = stdout.lock();
30+
let mut stdin = io::stdin();
31+
print!(">");
32+
let mut letter = [0; 1];
33+
stdin.read(&mut letter).unwrap();
34+
}

0 commit comments

Comments
 (0)