Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Net::OpenSSH premature session termination

by troy99 (Novice)
on Dec 07, 2011 at 13:10 UTC ( #942242=perlquestion: print w/ replies, xml ) Need Help??
troy99 has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks, I have been trying to use Net::OpenSSH to create shared pipes across a SSH session. These are used to pass simple ASCII messages back and forth across servers. In total, there are 8 of these SSH sessions created using Net::OpenSSH as such:

my %con_opts; $con_opts{host} = $host; $con_opts{timeout} = 120; $con_opts{async} = 1; $ssh{'SSH'} = Net::OpenSSH->new(%con_opts); # For Net::OpenSSH need to specify whether to create # pipes or not, default is no pipes. my %opts; $opts{stdin_pipe} = 1; $opts{stdout_pipe} = 1; $opts{stderr_to_stdout} = 1; # Kick off the script on the remote machine ($ssh{'STDIN'}, $ssh{'STDOUT'}, undef, $ssh{'PID'}) = $ssh{'SSH'}->open_ex(\%opts, $script) or die "Error ".$ssh{$host}->error;
And the STDOUT pipe is setup to be read asynchronously:
my $Q = new Thread::Queue; # Kick this off asynchronously to put things into the # queue to read whenever we get to it async{ while(<$pipe>) { $Q->enqueue( $_ ); } $Q->enqueue( undef ); }->detach; return $Q;

The problem is that seemingly at random, a (or more than 1) Net::OpenSSH SSH session will just terminate. This forces *all* existing Net::OpenSSH SSH sessions to terminate.

Wise Monks, any idea what's going on here to cause the premature exits? Many Thanks

Comment on Net::OpenSSH premature session termination
Select or Download Code
Re: Net::OpenSSH premature session termination
by salva (Abbot) on Dec 07, 2011 at 13:45 UTC
    Probably, when the first thread exits, the DESTROY method is called for all the Net::OpenSSH objects effectively closing the connections.

    Currently the module already has logic to support the case where the process is forked and the destructor is called from the child process so it shouldn't be too difficult to extend it to support the threads case as long as there is a method to detect it.

    So, is there some easy way to detect when an object has been cloned in a new thread?

    As a workaround, in the meantime, call the master_exited method for all Net::OpenSSH objects from every new thread.

      Thanks for the reply Salva! Would this be at the SSH session creation time? Or when the threads are reaped? Should the master_exited call be here? As in:

      # For Net::OpenSSH need to specify whether to create # pipes or not, default is no pipes. my %opts; $opts{stdin_pipe} = 1; $opts{stdout_pipe} = 1; $opts{stderr_to_stdout} = 1; # Kick off the script on the remote machine ($ssh{'STDIN'}, $ssh{'STDOUT'}, undef, $ssh{'PID'}) = $ssh{'SSH'}->open_ex(\%opts, $script) or die "Error ".$ssh{$host}->error; $ssh{$host}->master_exited()
        no, you have to call it from inside the async block.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942242]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-21 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls