18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2008, 2017 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2008, 2018 , Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
24
24
package org .opensolaris .opengrok .util ;
@@ -148,7 +148,7 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
148
148
ProcessBuilder processBuilder = new ProcessBuilder (cmdList );
149
149
final String cmd_str = processBuilder .command ().toString ();
150
150
final String dir_str ;
151
- Timer t = null ; // timer for timing out the process
151
+ Timer timer = null ; // timer for timing out the process
152
152
153
153
if (workingDirectory != null ) {
154
154
processBuilder .directory (workingDirectory );
@@ -186,7 +186,7 @@ public void run() {
186
186
if (reportExceptions ) {
187
187
LOGGER .log (Level .SEVERE ,
188
188
"Error while executing command {0} in directory {1}" ,
189
- new Object [] {cmd_str ,dir_str });
189
+ new Object [] {cmd_str ,dir_str });
190
190
LOGGER .log (Level .SEVERE ,
191
191
"Error during process pipe listening" , ex );
192
192
}
@@ -201,8 +201,8 @@ public void run() {
201
201
*/
202
202
if (this .timeout != 0 ) {
203
203
// invoking the constructor starts the background thread
204
- t = new Timer ();
205
- t .schedule (new TimerTask () {
204
+ timer = new Timer ();
205
+ timer .schedule (new TimerTask () {
206
206
@ Override public void run () {
207
207
LOGGER .log (Level .INFO ,
208
208
"Terminating process of command {0} in directory {1} " +
@@ -217,10 +217,13 @@ public void run() {
217
217
handler .processStream (process .getInputStream ());
218
218
219
219
ret = process .waitFor ();
220
+
220
221
LOGGER .log (Level .FINE ,
221
222
"Finished command {0} in directory {1}" ,
222
223
new Object [] {cmd_str ,dir_str });
223
- process = null ;
224
+
225
+ // Wait for the stderr read-out thread to finish the processing and
226
+ // only after that read the data.
224
227
thread .join ();
225
228
stderr = err .getBytes ();
226
229
} catch (IOException e ) {
@@ -234,19 +237,22 @@ public void run() {
234
237
"Waiting for process interrupted: " + cmdList .get (0 ), e );
235
238
}
236
239
} finally {
240
+ // Stop timer thread if the instance exists.
241
+ if (timer != null ) {
242
+ timer .cancel ();
243
+ }
237
244
try {
238
245
if (process != null ) {
246
+ IOUtils .close (process .getOutputStream ());
247
+ IOUtils .close (process .getInputStream ());
248
+ IOUtils .close (process .getErrorStream ());
239
249
ret = process .exitValue ();
240
250
}
241
251
} catch (IllegalThreadStateException e ) {
242
252
if (process != null ) {
243
253
process .destroy ();
244
254
}
245
255
}
246
- // stop timer thread if the instance exists
247
- if (t != null ) {
248
- t .cancel ();
249
- }
250
256
}
251
257
252
258
if (ret != 0 && reportExceptions ) {
0 commit comments