Changeset 17547

Show
Ignore:
Timestamp:
15.10.2008 17:45:08 (11 years ago)
Author:
ak19
Message:

When subroutines useWget and useWgetMonitored receive the STOP signal from GLI to prematurely terminate wget, these subroutines now wait 5 seconds for wget to have been launched before trying to terminate them. Previously it was possible for the kill signal to have been sent to wget before it started up, in which wget would then start running and continue in the background without anyone noticing that it hadn't gone away as it should have.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/downloaders/WgetDownload.pm

    r17537 r17547  
    100100    close($chld_in); 
    101101     
    102     #print STDOUT "Received termination signal: $termination_signal\n"; 
     102    print STDOUT "Received termination signal: $termination_signal\n"; 
    103103 
    104104    # Send TERM signal to child process to terminate it. Sending the INT signal doesn't work 
     
    240240    }  
    241241    else { # wget finished, terminate naturally 
    242         #print STDERR "\nPerl: open2 command, input stream closed. Wget terminated naturally.\n"; 
     242        #print STDOUT "\nPerl: open2 command, input stream closed. Wget terminated naturally.\n"; 
    243243        close($chld_in); 
    244244        close($chld_out); 
     
    269269        print $client "Talked to ServerSocket (port $port). Connection accepted\n"; 
    270270         
    271         # Read from the client (getting rid of trailing newline) 
     271        # Read from the client (getting rid of the trailing newline) 
    272272        # Has the client sent the <<STOP>> signal? 
    273273        my $signal = <$client>; 
     
    275275        if($signal eq "<<STOP>>") { 
    276276            print $client "Perl received STOP signal (on port $port): stopping wget\n"; 
     277            $loop = 0; 
    277278             
    278             $loop = 0; 
    279             close($chld_in); 
    280             close($chld_out); 
    281             kill("TERM", $childpid); 
    282              
    283             $childpid = undef; 
    284  
    285             # Stop monitoring the read_handle 
    286             # close the serverSocket (the Java end will close the client socket that Java opened) 
    287             $read_set->remove($rh); #$read_set->remove($serverSocket); 
    288             close($rh);         #close($serverSocket); 
    289             #print $client "Perl is about to exit\n"; 
    290             last; 
     279            # Sometimes the wget process takes some time to start up. If the STOP signal 
     280            # was sent, don't try to terminate the process until we know it is running.  
     281            # Otherwise wget may start running after we tried to kill it. Wait 5 seconds 
     282            # for it to start up, checking for whether it is running in order to kill it. 
     283            for(my $seconds = 1; $seconds <= 5; $seconds++) { 
     284            if(kill(0, $childpid)) {  
     285                # If kill(0, $childpid) returns true, then the process is running 
     286                # and we need to kill it. 
     287                close($chld_in); 
     288                close($chld_out); 
     289                kill("TERM", $childpid); 
     290                 
     291                $childpid = undef; 
     292                 
     293                # Stop monitoring the read_handle and close the serverSocket  
     294                # (the Java end will close the client socket that Java opened) 
     295                $read_set->remove($rh);     #$read_set->remove($serverSocket); 
     296                close($rh);             #close($serverSocket); 
     297                print $client "Perl terminated wget and is about to exit\n"; 
     298            } 
     299            else { # the process may just be starting up, wait 
     300                sleep(1); 
     301            } 
     302            } 
     303            last; # we're done here 
    291304        } 
    292305        } 
     
    410423        if($signal eq "<<STOP>>") { 
    411424            print $client "Perl received STOP signal (on port $port): stopping wget\n"; 
     425            $loop = 0; 
    412426             
    413             $loop = 0; 
    414             close($chld_in); 
    415             close($chld_out); 
    416             kill("TERM", $childpid); 
    417              
    418             $childpid = undef; 
    419  
    420             # Stop monitoring the read_handle 
    421             # close the serverSocket (the Java end will close the client socket that Java opened) 
    422             $read_set->remove($rh); #$read_set->remove($serverSocket); 
    423             close($rh);         #close($serverSocket); 
    424             #print $client "Perl is about to exit\n"; 
    425             last; 
     427            # Sometimes the wget process takes some time to start up. If the STOP signal 
     428            # was sent, don't try to terminate the process until we know it is running.  
     429            # Otherwise wget may start running after we tried to kill it. Wait 5 seconds 
     430            # for it to start up, checking for whether it is running in order to kill it. 
     431            for(my $seconds = 1; $seconds <= 5; $seconds++) { 
     432            if(kill(0, $childpid)) {  
     433                # If kill(0, $childpid) returns true, then the process is running 
     434                # and we need to kill it. 
     435                close($chld_in); 
     436                close($chld_out); 
     437                kill("TERM", $childpid); 
     438                 
     439                $childpid = undef; 
     440                 
     441                # Stop monitoring the read_handle and close the serverSocket  
     442                # (the Java end will close the client socket that Java opened) 
     443                $read_set->remove($rh);     #$read_set->remove($serverSocket); 
     444                close($rh);             #close($serverSocket); 
     445                print $client "Perl terminated wget and is about to exit\n"; 
     446            } 
     447            else { # the process may just be starting up, wait 
     448                sleep(1); 
     449            } 
     450            } 
     451            last; # we're done here 
    426452        } 
    427453        }