Skip to content

Bugfix: Route advisory-lock queries to the master - #185

Open
ErikBrendel wants to merge 1 commit into
Automattic:trunkfrom
ErikBrendel:fix/route-advisory-lock-queries-to-master
Open

ErikBrendel wants to merge 1 commit into
Automattic:trunkfrom
ErikBrendel:fix/route-advisory-lock-queries-to-master

Conversation

@ErikBrendel

Copy link
Copy Markdown

MySQL's advisory lock functions (GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS, IS_FREE_LOCK, IS_USED_LOCK) are currently issued as SELECTs, so is_write_query() treats them as reads and db_connect() routes them to a replica. Because these locks are connection scoped, this defeats their purpose: the lock is held on a different connection than the master where the guarded writes happen, and replica load balancing can place concurrent callers on different servers, so they never contend: the named lock provides no mutual exclusion at all.

Add is_lock_query() and force such queries to the master. It is kept separate from is_write_query() on purpose: a lock alters no data, and the is_write_query path also sets srtm[$table]=true. For a tableless lock query $table is '', and srtm[''] would make the later stripos($query, '') match every subsequent read and wrongly pin them all to the master.

MySQL's advisory lock functions (GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS,
IS_FREE_LOCK, IS_USED_LOCK) are issued as SELECTs, so is_write_query() treats
them as reads and db_connect() routes them to a replica. Because these locks
are connection scoped, this defeats their purpose: the lock is held on a
different connection than the master where the guarded writes happen, and
replica load balancing can place concurrent callers on different servers, so
they never contend — the named lock provides no mutual exclusion at all.

Add is_lock_query() and force such queries to the master. It is kept separate
from is_write_query() on purpose: a lock alters no data, and the is_write_query
path also sets srtm[$table]=true — for a tableless lock query $table is '',
and srtm[''] would make the later stripos($query, '') match every subsequent
read and wrongly pin them all to the master.
@ErikBrendel ErikBrendel changed the title Route advisory-lock queries to the master Bugfix: Route advisory-lock queries to the master Jun 3, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jun 3, 2026

Copy link
Copy Markdown

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.

1 participant