Skip to content

Rework tracking limits so they can't silently pause recommendations (on #93) - #95

Merged
jdevalk merged 4 commits into
codex/hardening-fixesfrom
filip/pr93-tracking-fixes
Nov 18, 2026
Merged

jdevalk merged 4 commits into
codex/hardening-fixesfrom
filip/pr93-tracking-fixes

Conversation

@ilicfilip

@ilicfilip ilicfilip commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #93, targeting its branch. Touches different code from #94, so both merge into #93 without conflicts. Checked with a local trial merge.

Problem in #93

A site-wide tracking_limited flag paused unused-option recommendations, and only a manual reset cleared it. Routine events set it:

  • a 1s GET_LOCK timeout, which every tracked request took at shutdown;
  • any request reading more than 1,000 distinct options;
  • any transient SQL error.

Also: legacy migrations over 10,000 names could never finish; pruning deleted the oldest rows, which are core options; and the existing-name check compared case- and accent-insensitive DB results with exact PHP keys.

Reproduced on #93's head: one 3-second slow writer during 20 requests lost 8 requests' observations, set the flag permanently and added about 1s latency. With this PR: 20/20 recorded, no flag, no stall.

What changes

  • No lock. The 10,000 cap is soft, checked without serialising requests. Concurrent requests near the cap can overshoot slightly; measured: +40 rows with 40 parallel requests.
  • The flag is set only when a new name is actually dropped for capacity, or when one request reads more names than storage holds. Write failures don't set it.
  • Existing names are found with a SQL join, so case and accent variants match the way the primary key does.
  • Legacy migration ignores the cap and always completes.
  • No pruning, and reads aren't truncated at the cap.
  • The notice and README now describe the remaining trigger.

Verification

  • tests/hardening.php, with the tracking section rewritten for the new rules, passes on a disposable WP 7.1.4 / MySQL 8.0.33 site, as do PHPCS, PHPStan and lint.
  • Mutation check: 8 deliberate regressions (exact PHP compare, flag on write failure, 1,000 request cap, capped migration, truncated reads, flag-when-merely-full, no cap, subquery-first join), and the suite caught every one.
  • Trying to break it found a real bug in my first version: with the subquery first in FROM, wpdb's charset check for non-ASCII queries ran SHOW FULL COLUMNS FROM SELECT. Every at-capacity write containing a non-ASCII name failed. Fixed in 1ea05ec, with a test.
  • Load over HTTP: 300 requests at 30 in parallel recorded a once-per-request option exactly 300 times (no lost writes or deadlocks), with no flag.
  • Browser (planner.test): the notice shows when the flag is set, Reset clears it, and recommendations return.
  • SQLite (sqlite-database-integration 3.0.1, WP_MySQL_On_SQLite driver, SQLite 3.54.1, WP 7.1.4): the full tests/hardening.php passes with no database errors printed.
    • On Harden option actions, tracking, and release workflows #93's head, every tracking write failed and set the flag, and reset couldn't clear the table. The driver returns '1=1' for GET_LOCK, which Harden option actions, tracking, and release workflows #93 compares with '1'. With this PR, writes succeed and no flag is set.
    • Over HTTP (PHP built-in server, 8 workers), 120 requests at 1 and at 8 in parallel recorded a once-per-request option exactly 120 times. No database is locked errors.
    • At the cap, 40 requests (8 in parallel): existing names counted 40/40, the table stopped at exactly 10,000 (SQLite serialises writers, so no overshoot), and the flag was set correctly.
    • Two test assumptions were MySQL-specific and are now engine-neutral. SQLite folds ASCII case but not accents, so sitéurl there is a different, unprotected option. Deleting it returns 404 and leaves siteurl untouched.
  • Note: on its own, this branch's test run hits the pagination test bug fixed in Fix protected options, sort direction and CI pinning (on #93) #94, on sites with more than 25 used-not-autoloaded options.

🤖 Generated with Claude Code

ilicfilip and others added 3 commits November 17, 2026 13:47
The tracking-limited flag paused unused-option recommendations site-wide and
only a manual reset cleared it, yet it was set by routine events: a 1s lock
timeout under concurrent traffic, any request reading over 1,000 options, or a
transient SQL error. Legacy migrations over 10,000 names could never finish,
and pruning deleted the oldest (core) rows.

- Drop the GET_LOCK serialisation; the 10,000-name cap is now a soft limit
  checked without a lock. No MySQL-only lock functions or DELETE ... LIMIT.
- Set the flag only when a new name is actually dropped for capacity, or one
  request reads more names than storage holds. Write failures don't set it.
- Detect existing names with a SQL join, so case/accent variants of a stored
  row count as existing, matching the primary key's collation.
- Migrate legacy data without the cap so migration always completes.
- Never prune rows, and don't truncate reads at the limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wpdb parses the first FROM to check the table charset for non-ASCII queries;
a leading subquery made every at-capacity write with a non-ASCII name fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Write failures no longer pause recommendations, so the notice now names the
only remaining cause: a full tracking table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test on Playground
Test this pull request on the Playground
or download the zip

SQLite's case folding is ASCII-only, so vary only the ASCII letters; the name
stays non-ASCII to keep exercising wpdb's charset check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jdevalk
jdevalk merged commit 17cf4ee into codex/hardening-fixes Nov 18, 2026
9 checks passed
@jdevalk
jdevalk deleted the filip/pr93-tracking-fixes branch November 18, 2026 13:09
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