Changeset 31642 for main


Ignore:
Timestamp:
05/02/17 19:41:53 (4 years ago)
Author:
ak19
Message:

For Windows, process.destroy() doesn't get called on interruption because the join() calls complete and process is set to null, so not triggering process.destroy(). Now the code explicitly triggers process.destroy() on cancel/interruptedException and on any Exception, by means of new member forciblyTerminateProcess. And the code no longer sets process to null until the very end of finishing up with the process.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java

    r31639 r31642  
    3939    private String inputStr = null;
    4040    private Process process = null;
    41 
     41    private boolean forciblyTerminateProcess = false;
     42   
    4243    // output from running SafeProcess.runProcess()
    4344    private String outputStr = "";
     
    138139                   SafeProcess.InputStreamGobbler errorGobbler)
    139140    throws IOException, InterruptedException
    140     {
    141 
    142    
     141    {   
    143142    // kick off the stream gobblers
    144143    inputGobbler.start();
     
    176175        errorGobbler.interrupt();
    177176        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       
    179183        // even after the interrupts, we want to proceed to calling join() on all the worker threads
    180184        // in order to wait for each of them to die before attempting to destroy the process if it
     
    204208        // set the variables that the code which created a SafeProcess object may want to inspect
    205209        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();       
    211211    }
    212212
     
    226226    public int runBasicProcess() {
    227227    try {
     228        this.forciblyTerminateProcess = true;
     229       
    228230        // 1. create the process
    229231        process = doRuntimeExec();
     
    231233        this.exitValue = process.waitFor();
    232234
    233        
    234     } catch(IOException ioe) {
     235        this.forciblyTerminateProcess = false;
     236    } catch(IOException ioe) {     
     237       
    235238        if(exceptionHandler != null) {
    236239        exceptionHandler.gotException(ioe);
     
    239242        }
    240243    } catch(InterruptedException ie) {
    241 
     244       
    242245        if(exceptionHandler != null) {
    243246        exceptionHandler.gotException(ie);
     
    249252    } finally {
    250253
    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
    254259    }
    255260    return this.exitValue;
     
    273278
    274279    try {
     280        this.forciblyTerminateProcess = false;
     281       
    275282        // 1. get the Process object
    276283        process = doRuntimeExec();
     
    311318       
    312319    } catch(IOException ioe) {
     320        this.forciblyTerminateProcess = true;
     321
    313322        if(exceptionHandler != null) {
    314323        exceptionHandler.gotException(ioe);
     
    317326        }
    318327    } catch(InterruptedException ie) { // caused during any of the gobblers.join() calls, this is unexpected so print stack trace
    319        
     328        this.forciblyTerminateProcess = true;
     329       
    320330        if(exceptionHandler != null) {
    321331        exceptionHandler.gotException(ie);
     
    332342        //log("*** In finally of SafeProcess.runProcess(3 params): " + cmd);
    333343
    334         if( process != null ) {
     344        if( this.forciblyTerminateProcess ) {
    335345        log("*** Going to call process.destroy 2");
    336346        process.destroy();
    337         process = null;
    338347        log("*** Have called process.destroy 2");
    339348        }
    340        
     349        process = null;
     350        this.forciblyTerminateProcess = false; // reset
    341351    }
    342352   
     
    351361
    352362    try {
     363        this.forciblyTerminateProcess = false;
     364       
    353365        // 1. get the Process object
    354366        process = doRuntimeExec();
     
    383395       
    384396    } catch(IOException ioe) {
     397        this.forciblyTerminateProcess = true;
     398       
    385399        if(exceptionHandler != null) {
    386400        exceptionHandler.gotException(ioe);
     
    389403        }
    390404    } catch(InterruptedException ie) { // caused during any of the gobblers.join() calls, this is unexpected so log it
    391        
     405        this.forciblyTerminateProcess = true;
     406       
    392407        if(exceptionHandler != null) {
    393408        exceptionHandler.gotException(ie);
     
    415430        //log("*** In finally of SafeProcess.runProcess(2 params): " + cmd);
    416431
    417         if( process != null ) {
     432        if( this.forciblyTerminateProcess ) {
    418433        log("*** Going to call process.destroy 1");
    419434        process.destroy();
     435        log("*** Have called process.destroy 1");
     436        }
    420437        process = null;
    421         log("*** Have called process.destroy 1");
    422         }
     438        this.forciblyTerminateProcess = false; //reset     
    423439    }
    424440   
Note: See TracChangeset for help on using the changeset viewer.