Saltar navegación

[Escalado a 2º nivel] Sitemap petitions exhaust memory

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Este tema contiene 1 respuesta, tiene 1 mensaje.

Última actualización por - 8 hours, 23 minutes ago.

Asistido por: Christopher Amirian.

Autor
Mensajes
#2920050

Hello,

On our site, the native WordPress sitemap for posts (wp-sitemap-posts-post-1.xml) returns HTTP 500 because of a memory exhaustion triggered by Toolset Types. We have identified the exact callback responsible and have a workaround, but we believe this should be fixed in the plugin.

ENVIRONMENT
- Toolset Types 3.6.5 (latest available; the issue is also present in the previous version)
- WordPress 7.1.5, PHP 8.5 (Apache module), memory_limit = 128M
- Ubuntu server, MySQL
- Plugin directory: wp-content/plugins/types

SYMPTOM
Request: GET /es_ES/wp-sitemap-posts-post-1.xml/ -> HTTP 500
PHP error log:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2097152 bytes) in [site]/wp-includes/class-wpdb.php on line 2324
Stack trace: #0 class-wpdb.php(2324): mysqli_fetch_object()

Only the sitemap of the "post" type failed. Sitemaps of taxonomies and users work. Sitemaps of our custom post types are lighter and did not fail, but they were also consuming far more memory than needed.

HOW TO REPRODUCE (WP-CLI)
wp eval '
$p = wp_sitemaps_get_server()->registry->get_provider("posts");
$u = $p->get_url_list(1, "post");
echo count($u) . " urls, peak: " . round(memory_get_peak_usage()/1048576) . " MB\n";
'

Result with Types active: 1998 urls, peak 426 MB
Result with --skip-plugins=types (Types disabled): 1998 urls, peak 78 MB

ROOT CAUSE
We listed all callbacks added by Types/Toolset to the post query hooks (pre_get_posts, posts_where, posts_join, posts_clauses, the_posts, etc.) and measured the extra memory with each one removed in turn:

- Removing ONLY WPCF_Loader::wpcf_cache_complete_postmeta from the "the_posts" filter (priority 10; file wp-content/plugins/types/vendor/toolset/types/embedded/classes/loader.php, line 68) reduces the extra memory from about 351 MB to about 0 MB.

That function runs an unbounded query:

SELECT * FROM {$wpdb->postmeta} WHERE post_id IN ($id_list)

for all posts returned by the query, loads every row into PHP as an object, regroups them in an array and then adds them again to the object cache (cache group "types_cache"). It does not check whether the query asked for meta caching: the WordPress core sitemap query sets update_post_meta_cache to false precisely to avoid this, but the preload happens anyway.

The memory used scales with the NUMBER OF ROWS, not with the size of the values. For our 1998 published posts, wp_postmeta holds 309,387 rows (155 per post) with only 4.1 MB of data in total. That is roughly 1 KB of PHP memory per row.

ADDITIONAL OBSERVATION
Some of the extra memory also depends on the Types relationships query extension (OTGS\Toolset\Common\Relationships\DatabaseLayer\Version2\WpQueryExtension\Extension: process_legacy_meta_query, check_custom_query_args, posts_where, posts_join). Removing any one of these four callbacks lowers the extra memory from about 351 MB to about 149 MB, even though without wpcf_cache_complete_postmeta the total is 0 MB. We do not know why they interact; you may.

WORKAROUND WE USE
A mu-plugin that removes wpcf_cache_complete_postmeta from "the_posts" only on requests to wp-sitemap-posts-*. With it, the peak of every sitemap in a real web request is between 8 and 23 MB, all returning HTTP 200, and no function of the site appears affected.

REQUEST
Please consider one or more of the following:
1. Skip the preload when the query has update_post_meta_cache = false (as in core sitemaps and other internal queries).
2. Limit the preload to the meta keys Types really needs, and/or process the IDs in chunks, instead of SELECT * over all rows.
3. Avoid storing the same data twice (arrays of row objects and the object cache).
4. Review the interaction with the relationships query extension described above.

We can provide more data (full hook inventory, SQL output, test script) if useful.

Thank you.

#2920121

Christopher Amirian
Colaborador

Idiomas: Inglés (English )

Hello,

Thank you for the detailed report and for finding the cause.

I reproduced it on a clean WordPress install with only Toolset Types and a default theme. The posts sitemap returns a 500 error once the site has many posts with many custom field rows, and it works when the Types function you found is skipped.

I have escalated this to our developers with your findings and your suggestions for the fix.

Until a fix is released, please keep your must-use plugin in place. It only changes the sitemap requests, so the rest of the site is not affected.

I will update this ticket when I have news from the developers.