Fix W3 Total Cache DbCache fatal in batch_insert#77
Open
ilicfilip wants to merge 1 commit into
Open
Conversation
W3TC's DbCache wrapper reads `$wpdb->col_info` after each cacheable query, which triggers `wpdb::load_col_info()` -> `mysqli_num_fields()`. For non-SELECT queries the result is `true` (bool), so this fatals with "Argument #1 ($result) must be of type mysqli_result, true given". W3TC bypasses caching for write queries via a regex that matches `START TRANSACTION`, `INSERT`, `COMMIT`, etc. — but not `BEGIN`. Switching `BEGIN` to `START TRANSACTION` (semantically identical in MySQL) lets W3TC's regex catch it and skip the col_info path. Reproduced locally with W3TC 2.9.4: identical stack trace with `BEGIN`, no fatal with `START TRANSACTION`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Test on Playground |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a fatal error reported by a user via WP.org support topic #4979 running this plugin alongside W3 Total Cache with Database Cache enabled:
The crash fires on shutdown when
Database::batch_insert()issuesBEGIN.Root cause
W3TC's DbCache layer wraps
$wpdband, after each cacheable query, reads$wpdb->col_infoto stash for the cache. That triggerswpdb::load_col_info()→mysqli_num_fields($this->result). For non-SELECT queries themysqli_queryresult is the booleantrue— hence the type error.W3TC has an explicit bypass that disables caching for transaction control and write statements via this regex (
DbCache_WpdbInjection_QueryCaching.php):Note the gap:
START TRANSACTIONandCOMMITare matched, butBEGIN(a MySQL alias forSTART TRANSACTION) is not. OurINSERTqueries and the closingCOMMITalready short-circuit caching, but the openingBEGINslips through — and that's the query that fatals.Fix
Use
START TRANSACTIONinstead ofBEGINinDatabase::batch_insert(). The two are semantically identical in MySQL, butSTART TRANSACTIONmatches W3TC's bypass regex, so thecol_infopath is never entered.Testing
Reproduced locally against a Valet site with W3TC 2.9.4 (DbCache enabled, file engine):
wp eval 'Database::batch_insert([...])'produces the identical stack trace to the user's report (same files, same line numbers, samemysqli_num_fields(true)fatal).batch_insert()completes cleanly; rows land inwp_option_optimizer_trackedas expected.Notes
🤖 Generated with Claude Code