@@ -1045,23 +1045,29 @@ abstract static class SystemNode extends PythonBuiltinNode {
1045
1045
: new String []{(System .getenv ().getOrDefault ("SHELL" , "sh" )), "-c" };
1046
1046
1047
1047
static class PipePump extends Thread {
1048
+ private static final int MAX_READ = 8192 ;
1048
1049
private final InputStream in ;
1049
1050
private final OutputStream out ;
1050
1051
private final byte [] buffer ;
1051
1052
private boolean finish ;
1053
+ private boolean flush ;
1052
1054
1053
1055
public PipePump (InputStream in , OutputStream out ) {
1054
1056
this .in = in ;
1055
1057
this .out = out ;
1056
- this .buffer = new byte [8192 ];
1058
+ this .buffer = new byte [MAX_READ ];
1057
1059
this .finish = false ;
1060
+ this .flush = false ;
1058
1061
}
1059
1062
1060
1063
@ Override
1061
1064
public void run () {
1062
1065
try {
1063
- while (!Thread .interrupted () && !finish ) {
1064
- int read = in .read (buffer , 0 , in .available ());
1066
+ while (!finish || (flush && in .available () > 0 )) {
1067
+ if (Thread .interrupted ()) {
1068
+ finish = true ;
1069
+ }
1070
+ int read = in .read (buffer , 0 , Math .min (MAX_READ , in .available ()));
1065
1071
if (read == -1 ) {
1066
1072
return ;
1067
1073
}
@@ -1071,9 +1077,15 @@ public void run() {
1071
1077
}
1072
1078
}
1073
1079
1074
- public void finish () {
1075
- Thread .yield ();
1080
+ public void finish (boolean force_flush ) {
1076
1081
finish = true ;
1082
+ flush = force_flush ;
1083
+ if (flush ) {
1084
+ // If we need to flush, make ourselves max priority to pump data out as quickly
1085
+ // as possible
1086
+ setPriority (Thread .MAX_PRIORITY );
1087
+ }
1088
+ Thread .yield ();
1077
1089
}
1078
1090
}
1079
1091
@@ -1099,9 +1111,9 @@ int system(String cmd) {
1099
1111
stdout .start ();
1100
1112
stderr .start ();
1101
1113
int exitStatus = proc .waitFor ();
1102
- stdin .finish ();
1103
- stdout .finish ();
1104
- stderr .finish ();
1114
+ stdin .finish (false );
1115
+ stdout .finish (true );
1116
+ stderr .finish (true );
1105
1117
return exitStatus ;
1106
1118
} catch (IOException | InterruptedException e ) {
1107
1119
return -1 ;
0 commit comments