Skip to content

Commit 5577fa6

Browse files
authored
Properly close stdin for child processes. (#1485)
Closes #1482.
1 parent dc2f902 commit 5577fa6

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

lib/src/executable.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Future<int> runExecutable(Entrypoint entrypoint, String package,
133133
// that prevents pub from also writing to the output streams.
134134
process.stderr.listen(stderr.add);
135135
process.stdout.listen(stdout.add);
136-
stdin.listen(process.stdin.add);
136+
stdin.listen(process.stdin.add, onDone: process.stdin.close);
137137

138138
// Work around dart-lang/sdk#25348.
139139
process.stdin.done.catchError((_) {});

test/run/app_can_read_from_stdin_test.dart

+42-15
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@
55
import '../descriptor.dart' as d;
66
import '../test_pub.dart';
77

8-
const SCRIPT = """
9-
import 'dart:io';
10-
11-
main() {
12-
print("started");
13-
var line1 = stdin.readLineSync();
14-
print("between");
15-
var line2 = stdin.readLineSync();
16-
print(line1);
17-
print(line2);
18-
}
19-
""";
20-
218
main() {
22-
integration('the spawned application can read from stdin', () {
9+
integration('the spawned application can read line-by-line from stdin', () {
2310
d.dir(appPath, [
2411
d.appPubspec(),
2512
d.dir("bin", [
26-
d.file("script.dart", SCRIPT)
13+
d.file("script.dart", """
14+
import 'dart:io';
15+
16+
main() {
17+
print("started");
18+
var line1 = stdin.readLineSync();
19+
print("between");
20+
var line2 = stdin.readLineSync();
21+
print(line1);
22+
print(line2);
23+
}
24+
""")
2725
])
2826
]).create();
2927

@@ -38,4 +36,33 @@ main() {
3836
pub.stdout.expect("second");
3937
pub.shouldExit(0);
4038
});
39+
40+
integration('the spawned application can read streamed from stdin', () {
41+
d.dir(appPath, [
42+
d.appPubspec(),
43+
d.dir("bin", [
44+
d.file("script.dart", """
45+
import 'dart:io';
46+
47+
main() {
48+
print("started");
49+
stdin.listen(stdout.add);
50+
}
51+
""")
52+
])
53+
]).create();
54+
55+
pubGet();
56+
var pub = pubRun(args: ["bin/script"]);
57+
58+
pub.stdout.expect("started");
59+
pub.writeLine("first");
60+
pub.stdout.expect("first");
61+
pub.writeLine("second");
62+
pub.stdout.expect("second");
63+
pub.writeLine("third");
64+
pub.stdout.expect("third");
65+
pub.closeStdin();
66+
pub.shouldExit(0);
67+
});
4168
}

0 commit comments

Comments
 (0)