Changeset 31956


Ignore:
Timestamp:
2017-09-08T19:08:40+12:00 (7 years ago)
Author:
ak19
Message:

Dr Bainbridge read up on why the Sockets to our wget child process' iostreams were being forcibly closed on Windows when we've finished successfully downloading, resulting unexpectedly in the $len bytes that we sysread() being undefined (usually denoting an error) rather than 0. It turns out that by using Sockets on Windows as filehandles to a child process' iostreams is merely implemented in this manner on success.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/downloaders/WgetDownload.pm

    r31929 r31956  
    295295    local (*TO_CHLD_R,     *TO_CHLD_W);
    296296    local (*FR_CHLD_R,     *FR_CHLD_W);
    297     local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W);
     297    #local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W);
    298298
    299299    if ($^O =~ /Win32/) {
     
    311311                                                                      # see http://blog.0x1fff.com/2009/09/howto-execute-system-commands-in-perl.html
    312312   
    313     return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R );
     313    #return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R );
     314    return ( $pid, *TO_CHLD_W, *FR_CHLD_R);
    314315}
    315316
     
    457458        else { # error or EOF: (!defined $len || $len == 0)         
    458459           
    459             if(!defined $len) { # error reading         
    460             print STDERR "WgetDownload: Error reading from child stream: $!\n";
    461             # SHOULD THIS 'die "errmsg";' instead? - no, sockets may need closing
    462             $error = 1;
    463             }
    464             elsif ($len == 0) { # EOF           
    465             # Finished reading from this filehand $fh because we read 0 bytes.
     460            if(!defined $len) { # could be an error reading
     461              # On Windows, the socket ends up forcibly closed on the "other" side. It's just the way it's implemented
     462              # on Windows when using sockets to our child process' iostreams. So $len not being defined is not an error in that case. Refer to
     463              # https://stackoverflow.com/questions/16675950/perl-select-returning-undef-on-sysread-when-using-windows-ipcopen3-and-ios/16676271
     464              if(!$!{ECONNRESET}) { # anything other ECONNRESET error means it's a real case of undefined $len being an error
     465                print STDERR "WgetDownload: Error reading from child stream: $!\n";
     466                # SHOULD THIS 'die "errmsg";' instead? - no, sockets may need closing
     467                $error = 1;
     468              } else { # $! contains message "An existing connection was forcibly closed by remote host" where "remote" is a reference to the sockets to our wget child process,
     469                    # NOT to the remote web server we're downloading from. In such a case, the error code is ECONNRESET, and it's not an error, despite $len being undefined.
     470                print STDERR "WgetDownload: wget finished\n";
     471              }
     472            }
     473            elsif ($len == 0) { # EOF
     474            # Finished reading from this filehandle $fh because we read 0 bytes.
    466475            # wget finished, terminate naturally
    467476            print STDERR "WgetDownload: wget finished\n";
     
    694703            $error = 1;
    695704            }
     705             if(!defined $len) {
     706              if(!$!{ECONNRESET}) { # anything other ECONNRESET error means it's a real case of undefined $len being an error
     707                #print STDERR "WgetDownload: Error reading from child stream: $!\n";               
     708                $error = 1;
     709              } else { # the error code is ECONNRESET, and it's not an error, despite $len being undefined.
     710                       # Happens on Windows when using sockets to a child process' iostreams
     711                #print STDERR "WgetDownload: wget finished\n";
     712              }
     713            }
    696714            elsif ($len == 0) { # EOF, finished with this filehandle because 0 bytes read
    697715            #print STDERR "WgetDownload: wget finished\n"; # wget terminated naturally
Note: See TracChangeset for help on using the changeset viewer.