Skip to content

Commit 7482970

Browse files
author
Vladimir Kotal
authored
close process streams explicitly after the process exits (#2053)
* close process streams explicitly after the process exits fixes #2050
1 parent 0ab5df4 commit 7482970

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/org/opensolaris/opengrok/util/Executor.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
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.
2222
*/
2323

2424
package org.opensolaris.opengrok.util;
@@ -148,7 +148,7 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
148148
ProcessBuilder processBuilder = new ProcessBuilder(cmdList);
149149
final String cmd_str = processBuilder.command().toString();
150150
final String dir_str;
151-
Timer t = null; // timer for timing out the process
151+
Timer timer = null; // timer for timing out the process
152152

153153
if (workingDirectory != null) {
154154
processBuilder.directory(workingDirectory);
@@ -186,7 +186,7 @@ public void run() {
186186
if (reportExceptions) {
187187
LOGGER.log(Level.SEVERE,
188188
"Error while executing command {0} in directory {1}",
189-
new Object[] {cmd_str,dir_str});
189+
new Object[] {cmd_str,dir_str});
190190
LOGGER.log(Level.SEVERE,
191191
"Error during process pipe listening", ex);
192192
}
@@ -201,8 +201,8 @@ public void run() {
201201
*/
202202
if (this.timeout != 0) {
203203
// 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() {
206206
@Override public void run() {
207207
LOGGER.log(Level.INFO,
208208
"Terminating process of command {0} in directory {1} " +
@@ -217,10 +217,13 @@ public void run() {
217217
handler.processStream(process.getInputStream());
218218

219219
ret = process.waitFor();
220+
220221
LOGGER.log(Level.FINE,
221222
"Finished command {0} in directory {1}",
222223
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.
224227
thread.join();
225228
stderr = err.getBytes();
226229
} catch (IOException e) {
@@ -234,19 +237,22 @@ public void run() {
234237
"Waiting for process interrupted: " + cmdList.get(0), e);
235238
}
236239
} finally {
240+
// Stop timer thread if the instance exists.
241+
if (timer != null) {
242+
timer.cancel();
243+
}
237244
try {
238245
if (process != null) {
246+
IOUtils.close(process.getOutputStream());
247+
IOUtils.close(process.getInputStream());
248+
IOUtils.close(process.getErrorStream());
239249
ret = process.exitValue();
240250
}
241251
} catch (IllegalThreadStateException e) {
242252
if (process != null) {
243253
process.destroy();
244254
}
245255
}
246-
// stop timer thread if the instance exists
247-
if (t != null) {
248-
t.cancel();
249-
}
250256
}
251257

252258
if (ret != 0 && reportExceptions) {

0 commit comments

Comments
 (0)