@@ -41,6 +41,8 @@ public class WorkerLifeCycle {
41
41
private CountDownLatch latch ;
42
42
private boolean success ;
43
43
private Connector connector ;
44
+ private ReaderThread errReader ;
45
+ private ReaderThread outReader ;
44
46
45
47
public WorkerLifeCycle (ConfigManager configManager , Model model ) {
46
48
this .configManager = configManager ;
@@ -84,10 +86,24 @@ private String[] getEnvString(String cwd, String modelPath, String handler) {
84
86
return envList .toArray (new String [0 ]); // NOPMD
85
87
}
86
88
87
- public void attachIOStreams (String threadName , InputStream outStream , InputStream errStream ) {
89
+ public synchronized void attachIOStreams (
90
+ String threadName , InputStream outStream , InputStream errStream ) {
88
91
logger .warn ("attachIOStreams() threadName={}" , threadName );
89
- new ReaderThread (threadName , errStream , true , this ).start ();
90
- new ReaderThread (threadName , outStream , false , this ).start ();
92
+ errReader = new ReaderThread (threadName , errStream , true , this );
93
+ outReader = new ReaderThread (threadName , outStream , false , this );
94
+ errReader .start ();
95
+ outReader .start ();
96
+ }
97
+
98
+ public synchronized void terminateIOStreams () {
99
+ if (errReader != null ) {
100
+ logger .warn ("terminateIOStreams() threadName={}" , errReader .getName ());
101
+ errReader .terminate ();
102
+ }
103
+ if (outReader != null ) {
104
+ logger .warn ("terminateIOStreams() threadName={}" , outReader .getName ());
105
+ outReader .terminate ();
106
+ }
91
107
}
92
108
93
109
public void startBackendServer (int port )
@@ -164,6 +180,7 @@ public synchronized void exit() {
164
180
if (process != null ) {
165
181
process .destroyForcibly ();
166
182
connector .clean ();
183
+ terminateIOStreams ();
167
184
}
168
185
}
169
186
@@ -200,6 +217,7 @@ private static final class ReaderThread extends Thread {
200
217
private InputStream is ;
201
218
private boolean error ;
202
219
private WorkerLifeCycle lifeCycle ;
220
+ private boolean isRunning = true ;
203
221
static final org .apache .log4j .Logger loggerModelMetrics =
204
222
org .apache .log4j .Logger .getLogger (ConfigManager .MODEL_METRICS_LOGGER );
205
223
@@ -210,10 +228,14 @@ public ReaderThread(String name, InputStream is, boolean error, WorkerLifeCycle
210
228
this .lifeCycle = lifeCycle ;
211
229
}
212
230
231
+ public void terminate () {
232
+ isRunning = false ;
233
+ }
234
+
213
235
@ Override
214
236
public void run () {
215
237
try (Scanner scanner = new Scanner (is , StandardCharsets .UTF_8 .name ())) {
216
- while (scanner .hasNext ()) {
238
+ while (isRunning && scanner .hasNext ()) {
217
239
String result = scanner .nextLine ();
218
240
if (result == null ) {
219
241
break ;
@@ -234,9 +256,16 @@ public void run() {
234
256
logger .info (result );
235
257
}
236
258
}
259
+ } catch (Exception e ) {
260
+ logger .error ("Couldn't create scanner - {}" , getName (), e );
237
261
} finally {
238
- logger .error ( "Couldn't create scanner - {}" , getName ());
262
+ logger .info ( "Stopped Scanner - {}" , getName ());
239
263
lifeCycle .setSuccess (false );
264
+ try {
265
+ is .close ();
266
+ } catch (IOException e ) {
267
+ logger .error ("Failed to close stream for thread {}" , this .getName (), e );
268
+ }
240
269
}
241
270
}
242
271
}
0 commit comments