Skip to content

Options, Meta APIs: Avoid caching options as nonexistent after database errors. - #12595

Open
gunjanjaswal wants to merge 2 commits into
WordPress:trunkfrom
gunjanjaswal:fix/61762-notoptions-cache-db-errors
Open

gunjanjaswal wants to merge 2 commits into
WordPress:trunkfrom
gunjanjaswal:fix/61762-notoptions-cache-db-errors

Conversation

@gunjanjaswal

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/61762

$wpdb->get_row() returns null both when an option row is missing and when the query itself failed. get_option() and get_network_option() treated the second case as the first and primed the notoptions cache, so a transient database error could hide an option that is still in the database. With a persistent object cache that state sticks around, and the option keeps returning its default long after the database has recovered.

delete_option() had the same problem from the other direction: it primed notoptions before checking the result of $wpdb->delete(), so a failed delete marked a still-present option as nonexistent.

Each notoptions write is now gated on the query being known to have succeeded, using $wpdb->last_error on the read paths and false !== $result on the delete paths. delete_network_option() was guarded on a truthy result, which meant a successful delete affecting zero rows never primed the cache. Zero rows is a successful query, so it primes there now too.

Adds regression tests covering the read and delete paths, including the zero-row case.

Clearing notoptions in add_network_option() and update_network_option() is left for a follow-up, per the ticket.

…se errors.

A failed `SELECT` in `get_option()` and `get_network_option()` returns null,
which is indistinguishable from a missing row. Both functions treated that as
proof of non-existence and primed the `notoptions` cache, hiding an option that
is still in the database until the cache is invalidated.

`delete_option()` had the same problem from the other direction: it primed
`notoptions` before checking the result of `$wpdb->delete()`, so a failed delete
marked a still-present option as nonexistent.

The `notoptions` writes are now gated on the query being known to have
succeeded. `delete_network_option()` is adjusted to match: it was guarded on a
truthy result, so a successful delete affecting zero rows never primed the
cache. Zero rows is a successful query, so it still primes; only false does not.
@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props gunjanjaswal, shanerounce.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@gunjanjaswal

Copy link
Copy Markdown
Author

Motivation from the field: Trac #61762 just picked up a report of this hitting a multisite in production. The initial DB error poisoned notoptions for a network option, so every later admin load re-attempted the write, and add_network_option()'s raw INSERT piled up ~1.2M duplicate rows before it was caught (https://core.trac.wordpress.org/ticket/61762#comment:3, plugin report at itmaybejj/editoria11y#80).

This PR fixes that at the root: gating the notoptions write on the query having actually succeeded means a transient error no longer marks a live option as nonexistent, so the recover-never re-write loop never starts. The raw-INSERT idempotency raised on the ticket is worth hardening separately as a follow-up.

@gunjanjaswal

Copy link
Copy Markdown
Author

Gentle bump — this has been open a couple of months without a review. It's a focused fix so the options and metadata caches don't persist a "nonexistent" marker after a transient database error, which otherwise masks the real value until the cache is cleared. Happy to rebase or adjust anything if that makes it easier to pick up.

@gunjanjaswal

Copy link
Copy Markdown
Author

Circling back on this one. Happy to rebase it to refresh CI whenever that's useful — the lone red check is a stale July matrix job showing core's own PHP 8.2 deprecation warnings, not anything from this change. The fix itself is unchanged and focused: don't cache an option as "nonexistent" when the read actually failed on a database error, which otherwise sticks a false negative in the cache until it's flushed. There's since been a field report on Trac #61762 of this biting a multisite, so a first review whenever someone has a window would be much appreciated.

Adds regression tests on top of the existing coverage for the notoptions fix. Each one simulates the next request of a persistent object cache by flushing the cache and restoring only the persisted `notoptions` entry, so a false "missing" marker written after a failed query would hide an option that still exists. Also checks that a failed network option read on one network does not touch another network's `{network_id}:notoptions` entry in the `site-options` group.

Follow-up to the work on #61762.

See https://core.trac.wordpress.org/ticket/61762.
@gunjanjaswal

Copy link
Copy Markdown
Author

Added a round of regression tests from @AceMedia (props AceMedia), merged in from gunjanjaswal#1. They cover the angle the existing tests didn't: the next request. The current tests check that notoptions isn't written after a failed query; these simulate a persistent object cache (flush, restore only what would have persisted) and assert a poisoned notoptions entry can't hide an option that actually exists, on the next request. There's also a per-network isolation case for get_network_option().

No production code changed — tests only, built on this PR's head.

Ran the full option suite locally (PHP 8.3, MySQL 8.0): tests/option/option.php 90 tests / 140 assertions and tests/option/networkOption.php 37 tests / 58 assertions, all green, including the new cases.

@gunjanjaswal

Copy link
Copy Markdown
Author

A nudge on this one — it's been open a while without a review. It avoids caching a stale value in the options/meta APIs for the case described in the ticket. The single failing cell is the known PHP 8.0 / MariaDB multisite flake, not related to this patch. Happy to take another pass if anything's needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants