Skip to content

fix: avoid entry author deadlocks - #19363

Open
mvanhorn wants to merge 1 commit into
craftcms:5.xfrom
mvanhorn:fix/15768-entry-author-deadlocks
Open

fix: avoid entry author deadlocks#19363
mvanhorn wants to merge 1 commit into
craftcms:5.xfrom
mvanhorn:fix/15768-entry-author-deadlocks

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

Change Entry::_saveAuthors() to use the existing Db::deleteIfExists() helper for entries_authors, which performs the delete only when matching rows exist and was designed to avoid deadlocks caused by deleting absent rows. Keep the existing author lookup and batch insert behavior intact so author replacement on established entries and initial author persistence continue to work across supported databases. Add a focused regression in EntriesTest that observes the database commands for an initial authored-entry save versus an author update: the initial save must not execute a delete for its absent author rows, while the update must still remove the existing rows and persist the replacement author.

Concurrent entry creation can intermittently deadlock on MySQL or MariaDB while Entry::_saveAuthors() writes to entries_authors. The thread includes reproductions from queue jobs, load-balanced installations, Craft Cloud, and a core-only control-panel flow, with InnoDB diagnostics showing distinct new entry IDs contending on the primary-index supremum. The save path currently issues an unconditional delete for an entry ID before inserting its authors, even when a newly-created entry cannot have author rows yet. That empty-range delete can take a gap lock that conflicts with another transaction following the same delete-then-insert sequence.

Fixes #15768

Related issues

Not applicable to this change.

@brandonkelly

Copy link
Copy Markdown
Member

Thanks for the PR!

You’ve got a couple PhpStan issues to address in testSaveAuthorsAvoidsDeletingMissingRows() though.

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.

[5.x]: Deadlock found when trying to get lock on entries_authors table

2 participants