in reply to threads and Net::OpenSSH

The first thing you should do is to ensure that you have installed and are using the latest stable version of the module, 0.57 as it incorporates some bug fixes affecting threads.

If you are already using 0.57, enable debugging and post here the output:

$Net::OpenSSH::debug = -1;

Replies are listed 'Best First'.
Re^2: threads and Net::OpenSSH
by rastoboy (Monk) on Mar 21, 2012 at 17:39 UTC
    Apologies for the delay in responding. I was indeed at version 0.50, and I was about to upgrade when I realized that I was actually dependent on this behavior to get my job done :-) So I finished my work before upgrading.

    After upgrading, the join() on the tail process hangs forever, as one might expect.

    Problem solved (sorta!). In any case now I can work on elegant ways to kill the threads, and anyways feel like what's going on makes sense.

    Thanks!

      In any case now I can work on elegant ways to kill the threads ...

      There is no "elegant way to kill a thread".

      Your best solution, assuming it is possible, would be to enable a timeout on the ssh connection.

      Your second best solution would be to use a timer/signal to interrupt the read. That is difficult (if not impossible), due to Perl SAFE SIGNALS.

      The third best solution would be to set the socket non-blocking and only read when there is something available. (Ie.Poll!)

      The problem with killing the thread -- assuming you discover a reliable mechanism for doing so(*) -- is that all the data you've already so carefully arranged to capture, would be discarded. Ie. As you are returning the captured data as the return value from the thread, you'll never see it if you kill the thread.

      (*)The problem here is that to kill the thread, you first need to interrupt the read. And if you can interrupt the read, there is no need to kill the thread.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      In any case now I can work on elegant ways to kill the threads

      This is elegant enough for me, but I'm easy. :-)

      #!/usr/bin/perl -w use strict; use threads; use threads::shared; my $timer_go:shared = 0; my $worker = threads->create(\&worker); my $timer = threads->create(\&timer,$worker)->detach(); print "hit enter to start\n"; <>; $timer_go=1; while( (scalar threads->list) > 0 ){ print scalar threads->list,"\n"; sleep 1; foreach my $thread (threads->list) { if( $thread->is_joinable ){ $thread->join;} } } print "worker joined, all done\n"; exit; sub timer { my $worker = shift; while(1){ if($timer_go){ my $count = 0; while(1){ $count++; if($count > 5){ print "timed out\n"; # Send a signal to a thread $worker->kill('INT'); return; # will destroy a detached thread } sleep 1; print "timing $count\n"; } }else{sleep 1} } } sub worker { $|++; $SIG{INT} = sub{ warn "Caught Zap!\n"; threads->exit() }; # threads->exit() will exit thread only while(1){sleep 1; next} return; }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        Oh zentara. How many times(*) do I have to explain it. (*there are many, many previous occasions if anyone cares to do the search.)

        This will not work if the thread is in a blocking IO read state. Ie. The OPs problem.

        Hell. You even meditated on it!

        You're not related to sundialsvc4 are you?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?