Why not use another thread to effect a timeout?

Stick the code that is currently in the main thread into an async() block, have the real main wait for some specified number of seconds before dying.

I've attempted to use is_running() to produce an indication that it was hung, but that will only work if you have jdhedden's cpan version.

use strict; use warnings; use threads; $|=1; my $niter = 10; # Seems to mostly work with $nforks = 2 but usually fails with $nforks + = 3. my $nforks = 3; my $Cmd = 'invalid_command'; warn "start niter=$niter nforks=$nforks cmd='$Cmd'\n"; sub do_one_kid { my $kid = shift; my $tid = threads->self->tid; warn "kid $kid (tid:$tid) pid=$$ run cmd='$Cmd'\n"; for my $i (1..$niter) { my $out = `$Cmd 2>&1`; my $rc = $? >> 8; warn "$i: kid $kid pid=$$ rc=$rc\n"; } warn "kid $kid pid=$$ exit\n"; return 42; } my @kids = (); for my $n (1..$nforks) { warn "$n: forking\n"; my $t = threads->new(\&do_one_kid, $n); warn "I am the parent\n"; push(@kids, $t); } my $main = async { for my $t (@kids) { my $tid = $t->tid; warn "parent waiting for $tid\n"; my $rc = $t->join(); warn "parent $$: thread $tid exited rc=$rc\n"; } }; sleep 10; warn "'main' thread still running (hung); aborting" if $main->is_runni +ng(); warn "end main\n";

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: How to write a test for backticks threads hang by BrowserUk
in thread How to write a test for backticks threads hang by eyepopslikeamosquito

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.