samtregar has asked for the wisdom of the Perl Monks concerning the following question:
Here's my code:
use strict; use warnings; use XML::LibXML; my $html = do { local $/; <> }; my $libxml = XML::LibXML->new(); #$libxml->recover(2); eval { local $SIG{ALRM} = sub { die "TIMEOUT\n" }; alarm(10); $libxml->parse_html_string($html); alarm(0); }; if ($@ and $@ eq "TIMEOUT\n") { warn "Timed out ok.\n"; } elsif ($@) { die $@; }
If I replace the parse call with sleep(20) then it works as expected - the alarm triggers and the timeout is caught. If I run it as-is with my sample HTML then it never stops until killed. If you want to play along at home here's the test file:
http://sam.tregar.com/libxml-fail.html
BEWARE: that's some really bad HTML and it not only breaks XML::LibXML but it also crashed Firefox while I was writing this post the first time! You probably don't want to load it in your browser.
I've never had alarm() fail like this. Is there an alternative I can try? Any other ideas about how to handle this?
Thanks!
-sam
UPDATE: perrin reminded me about how safe-signals work in recent perls. That is indeed the problem - setting PERL_SIGNALS=unsafe makes my code DWIM, at the cost of a certain degree of safety. Ideas for alternatives are still welcome of course.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Problem timing out XML::LibXML parsing
by gwadej (Chaplain) on Feb 03, 2009 at 20:26 UTC | |
by hobbs (Monk) on Feb 04, 2009 at 00:54 UTC | |
Re: Problem timing out XML::LibXML parsing
by hbm (Hermit) on Feb 03, 2009 at 20:21 UTC | |
by samtregar (Abbot) on Feb 03, 2009 at 20:25 UTC | |
Re: Problem timing out XML::LibXML parsing
by mirod (Canon) on Feb 03, 2009 at 20:01 UTC | |
by samtregar (Abbot) on Feb 03, 2009 at 20:12 UTC |