Commit 4c78692
committed
[IMP] jinja_to_qweb: avoid high memory use by lxml/libxml2
upg-2994884
Avoid `MemoryError` and/or killed process because of `malloc()` failing within
`lxml` / `libxml2`.
Debugging this by determining the size of involved datastructures through means
of `sys.getsizeof()` showed that the overly large memory consumption has 3
different sources:
1. During conversion: The global variable `templates_to_check` grows to
hundreds of MiB after the various calls to `upgrade_jinja_fields()` by
upgrade scripts.
2. During conversion: The call to `cr.dictfetchall()` to gather all
templates(fields) that are to be converted, already consumes hundreds of
MiB.
3. At the start of function `verify_upgraded_jinja_fields()`, the process is
at ~1.5GiB because of (1) and (2). While iterating over all the templates in
`templates_to_check`, no significant amount of memory is allocated on top of
this *by python datastructures*. But, with each call to
`is_converted_template_valid()`, the size of the process increases until it
hits the RLIMIT. This function calls into `lxml` multiple times, suggesting
that the memory is allocated in `malloc()` calls in the `lxml` and/or
`libxml2` C library/ies, evading python's memory accounting and garbage
collection.
Internet research shows that `lxml` has a long history of different memory
leaks in C code, plus some caching mechanism *across documents* that could
be responsible[^1]. More recent versions of the module seem to have been
improved, but still we're stuck with old versions.
This patch solves / works around (3) by running the function body of
`is_converted_template_valid()` in a subprocess that is killed immediately
after and thus no additional memory is ever allocated by `lxml` in the main
process.
[^1]: https://benbernardblog.com/tracking-down-a-freaky-python-memory-leak-part-2/
closes #288
Signed-off-by: Christophe Simonis (chs) <[email protected]>1 parent 446ff3a commit 4c78692
1 file changed
+36
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
544 | 545 | | |
545 | 546 | | |
546 | 547 | | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
571 | 575 | | |
572 | | - | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
573 | 584 | | |
574 | 585 | | |
575 | 586 | | |
| |||
0 commit comments