Skip to content

Commit

Permalink
Prevent infinite loops of migrations (see #6957)
Browse files Browse the repository at this point in the history
Description
-----------

Fixes #3803, #4254, ...

Something bad happened to me recently. I had a mailcious migration that did run forever. Because the migration was triggered by CI/CD, I had no control in stopping it... 🙀

IMHO, there is no legitamte case (esp in prodution) where migrations need to run more than (lets say a random number) eight times. Therefore, this PR adds a maximum loop count. This PR fixes a range of potential errors, incl. concrete errors (see issue numbers above). 

(We might also consider to break the loop and return a command failure code).

Commits
-------

6cc2c91 Add loop control for migrations
c06528c Introduce local variable
5f3e30e Stop with error
8698970 Also write NDJSON
07fb02e Increase maximum iterations
ad0dd71 Update MigrateCommand.php
e453142 Update MigrateCommand.php
a6fd497 Adjust the error message

Co-authored-by: leofeyer <1192057+leofeyer@users.noreply.github.com>
  • Loading branch information
richardhj and leofeyer committed Mar 20, 2024
1 parent 6877c40 commit 44dd01c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core-bundle/src/Command/MigrateCommand.php
Expand Up @@ -250,6 +250,8 @@ private function hasWorkToDo(): bool

private function executeMigrations(bool &$dryRun, bool $asJson, string $specifiedHash = null): bool
{
$loopControl = 19;

while (true) {
$first = true;
$migrationLabels = [];
Expand Down Expand Up @@ -362,6 +364,19 @@ private function executeMigrations(bool &$dryRun, bool $asJson, string $specifie
// Do not run the update recursive if a hash was specified
break;
}

if ($loopControl-- < 1) {
if ($asJson) {
$this->writeNdjson('error', [
'message' => 'The migrations were stopped after 19 iterations as a precaution. There is a high chance of an infinite loop of migrations.',
'isSuccessful' => false,
]);
} else {
$this->io->error('The migrations were stopped after 19 iterations as a precaution. There is a high chance of an infinite loop of migrations. If this is not the case, please re-run the command. To troubleshoot this error, check the shouldRun() method of the migration(s) listed above.');
}

return false;
}
}

return true;
Expand Down

0 comments on commit 44dd01c

Please sign in to comment.