Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello all. I'm using XML::LibXML to parse some HTML. Mostly it's working great - fast and very useful XPath support. My problem is that it's choking on some very bad HTML in a very bad way - it's sitting on the CPU until killed manually. I expected some HTML wouldn't parse, so this isn't such a tragedy. What is a big problem is that my attempt to work around this with alarm() aren't working!

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.


In reply to Problem timing out XML::LibXML parsing by samtregar

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found