You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
StackOverflowException when there are many nested controls #65
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
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.
.. 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).
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.
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!
When I ran the program, it spent around 10 minutes and used up around 4GB memory. Then, it raised StackOverflow exception:
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?
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.
.. 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).
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.
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.
Activity
jamietre commentedon Dec 13, 2012
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.
jamietre commentedon Dec 13, 2012
.. 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).
Fix for Issue #65 and Issue #66.
jamietre commentedon Dec 13, 2012
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.
derickwong commentedon Dec 14, 2012
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.

However, I still got the StackOverflow exception at the highlighted line. Do I have something missed?
jamietre commentedon Dec 14, 2012
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!
derickwong commentedon Dec 14, 2012
When I ran the program, it spent around 10 minutes and used up around 4GB memory. Then, it raised StackOverflow exception:
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.
jamietre commentedon Dec 14, 2012
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
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.
jamietre commentedon Dec 14, 2012
.. actually that selector is pretty slow.. under the circumstances probably it makes sense to just do this:
.. 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).
derickwong commentedon Dec 14, 2012
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.
jamietre commentedon Dec 14, 2012
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.
derickwong commentedon Dec 14, 2012
Ok, no problem. Take your time and thanks to contribute to the open source community! :)
Update OutputFormatterDefault to use a stack instead of recursing (Is…
jamietre commentedon Dec 17, 2012
Updated the output code to use a stack - wasn't too hard after all. Just pushed 1.3.2 to nuget.