Skip to content

Lock healed records in primary key order - #93

Merged
brendon merged 1 commit into
mainfrom
fix/healer-lock-order
Nov 28, 2026
Merged

brendon merged 1 commit into
mainfrom
fix/healer-lock-order

Conversation

@brendon

@brendon brendon commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

When a scope has no belongs_to association, or its parent is nil, the healer locks the scope's records with plain lock.ids before renumbering them. #75 found that on MySQL, plain lock.ids deadlocks more often than the same lock ordered by primary key, and changed positioning's own lock to reorder(primary_key).lock.ids. This PR makes the same change to both of the healer's lock.ids calls.

Measurements

MySQL 9.7.4, locally. Each run is five rounds of three concurrent moves to position 1 plus one heal_position_column!, on 10 records in one list. Deadlocks are moves/heals, for each of three runs:

List main This PR #75 #75 and this PR
No scope (Product) 4/1, 0/1, 1/0 0/0, 0/0, 0/0 0/2, 0/1, 3/1 0/0, 0/0, 0/0
Plain column scope, only a [scope, position] unique index 2/4, 4/4, 2/5 2/1, 2/1, 2/1 2/5, 2/5, 2/5 2/1, 2/1, 2/1

Positions were correct after every round. This PR doesn't change the moves' own lock; their deadlocks in the column scope are the ones #75 also measured on main. The probe isn't part of this PR.

Who this changes things for

Only apps that heal a list with no parent row to lock: a model with no scope, a scope on plain columns, or a list whose belongs_to parent is nil. The same rows are locked, now in primary key order. Healing a list that has a parent still locks the parent with lock!.

Tests

test_heal_locks_records_in_primary_key_order checks that the healer's lock is ordered by primary key for a model with no scope (Product), a column scope (Blog) and a nil parent (Category). It's skipped on SQLite, which has no row locks, and fails against main's healer on MySQL and PostgreSQL.

Changelog: When healing a scope that has no belongs_to association (or whose parent is nil), lock its records in primary key order. On MySQL, healing next to saves to the same list deadlocked more often without it.

🤖 Generated with Claude Code

When a scope has no belongs_to association, or its parent is nil, the
healer locks the scope's records with plain lock.ids. On MySQL, that
deadlocks more often against saves to the same list than ordering the
lock by primary key, which #75 does for positioning's own lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brendon
brendon force-pushed the fix/healer-lock-order branch from 0069249 to 6ef8590 Compare November 28, 2026 01:33
@brendon
brendon merged commit 479d9ce into main Nov 28, 2026
27 checks passed
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