Subject: | threading timeout Linux bug |
Date: | Wed, 17 Feb 2016 15:36:54 +0000 |
To: | "bug-Net-SFTP-Foreign [...] rt.cpan.org" <bug-Net-SFTP-Foreign [...] rt.cpan.org> |
From: | "Denley, Chris" <Chris.Denley [...] experian.com> |
When Net::SFTP::Foreign is used in Linux from a new thread, the timeout does not work correctly. It does not kill the underlying SSH client which prevents the thread from joining back to the main thread. In fact, it will kill the main thread unless you set an alarm handler.
use Net::SFTP::Foreign;
use threads;
my %args = ( 'user' => 'testuser',
'password' => 'secret',
'host'=> 'hostname',
'timeout' => 10,
'port'=> 22);
# without this, main thread dies with "Alarm clock"
$SIG{ALRM} = 'a';
# start a thread that will take more than 10 seconds to login
my ($t) = threads->create('sftp_thread');
print "Thread started\n";
$t->join();
print "Thread finished\n";
# no problem when not used in separate thread
#&sftp_thread;
sub sftp_thread {
my $sftp = Net::SFTP::Foreign->new(%args);
if($sftp->error) {
print "Failed to connect: ".$sftp->error."\n";
# workaround
#kill "KILL",$sftp->{pid} if($sftp->{pid} =~ /^\d+$/);
}
else {
print "Connected successfully\n";
}
}