If I trigger an alarm, subsequent reading from a filedescriptor seems broken in an odd way :
$ ./alarmfail.pl 1
TIMEOUT=1
SLEEP=5

(sub1):begin_pos:0
#MAIN, a
#MAIN, b
#MAIN, c
(sub1):sleeping...
(sub1):timed_out!
(sub1):end_pos:52

(sub2):begin_pos:0
(sub2):end_pos:1

(sub2):begin_pos:0
(sub2):end_pos:1
Code works as expected when alarm isn't triggered :
$ ./alarmfail.pl 10
TIMEOUT=10
SLEEP=5

(sub1):begin_pos:0
#MAIN, a
#MAIN, b
#MAIN, c
(sub1):sleeping...
(sub1):end_pos:52

(sub2):begin_pos:0
#MAIN, a
#MAIN, b
#MAIN, c
(sub2):end_pos:52

(sub2):begin_pos:0
#MAIN, a
#MAIN, b
#MAIN, c
(sub2):end_pos:52
Note: The code is an example only, I'm unable to move the inline '#MAIN' tags to a __DATA__ section in my legacy codebase. So perhaps this is a quirk of opening a filehandle to the script itself?
#!/usr/bin/env perl # Example code to show how reading from file descriptors fails after a +n alarm is triggered? use strict; use warnings; my $timeout = defined($ARGV[0]) ? $ARGV[0] : 2; my $sleep = 5; print "TIMEOUT=$timeout\n"; print "SLEEP=$sleep\n\n"; #MAIN, a #MAIN, b #MAIN, c sub sub1 { open(FD1, "$0") or die "ERROR: Could not open $0 : $!\n"; $. = defined($.) ? $. : 0; print "(sub1):begin_pos:$.\n"; while (<FD1>){ next if ! /^#MAIN|^#DATA/; print $_; } eval { local $SIG{ALRM} = sub { die "alarm\n"; }; alarm $timeout; print "(sub1):sleeping...\n"; my $cmd = `sleep $sleep 2>&1;echo slept`; alarm 0; }; if ($@){ print "(sub1):timed_out!\n"; } else { # did not time out } print "(sub1):end_pos:$.\n\n"; close FD1 or die "$!\n"; } sub sub2 { open(FD2, "$0") or die "ERROR: Could not open $0 : $!\n"; print "(sub2):begin_pos:$.\n"; while (<FD2>){ next if ! /^#MAIN|^#DATA/; print $_; } print "(sub2):end_pos:$.\n\n"; close FD2 or die "$!\n"; } &sub1; &sub2; &sub2;

In reply to alarm and reading from file decriptor by tomtastic

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.