Skip to content

Bayes: sort tokens in MySQL store to prevent InnoDB deadlocks - #40

Closed
fkoyer wants to merge 1 commit into
apache:trunkfrom
fkoyer:fix-bayes-mysql-deadlock
Closed

Bayes: sort tokens in MySQL store to prevent InnoDB deadlocks#40
fkoyer wants to merge 1 commit into
apache:trunkfrom
fkoyer:fix-bayes-mysql-deadlock

Conversation

@fkoyer

@fkoyer fkoyer commented Aug 26, 2026

Copy link
Copy Markdown

bayes_token is InnoDB with PRIMARY KEY (id, token), so every token write takes a row lock on the clustered index. _put_tokens and tok_touch_all iterated tokens in Perl hash / caller order, which is randomized per process, so two concurrent scans that share tokens could lock the same rows in opposite orders and deadlock resulting in the following
error:

bayes: _put_tokens: SQL error: Deadlock found when trying to get lock; try restarting transaction

Sorting the tokens gives every writer a single global lock order, so the circular wait cannot form. The token column is binary(5), so Perl's default string sort matches InnoDB's byte ordering on the clustered index.

Also, replace dbg with warn so future MySQL errors are visible in the logs.

bayes_token is InnoDB with PRIMARY KEY (id, token), so every token
write takes a row lock on the clustered index. _put_tokens and
tok_touch_all iterated tokens in Perl hash / caller order, which is
randomized per process, so two concurrent scans that share tokens
could lock the same rows in opposite orders and deadlock.

Sorting the tokens gives every writer a single global lock order, so
the circular wait cannot form. The token column is binary(5), so
Perl's default string sort matches InnoDB's byte ordering on the
clustered index.

Also, replace `dbg` with `warn` so future MySQL errors are visible in
the logs.
@fkoyer

fkoyer commented Aug 26, 2026

Copy link
Copy Markdown
Author

I believe this fixes bug 7363

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