You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migration refresh during long-running migrate request can re-trigger same migration and deadlock #4763
Observed in a local log: a login-page migration run completed successfully through 3.4.3, but the user saw a connection error on screen with no corresponding server-side log entry. Refreshing the page re-triggered the migration process, which then deadlocked.
Timeline from log
11:42:34 - migration run begins
11:45:02 - "Successfully migrated to 3.4.3" logged (3.4.3_mysql84_utf8mb4_conversion.sql)
User sees a connection error client-side (nothing logged server-side for this)
11:45:29 - migration run starts again, re-running the same 3.4.3 script
11:45:37 - Deadlock found when trying to get lock; try restarting transaction on ALTER TABLE ospos_attribute_values CONVERT TO CHARACTER SET utf8mb4 ...
11:45:54 - migration marked failed
Root cause (suspected)
App\Controllers\Login::index() / Login::migrate() call MY_Migration::getCurrentVersion(), which determines "current version" from the max version row in the CI4 migrations history table (app/Libraries/MY_Migration.php). That history row is only written by the framework after a migration's up() returns successfully.
If the client disconnects (e.g. a page refresh) while a long-running migration request is still executing server-side — but before CI4 finishes recording the history row for the just-applied migration — the DB schema has already been altered, but the tracker still reports the prior version as current. A subsequent request then considers the database "not latest" and re-runs the same migration script, which re-applies ALTER TABLE ... CONVERT TO CHARACTER SET utf8mb4 against a table that's already been converted, racing against leftover connections/locks from the first (abandoned) request. This produces the deadlock.
Neither Login::index() nor Login::migrate() currently call ignore_user_abort(true), so a client disconnect can tear down the PHP request mid-migration depending on server configuration (set_time_limit(3600) alone does not protect against this).
Suggested fix
Call ignore_user_abort(true) alongside the existing set_time_limit(3600) in both Login::index() and Login::migrate() so a client-side disconnect/refresh doesn't abort the PHP process mid-migration.
Consider guarding against concurrent/duplicate migration runs (e.g. an application-level lock) so a second request can't kick off the same migration while one is already in progress.
Thanks for the detailed write-up. Agreed on the root cause — the window between schema change and the history-row write is the race.
Direction: we want pessimistic locking to block concurrent migration runs entirely, not just mitigate the client-disconnect case. Concretely:
Acquire a DB-level lock (e.g. — non-blocking, 0 timeout) at the start of the migration sequence, before any individual migration runs.
If the lock can't be acquired, fail fast with a clear "migration already in progress" message rather than attempting a second run.
Release the lock () after the full sequence completes (or on error).
is still a good hardening on top, but the lock is the real guard — it also covers the case of two simultaneous requests, a supervisor retry, or a user double-clicking.
index() and migrate() both call set_time_limit(3600) before $migration->setNamespace('App')->latest(), but neither calls ignore_user_abort(true). A client disconnect/refresh during a long-running migration can still abort the PHP process mid-migration, leaving the schema altered but the migration history row unwritten — the race condition described.
There is no DB-level lock (e.g. GET_LOCK/RELEASE_LOCK) guarding the migration sequence in either method, so a concurrent/duplicate migration run is not prevented.
There is an open, unmerged PR addressing this: #4769 "fix(migrations): prevent concurrent runs with pessimistic DB lock". Maintainer discussion in the issue confirms the pessimistic-locking direction is the intended fix for 3.4.4, but it hasn't landed on master yet, so the deadlock scenario is still reproducible there.
Activity
jekkos commentedon Oct 22, 2026
Thanks for the detailed write-up. Agreed on the root cause — the window between schema change and the history-row write is the race.
Direction: we want pessimistic locking to block concurrent migration runs entirely, not just mitigate the client-disconnect case. Concretely:
This is the right fix for 3.4.4. Will pick it up.
jekkos commentedon Oct 22, 2026
To clarify the code that got mangled above:
objecttothis commentedon Oct 23, 2026
Claude Issue Checker Agent (cc @objecttothis)
Investigated against current
master.Status: Still present (fix not yet merged).
app/Controllers/Login.php:index()andmigrate()both callset_time_limit(3600)before$migration->setNamespace('App')->latest(), but neither callsignore_user_abort(true). A client disconnect/refresh during a long-running migration can still abort the PHP process mid-migration, leaving the schema altered but the migration history row unwritten — the race condition described.GET_LOCK/RELEASE_LOCK) guarding the migration sequence in either method, so a concurrent/duplicate migration run is not prevented.There is an open, unmerged PR addressing this: #4769 "fix(migrations): prevent concurrent runs with pessimistic DB lock". Maintainer discussion in the issue confirms the pessimistic-locking direction is the intended fix for 3.4.4, but it hasn't landed on
masteryet, so the deadlock scenario is still reproducible there.fix(migrations): prevent concurrent runs with pessimistic DB lock
fix(migrations): prevent concurrent runs with pessimistic DB lock
fix(migrations): prevent concurrent runs with pessimistic DB lock (op…