Skip to content

StackOverflowException when there are many nested controls #65

Closed

Description

@derickwong

Thanks for your effort and it's really a great project. I used to use HAP and I am now trying to migrate to use CsQuery instead.

By the way, I found an issue (same as HAP) that when I try to parse a HTML document with many nested controls, it flows StackOverflowException.

The HTML string we get from this URL: http://blog.sina.com.cn/s/blog_af65d54c0101b60x.html

Code:
var ContentHtml = CQ.Create(htmlString);
string htmlSubString = ContentHtml["#sina_keyword_ad_area2"].Html();

It flows StackOverflow exception. Somebody said HAP has similar problem and can be fixed by using Queue / Stack collection to replace recursion to solve the problem:
http://htmlagilitypack.codeplex.com/workitem/30268

Can you help on this issue? Thanks.

Activity

  1. jamietre commented on Dec 13, 2012

    @jamietre
    Owner

    Hmm..

    The parser is not my code and changing it to use a stack is probably a lot of work. It would also be a major divergence from the separately-maintained code base.

    On the other hand the original parser that I had written for CsQuery is non-recursing, I am sure it would be able to parse this page. It's also not nearly as complete, though, and might have other problems especially with invalid html.

    Perhaps this is an acceptable tradeoff for these rare situations. I could pretty easily trap stack overflows and fall back to my old parser.

  2. jamietre commented on Dec 13, 2012

    @jamietre
    Owner

    .. btw .. can you post a stack trace where it crashes? You don't need to show the whole thing but maybe the last 10 or 20 lines so I can see whether it's actually in the parser, or in CsQuery.

    This page isn't crashing on my computer, and I can't make it crash in a test with even 10,000 nested controls (though it does get really slow).

  3. added a commit that references this issue on Dec 13, 2012
  4. jamietre commented on Dec 13, 2012

    @jamietre
    Owner

    OK I am pretty sure this was not a bug in the parser but rather a bug in another function that used recursion. I changed it - tested to a depth of 15,000, it doesn't crash (though it is really slow).

    Nothing should really be doing this (it practically seems designed to crash a parser) so I'm not going to worry about the speed, this should solve your problem with that page.

  5. derickwong commented on Dec 14, 2012

    @derickwong
    Author

    Thanks for your quick reply and fix. Have you put the fix online?

    I have tried to download the latest code from here and create a simple project with reference to the new dll.
    Screen Shot 2012-12-14 at 12 34 48 PM

    However, I still got the StackOverflow exception at the highlighted line. Do I have something missed?

  6. jamietre commented on Dec 14, 2012

    @jamietre
    Owner

    I was not actually able to reproduce it - I was just guessing that the only recursive code I could find was causing it. The parser isn't actually recursive, it uses a stack, so I don't really have any other ideas what might be causing it now.

    If you could post a partial stack trace it would be most helpful!

  7. derickwong commented on Dec 14, 2012

    @derickwong
    Author

    When I ran the program, it spent around 10 minutes and used up around 4GB memory. Then, it raised StackOverflow exception:

    StackOverFlow

    It's strange that you cannot reproduce it. Are you really able to load the page and get the html from the dom["#sina_keyword_ad_area2"].Html() function?

    Thanks a lot.

  8. jamietre commented on Dec 14, 2012

    @jamietre
    Owner

    OK - the problem is occurring in the Html() function, I didn't understand it was happening when rendering, not when parsing. I never tried that.

    Yeah that process would be recursive :) That will be a little harder to fix.

    If I were you I would deal with this page manually - the HTML that's causing the problem is junk, I don't even think the font tag does anything these days. Just do something like

    dom["font > font > font"].First().Remove();
    

    after loading to clean it up. (Testing for 3 nested font tags in a row should kill the whole tree)

    I think it would be useful to change the renderer to operate in a non-recursive manner and I will do that as soon as I can but it's going to be a little harder to fix than what I thought was causing the problem.

  9. jamietre commented on Dec 14, 2012

    @jamietre
    Owner

    .. actually that selector is pretty slow.. under the circumstances probably it makes sense to just do this:

    var fonts = dom["font"];
    

    .. and then loop through the results looking for the pattern (first child is another font tag or something?) and then stop after you remove the first match. (Selecting just on tag is far faster than the child selector when there are so many matches).

  10. derickwong commented on Dec 14, 2012

    @derickwong
    Author

    Yes, that's rare case and should be handled manually. However, it is hard to predict what html tag to be used as junk tag. Next time may be div rather font tag. Therefore, it is hard to remove or skip junk tags. Even worse, .Net program will dead when stackoverflow occurs, it cannot be catched by try-catch starting from .Net framework 2.0.

    Anyway, thanks for your suggestion and looking forward to get your new release.

  11. jamietre commented on Dec 14, 2012

    @jamietre
    Owner

    I agree - it needs to be addressed -- just suggesting a way to get through the problem right now. I am sure I can change the rendering algo to use a stack but it's not totally trivial so might take me a week or two to find the time.

  12. derickwong commented on Dec 14, 2012

    @derickwong
    Author

    Ok, no problem. Take your time and thanks to contribute to the open source community! :)

  13. jamietre commented on Dec 17, 2012

    @jamietre
    Owner

    Updated the output code to use a stack - wasn't too hard after all. Just pushed 1.3.2 to nuget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions