Perl Net::SFTP::Foreign disconnect not closing the connection

Viewed 275

While calling $sftp->disconnect() connection is not getting closed and the Perl script is going in hung state until I manually kill the process .

Below is the code how we are creating a SFTP connection :

my %sftp_args = ( user => $username, autodie => 1, stderr_discard => 1,more => qw(-v),
                    timeout => $timeout_secs,ssh_cmd => $SSH_PATH );

  my $sftp = Net::SFTP::Foreign->new($remote_host, %sftp_args);

When we are calling the disconnect method script is getting hung.

$sftp->disconnect();

I tried putting the disconnect is in eval under alarm but still it is not coming back .

 eval {

    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm 25;
    my $retrun = $sftp->disconnect();
    alarm 0;
  };
  my $exception = $@;
  msg("Error Dump".Dumper($exception));
}

Below is the error i am getting in my nohup.out file.

bash: line 1: 27860 Alarm clock   sftp_connection.pl 
1 Answers

After doing the analysis on the Net::SFTP::Foreign Module i was able to find the solution . So Net::SFTP::Foreign module has a bug below are the details , i found this in perldoc:

On some operating systems, closing the pipes used to communicate  the 
slave SSH process does not terminate it and a work around has to be applied. 
If you find that your scripts hung when the $sftp object gets out of scope,
try setting $Net::SFTP::Foreign::dirty_cleanup to a true value

According to the above comment i made the changes in my application and now it is working fine:

my %sftp_args = ( user => $username, autodie => 1, stderr_discard => 1,
                  timeout => $timeout_secs, ssh_cmd => $SSH_PATH, dirty_cleanup => 1 );

my $sftp = Net::SFTP::Foreign->new($remote_host, %sftp_args);
return $sftp;
Related