Alarm Clock Bug w/ithreads

[email protected] (Steve Schein)
Newsgroups perl.ithreads
Message-ID <[email protected]>
All:

I bumped into a bug reported by Elizabeth Mattijsen referred to in a
post found on Google Groups (link follows):

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=rt-16807-35884.2.54002486802122%40bugs6.perl.org&rnum=1&prev=/groups%3Fq%3Dperl%2Balarm%2Bclock%2Bthreads%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3Drt-16807-35884.2.54002486802122%2540bugs6.perl.org%26rnum%3D1

When an alarm is set within thread, a program can bomb with an "Alarm
Clock" error.

I can reproduce this error repeatedly in my own program.

Elizabeth states verbatim:

"If you try to do alarms within a thread, and $SIG{ALRM} was not 
assigned in the main thread previously, an alarm() that goes
off in the thread will bomb the program completely with the
mysterious message

Alarm clock

being sent to STDERR.  It bombs in such a way that I'm not able
to get valgrind to give me a trace (yet).

This behaviour can be circumvented by assigning $SIG{ALRM} with
something (except the empty string) in the main thread before
the thread starts.

Further investigations reveal that assigning in a surrounding
thread that is not the main thread, does _not_ work, it _must_
be the main thread.  However, other threads _may_ be running
before the $SIG{ALRM} is set in the main thread, but of course
these threads can not use alarm() reliably then. "


<re-enter Steve>

I attempted to use the circumvention patch by entering the following
statement in the main program prior to any threads launching:

$SIG{ALRM} = 'a' if @ARGV;

Unfortunately, this did not work and the Alarm Clock bug continues. 
Here is the code inside the thread that contains the alarm:

 $SIG{ALRM} = sub {die "timeout" };
    eval {
	alarm($time_out);
	print "doing: [$command]\n";
	$web_string = `$command`;
	alarm(0);
    };
    if ($@) {
	if ($@ =~ /timeout/) {
	    print "OOPS! TIMED OUT!\n";
	    #timeout action
	    $web_string = "";
	    my $current_time = localtime();
	    open(HARVESTLOG, ">>HARVEST.LOG");
	    print HARVESTLOG "$current_time   $URL\ttimeout\n";
	    close(HARVESTLOG);
	    
	}
	else {
	    # should never get here
	}
    }
    else { # successful
	print "Successfully reached site!\n\n";
	my $web_string_length = length($web_string);

    } # end else

<re-enter Steve>

I am using Perl v5.8 on a Mandrake Linux v9.1 platform.  I am hopeful
that there are additional circumvention methods that have now been
developed to manage this problem.

I would appreciate your insight and suggestions.

Thanks and best,

Steve
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.