wp_suspend_cache_addition( true ) is never restored, degrading the object cache
-
The forum editor mangles the formatting of this report, so a properly formatted copy is here: https://gist.github.com/curtismchale/04070858f1cf20d9211d3ad89b3112cb β same text, readable code blocks.
Fix Alt Text calls
wp_suspend_cache_addition( true )at the top of itssave_post,attachment_updated,add_attachment,saved_termanddelete_termhandlers, and never callswp_suspend_cache_addition( false )afterward. Adefine()guard prevents the call from repeating, so the suspension is set once and stays set for the remainder of the PHP request.wp_suspend_cache_addition()gateswp_cache_add()andwp_cache_add_multiple(), which is exactly what core’supdate_meta_cache()and_prime_post_caches()use to populate their caches. So from the first post save onward, cache priming silently no-ops and every subsequentget_post_meta()/metadata_exists()/ post lookup goes back to the database.This is a performance and correctness bug, not a security issue. It is most visible on
wp-admin/post-new.php(which always creates an auto-draft and therefore always firessave_post), and on any long-running request that saves posts and keeps working: importers, cron jobs, bulk edits, programmaticwp_insert_post()loops.AFFECTED VERSION
Fix Alt Text 1.9.1 β
Stable tag: 1.9.1inreadme.txt,define( 'FIXALTTEXT_VERSION', '1.9.1' );infix-alt-text.php. This is the current wordpress.org release as of 2026-08-19 (last updated 2025-05-05), so the behaviour is present in the shipping version.Verified against WordPress 7.0.4, PHP 8.x, with and without a persistent object cache drop-in (Redis Object Cache 2.8.0).
WHERE IT HAPPENS
Type: object cache state leak / unbalanced global flag (performance regression).
inc/Scan.phplines 36-39, 56-59, 62-65, 79-82, 85-88 β hook registration, all priority 999inc/Scan.phpline 193 (save_post_scan), line 111 (save_term_scan), line 138 (delete_term_scan)library/_helpers-library/inc/Run_Library.phplines 342-371 (prevent_caching()), specifically line 367
ROOT CAUSE
inc/Scan.php,init()registers five hooks, all at priority 999:// Save Post
add_action( 'save_post', [<br>self::class,<br>'save_post_scan',<br>], 999, 1 );</p><p>Each of those handlers begins by suspending cache addition. <code>inc/Scan.php:191-197</code>:</p>
Run::prevent_caching(true); $this_post = get_post( $post_id );The same first line appears in
save_term_scan()(line 111) anddelete_term_scan()(line 138).library/_helpers-library/inc/Run_Library.php:342-371:`<br>public final static function prevent_caching( bool $wp_cache = false ): void {</p></p> <pre class=”wp-block-code”><code></code></pre> <p><p><p>There is no matching <code>wp_suspend_cache_addition( false )</code> anywhere in the plugin:</p></p></p> <p><p><p><code>$ grep -rn “wp_suspend_cache_addition” /path/to/wp-content/plugins/fix-alt-text/ library/_helpers-library/inc/Run_Library.php:367: wp_suspend_cache_addition( true );</code></p></p></p> <p><p><p>Two properties make this bite harder than it looks.</p></p></p> <pre class=”wp-block-code”><code>if ( is_bool( $suspend ) ) { $_suspend = $suspend; } return $_suspend;</code></pre> <p><p><br></p></p> <p><p><p><code>public function add( $key, $data, $group = ‘default’, $expire = 0 ) { if ( wp_suspend_cache_addition() ) { return false; }</code></p></p></p> <p><p><p>and <code>add_multiple()</code> (line 235) is a loop over <code>add()</code>. Persistent drop-ins honour the same contract β Redis Object Cache 2.8.0, <code>includes/object-cache.php:1167</code>:</p></p></p> <p><p><p><code>public function add_multiple( array $data, $group = ‘default’, $expire = 0 ) { if ( function_exists( ‘wp_suspend_cache_addition’ ) && wp_suspend_cache_addition() ) { return array_combine( array_keys( $data ), array_fill( 0, count( $data ), false ) ); }</code></p></p></p> <p><p><p>Core’s cache priming paths use exactly those functions:</p></p></p> <ul> <li></li> <li></li> <li></li> <li></li> </ul> <p><p><p><code>wp_cache_set()</code> is not gated, so ordinary writes still succeed and the cache appears to be working. What stops working is priming: <code>update_meta_cache()</code> runs its SELECT, builds <code>$data</code>, hands it to <code>wp_cache_add_multiple()</code>, and the result is discarded. Every later <code>get_post_meta()</code> on those objects finds no cache entry and primes again, running the same query repeatedly within one request.</p></p></p> <p><p><p>PROOF OF CONCEPT</p></p></p> <p><p><p>Prerequisites: WordPress 6.x or 7.x (tested on 7.0.4), Fix Alt Text 1.9.1 active, WP-CLI. No configuration required β the effect occurs before any of the plugin’s own post-type/taxonomy filtering runs.</p></p></p> <p><p><p>Run a single <code>wp_insert_post()</code> and read the flag afterward:</p></p></p> <p><p><p><code>wp eval ‘var_dump( wp_suspend_cache_addition() ); wp_insert_post([“post_type”=>”page”,”post_title”=>”cache-flag-test”,”post_status”=>”draft”]); var_dump( wp_suspend_cache_addition() ); var_dump( defined(“FIXALTTEXT_HELPERSLIBRARY_DONOTCACHE_WP”) );'</code></p></p></p> <p><p><p>Expected result β the scan handler may suspend cache addition while it works, but the flag should be back to its prior value once the handler returns, so the rest of the request caches normally:</p></p></p> <p><p><p><code>bool(false) bool(false)</code></p></p></p> <p><p><p>Actual result with Fix Alt Text 1.9.1 active:</p></p></p> <p><p><p><code>bool(false) bool(true) bool(true)</code></p></p></p> <p><p><p>Cache addition remains suspended, and the guard constant is set so it will never be re-evaluated for the remainder of the request.</p></p></p> <p><p><p>Note that <code>save_post_scan()</code> calls <code>Run::prevent_caching( true )</code> before its post-type checks, so this reproduces even for post types the plugin is not configured to scan, and even when the handler immediately returns.</p></p></p> <p><p><p>To see the cost rather than just the flag, on any site with a meaningful amount of post meta:</p></p></p> <ol> <li></li> <li></li> <li></li> <li></li> </ol> <p><p><p>IMPACT</p></p></p> <p><p><p>Measured on a production-scale site (23 nav menus, 863 nav menu items), object cache flushed before each run:</p></p></p> <ul> <li><code>post.php?post=N&action=edit</code>, no <code>save_post</code> β 664 queries, 83.4% object cache hit rate, 5.5 s render</li> <li><code>post-new.php?post_type=page</code>, <code>save_post</code> fires β 24,968 queries, 25.2% object cache hit rate, 88-92 s render</li> </ul> <p><p><p>To be precise about attribution: the 24,968 queries are not generated by Fix Alt Text. That volume comes from an unrelated N+1 in another plugin’s meta box. What the cache suspension does is remove the mechanism that would otherwise absorb it β with cache addition intact, the same screen renders in 5-7 s. The plugin does not create the queries; it prevents them from ever being cached, so their cost scales linearly with the number of lookups instead of being paid once.</p></p></p> <p><p><p>The effect is not limited to that one screen. Any request that saves a post and then keeps doing work runs degraded from that point forward:</p></p></p> <ul> <li><code>wp-admin/post-new.php</code> for every post type, on every load</li> <li>Bulk edits (<code>edit.php</code> bulk actions), which save many posts in one request</li> <li>Importers such as WP All Import, which loop <code>wp_insert_post()</code> hundreds or thousands of times in a single request β the first save disables priming for all the rest</li> <li>Cron runs and any programmatic <code>wp_insert_post()</code> / <code>wp_update_post()</code> loop</li> <li>Term-heavy operations, via <code>saved_term</code> and <code>delete_term</code></li> </ul> <p><p><p>No user interaction beyond a normal save is required, and no specific role or configuration is needed. The suspension happens unconditionally at the top of the handler for any authenticated user or process capable of saving a post, attachment or term.</p></p></p> <p><p><p>There is no security consequence here: no data is exposed, modified or lost. The impact is CPU and database load, and on large sites, request timeouts.</p></p></p> <p><p><p>SUGGESTED FIX</p></p></p> <p><p><p>Suspending cache addition during the scan is defensible; leaving it suspended is not. The flag should be restored when the scan finishes.</p></p></p> <p><p><p>Because another plugin or core routine may legitimately have suspended cache addition already, the previous value should be captured and restored rather than unconditionally set to false. <code>wp_suspend_cache_addition()</code> returns the current value when called with no argument, which makes this straightforward.</p></p></p> <p><p><p>In <code>library/_helpers-library/inc/Run_Library.php</code>, add a companion method and record the prior state:</p></p></p> <p>`
/**
* Stores the object cache addition state that was in effect before we suspended it.
*
* @var bool|null
*/
private static $previous_cache_addition = null;public final static function prevent_caching( bool $wp_cache = false ): void { // ... existing DONOTCACHE* handling unchanged ... if ( $wp_cache && ! defined( 'FIXALTTEXT_HELPERSLIBRARY_DONOTCACHE_WP' ) ) { // Flag to prevent this from running twice define( 'FIXALTTEXT_HELPERSLIBRARY_DONOTCACHE_WP', true ); // Remember what the caller had set, so we can hand it back untouched. self::$previous_cache_addition = wp_suspend_cache_addition(); // Prevent WP Caching objects in the global variables wp_suspend_cache_addition( true ); } } /** * Restores object cache addition to whatever it was before prevent_caching() ran. */ public final static function restore_caching(): void { if ( null !== self::$previous_cache_addition ) { wp_suspend_cache_addition( self::$previous_cache_addition ); self::$previous_cache_addition = null; } }Then call
Run::restore_caching()at the end of each handler ininc/Scan.phpβsave_post_scan(),save_term_scan(),delete_term_scan()β including on the early-return paths. Atry { ... } finally { Run::restore_caching(); }around the body is the tidiest way to cover every exit.Note that the
FIXALTTEXT_HELPERSLIBRARY_DONOTCACHE_WPconstant would then need to stop doubling as the re-entry guard, since a second save in the same request should be able to suspend again. Replacing it with a static boolean property, or checkingnull === self::$previous_cache_addition, handles that.Two lighter alternatives if a wider refactor is unwelcome:
- Shutdown hook: register
add_action( 'shutdown', [ Run::class, 'restore_caching' ] )when suspending. This bounds the suspension to the request but does not help long-running importers or cron, which do most of their work before shutdown. Better than nothing, but not the fix. - Restore immediately after the scan call: in
save_post_scan(), move the suspension so it wraps onlyself::scan_post( $this_post ), and restore right after. This is the smallest change and matches the apparent intent β avoid caching the objects the scanner touches β without leaking state.
The
Scan_Process_Library.php:421call inhandle()is a different case: a background scan process generally owns its whole request, so an unrestored flag there is far less harmful. Restoring it consistently would still be an improvement.One unrelated note while in that function: the
WP_REDIS_DISABLEDdefine is inert. The Redis Object Cache drop-in reads that constant at load time inwp-content/object-cache.php, duringwp-settings.php, long beforesave_postfires, so defining it inside asave_posthandler cannot disable a drop-in that has already loaded. It is harmless, just ineffective β worth removing or documenting so it is not mistaken for the cause of the above.REFERENCES
Plugin source (wordpress.org SVN, tag 1.9.1):
inc/Scan.phpβ https://plugins.trac.wordpress.org/browser/fix-alt-text/tags/1.9.1/inc/Scan.phplibrary/_helpers-library/inc/Run_Library.phpβ https://plugins.trac.wordpress.org/browser/fix-alt-text/tags/1.9.1/library/_helpers-library/inc/Run_Library.php
WordPress core and documentation:
wp_suspend_cache_addition()β https://developer.wordpress.org/reference/functions/wp_suspend_cache_addition/wp_cache_add()β https://developer.wordpress.org/reference/functions/wp_cache_add/update_meta_cache()β https://developer.wordpress.org/reference/functions/update_meta_cache/_prime_post_caches()β https://developer.wordpress.org/reference/functions/_prime_post_caches/WP_Object_Cache::add()β https://developer.wordpress.org/reference/classes/wp_object_cache/add/
You must be logged in to reply to this topic.