Page MenuHomePhabricator

PEG backtracking causes OOMs in some cases
Open, Needs TriagePublic

Description

frwiki:Projet:Football/Maintenance

[dump] HTML5 TreeBuilder-PMU: 98.90210723877
[dump] PEG-PMU: 1794.8142089844

So, in this case, the problem is because PEG is backtracking because of bad markup where {{foo|.. escapes a nowiki because of a markup error. This causes the rest of the page to be processed as a potential template argument, and then on hitting EOF and not seeing a closing }}, PEG backtracks all the way to that line and retries a different tokenization. The backtracking cache ensures that retry can now benefit from previous parses of a number of substrings. *But*, the backtracking cache swells with cached info for the rest of the page.

The backtracking issue can be reproduced with this sample snippet:

* a {{bar|1
* b

== h ==
c

To fix OOM fails on markup errors like this, we need to fix handling of these kinds of broken markup. One crude fix is to limit cache memory available to PEG. This will require fixes to the wikipeg library.

Event Timeline

Should in theory be fixed by T394836 but it's a little non-trivial.

An effective reproduction of the backtracking is this snippet:

* a {{bar|1
* b

== h ==
c
* a {{1x|....}} and [[Foo|other]] matched constructs
* a {{1x|....}} and [[Foo|other]] matched constructs
... and on for a couple of thousand more list items like that ...

The legacy preprocessor (and the refactored grammar of T393391) will still look ahead to the end of the file trying to find the missing {{, but the important thing is that it should ideally *not* allocate space in the peg backtracking cache (or /not as much space/) since the processor rules are "guaranteed never to fail", and so backtracking will never be needed.

We /might/ need to add special support in peg for "never fail rules" (akin to "possessive quantifiers" in regexp, or "naturally possessive" rules). The claim in the "natural possessive" quantifiers article was that we might even want to assert that *all* quantifiers in our wikipeg grammar are 'naturally possessive' to eliminate the possibility of exponential backtracking... that will be an issue for a different task, but we might move one step in that direction here.

(Note that PEG grammars are making an explicit tradeoff between memory and run time, and guarantee quasi-linear runtime in the presence of backtracking by aggressively using memory to cache previous parses. So we need to ensure that managing memory does just turn these bad OOMs into bad timeouts.)

This can't be fixed with the new postprocessing grammar until T394836: Use refactored grammar for `{{....}}` constructs is resolved; right now we are using the postprocessing grammar only for {{#...}} constructs.