Thanks for this, it has greatly helped with some cases where the tests I'm running actually hang or fail (due to bad hardware or inserted faults).

Here's what I ended up actually implementing

use threads; use Thread::Queue; use threads::shared; use IPC::Open2; sub pipeCommand ($$$$;@) { my $uut = shift; my $cmd = shift; my $test = shift; my $wd = $homedir; $wd .= shift; my @input = @_ ? @_ : (); my $thread = async { my $tid = threads->tid(); my ( $out, $in ); $cmd = "cd ".$wd."; ".$cmd; my $pid = open2( $out, $in, $cmd ); print $in "$_\n" foreach (@input); close $in or warn "close of input: $! $?\n"; my $err = 0; while (<$out>) { last if ($die); #drops me out of loop and lets me exit cl +eanly chomp; $err = 1 if (/^thread failed/); next unless length $_; $Q->enqueue("$tid:$uut:$test:$_"); last if ($err); } kill( 2, $pid ) if ($err or $die); close $out or warn "close of output: $! $?\n"; my $kpid = waitpid ($pid, 0); $Q->enqueue("$tid:$uut:$test:ENDEND"); return; }; $thread->detach(); return ( $thread->tid() ); }

then I call set die in one of two ways: either via setting it at the end of the while(1) loop, or when an alarm is triggered

my $tests = 7; # a number of tests sub recv_results { my $done = 0; my $err_count = 0; while (1) { if ( $Q->pending ) { my $line = $Q->dequeue; $_ = $line; chomp; if (/ENDEND$/) { $done++; } last if ( $done == $t_count ); last if (/^DIEDIE$/); next if (/ENDEND$/); if (/some pattern/) { #log something } else { #log something else } } else { usleep 5000; } } return $err_count; }
sub ALRM_handler { alarm 0; $Q->enqueue("DIEDIE"); $die = 1; }
my $time = 3600; #or some number of seconds launch_tests(); $SIG{ALRM} = 'ALRM_handler'; my $alrm = $time + 35; #little extra time for init and close alarm $alrm; my $err_count = recv_results(); $die = 1; #this will force any threads that are still stuck in the +while loop to exit

At this point, I never have tests that mysteriously fail to start. (thanks for the pointer on the possible race condition), I always have every test that is started send its end to the results receiver (sometimes being voilently killed via the alarm and setting of die, which does go and kill the pid for the open process, close out, etc), and yet even still, I always have two threads upon exit; even when nothing goes wrong and everything completes normally.

I'm going to try a little bit with the join method instead of detach, but at this point i'm almost inclined to turn on ignore for them, as one of the monks had suggested. Since the OS seems to be cleaning them up for me okay...

however, any other thoughts would be welcome


In reply to Re^2: cleanly exiting threads by JoeKamel
in thread cleanly exiting threads by JoeKamel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.