- Timestamp:
- 2017-05-02T19:41:53+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java
r31639 r31642 39 39 private String inputStr = null; 40 40 private Process process = null; 41 41 private boolean forciblyTerminateProcess = false; 42 42 43 // output from running SafeProcess.runProcess() 43 44 private String outputStr = ""; … … 138 139 SafeProcess.InputStreamGobbler errorGobbler) 139 140 throws IOException, InterruptedException 140 { 141 142 141 { 143 142 // kick off the stream gobblers 144 143 inputGobbler.start(); … … 176 175 errorGobbler.interrupt(); 177 176 outputGobbler.interrupt(); 178 177 178 // Since we have been cancelled (InterruptedException), or on any Exception, we need 179 // to forcibly terminate process eventually after the finally code first waits for each worker thread 180 // to die off. Don't set process=null until after we've forcibly terminated it if needs be. 181 this.forciblyTerminateProcess = true; 182 179 183 // even after the interrupts, we want to proceed to calling join() on all the worker threads 180 184 // in order to wait for each of them to die before attempting to destroy the process if it … … 204 208 // set the variables that the code which created a SafeProcess object may want to inspect 205 209 this.outputStr = outputGobbler.getOutput(); 206 this.errorStr = errorGobbler.getOutput(); 207 208 // Since we didn't have an exception, process should have terminated now (waitFor blocks until then) 209 // Set process to null so we don't forcibly terminate it below with process.destroy() 210 this.process = null; 210 this.errorStr = errorGobbler.getOutput(); 211 211 } 212 212 … … 226 226 public int runBasicProcess() { 227 227 try { 228 this.forciblyTerminateProcess = true; 229 228 230 // 1. create the process 229 231 process = doRuntimeExec(); … … 231 233 this.exitValue = process.waitFor(); 232 234 233 234 } catch(IOException ioe) { 235 this.forciblyTerminateProcess = false; 236 } catch(IOException ioe) { 237 235 238 if(exceptionHandler != null) { 236 239 exceptionHandler.gotException(ioe); … … 239 242 } 240 243 } catch(InterruptedException ie) { 241 244 242 245 if(exceptionHandler != null) { 243 246 exceptionHandler.gotException(ie); … … 249 252 } finally { 250 253 251 if( process != null ) { 252 process.destroy(); // see runProcess() below 253 } 254 if( this.forciblyTerminateProcess ) { 255 process.destroy(); // see runProcess() below 256 } 257 process = null; 258 this.forciblyTerminateProcess = false; // reset 254 259 } 255 260 return this.exitValue; … … 273 278 274 279 try { 280 this.forciblyTerminateProcess = false; 281 275 282 // 1. get the Process object 276 283 process = doRuntimeExec(); … … 311 318 312 319 } catch(IOException ioe) { 320 this.forciblyTerminateProcess = true; 321 313 322 if(exceptionHandler != null) { 314 323 exceptionHandler.gotException(ioe); … … 317 326 } 318 327 } catch(InterruptedException ie) { // caused during any of the gobblers.join() calls, this is unexpected so print stack trace 319 328 this.forciblyTerminateProcess = true; 329 320 330 if(exceptionHandler != null) { 321 331 exceptionHandler.gotException(ie); … … 332 342 //log("*** In finally of SafeProcess.runProcess(3 params): " + cmd); 333 343 334 if( process != null) {344 if( this.forciblyTerminateProcess ) { 335 345 log("*** Going to call process.destroy 2"); 336 346 process.destroy(); 337 process = null;338 347 log("*** Have called process.destroy 2"); 339 348 } 340 349 process = null; 350 this.forciblyTerminateProcess = false; // reset 341 351 } 342 352 … … 351 361 352 362 try { 363 this.forciblyTerminateProcess = false; 364 353 365 // 1. get the Process object 354 366 process = doRuntimeExec(); … … 383 395 384 396 } catch(IOException ioe) { 397 this.forciblyTerminateProcess = true; 398 385 399 if(exceptionHandler != null) { 386 400 exceptionHandler.gotException(ioe); … … 389 403 } 390 404 } catch(InterruptedException ie) { // caused during any of the gobblers.join() calls, this is unexpected so log it 391 405 this.forciblyTerminateProcess = true; 406 392 407 if(exceptionHandler != null) { 393 408 exceptionHandler.gotException(ie); … … 415 430 //log("*** In finally of SafeProcess.runProcess(2 params): " + cmd); 416 431 417 if( process != null) {432 if( this.forciblyTerminateProcess ) { 418 433 log("*** Going to call process.destroy 1"); 419 434 process.destroy(); 435 log("*** Have called process.destroy 1"); 436 } 420 437 process = null; 421 log("*** Have called process.destroy 1"); 422 } 438 this.forciblyTerminateProcess = false; //reset 423 439 } 424 440
Note:
See TracChangeset
for help on using the changeset viewer.