Review main Merged

88556[BUGFIX] Parallel execution of non-parallel scheduler task

Adam Koppe CR +2 V +2 Normal +113 −82 updated 04/09/25, 11:12

Commit message

Close a Time-of-Check Time-of-Use style time window in
Scheduler::executeTask() by checking the actual task execution id value
after the allocation instead of checking for any task execution id
allocations long before the actual execution id allocation happens. The
time window allowed multiple scheduler invocations to pick up and
execute the same non-parallel scheduler task at the same time.

Make SchedulerTaskRepository::addExecutionToTask() and
SchedulerTaskRepository::removeExecutionOfTask() atomic by modifying
(UPDATE) the value of the "serialized_executions" field only if it's
still set to the update base value previously SELECT-ed. That allows
parallel invocations of SchedulerTaskRepository::addExecutionToTask() to
no longer obtain the same execution id and fixes a bug in
SchedulerTaskRepository::removeExecutionOfTask() that reverted
removals of execution ids.

Adjust SchedulerTaskRepository::addExecutionToTask() to no longer
hallucinate a non-existing execution id of 0 in situations were the
persistence record associated with the task cannot be SELECT-ed from the
database.

Change SchedulerTaskRepository::removeExecutionOfTask() to no longer
override the previous value of the tasks record "lastexecution_failure"
field if the given `$failureReason` method argument is `null`. This is
due to the change described in the first paragraph, where we need to
remove the execution of a task when it wasn't executed. The previous as
well as the updated implementation of Scheduler::executeTask() always
pass in a value of type `string` if the task execution happened.

A full reproduction is available online on GitHub:
https://github.com/adamkoppede/typo3-parallel-scheduler-task-processing-test

Review

  • Code review CR +2 Done
    • Christian Kuhn +2
    • Stefan Bürk +1
  • Verified V +2 Done
    • Christian Kuhn +2
    • Stefan Bürk +1
    • core-ci +1 CI

Issues 1

Files 2

Review log 2228

Show 23 earlier messages
  1. Adam Koppe uploaded patch set 1 03/10/25, 13:10

  2. Adam Koppe commented on patch set 1 03/10/25, 13:17

    The changes described in the third and fourth paragraphs may be too much for patch level change. Please let me know what you think!
  3. core-ci voted on patch set 1 V -1 03/10/25, 13:22 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/75416

  1. Adam Koppe uploaded patch set 2 V -1 03/10/25, 13:35

  2. core-ci voted on patch set 2 V -1 03/10/25, 13:47 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/75418

  3. Adam Koppe uploaded patch set 3 V -1 03/10/25, 16:36

  4. core-ci voted on patch set 3 V +1 03/10/25, 16:42 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/75430

  5. Benni Mack updated the commit message in patch set 4 V +1 03/26/25, 09:34

  6. Benni Mack rebased onto patch set 5 03/26/25, 09:34

  7. core-ci voted on patch set 4 V +1 03/26/25, 09:43 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/76363

  8. core-ci voted on patch set 5 V +1 03/26/25, 09:45 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/76364

  9. Andreas Kienast commented on patch set 5 CR -1 03/26/25, 10:15

    SchedulerTaskRepository.php:462

    Why not a simple `while`?

    SchedulerTaskRepository.php:503

    `assert()` must be avoided in production mode as it may get disabled in php.ini via `zend.assertions = -1`.
  10. Adam Koppe commented on patch set 5 03/26/25, 10:52

    Thank you for your review @akienast@scripting-base.de. I'm not sure about the exact details of the adjustments you want to see. I'm waiting for your response before implementing them.

    SchedulerTaskRepository.php:462 Reply

    Do you mean `while (true) { ... }` or `while (($row = $this->findRecordByUid($task->getTaskUid()) !== null) { ... }`? For the first one I don't know the different to the `for (;;) { ... }` syntax so there was no reasoning for either one from my side. I didn't use the complex while variant with the exception thrown below the loop so readers interested in what happens when no row could be SELECT-ed don't need to read most of the function body.

    SchedulerTaskRepository.php:503 Reply

    The `assert()` here primarily serves as a hint for code readers, so I don't think it is an issue if system administrators disable it. A properly behaving database will never return anything but 0 or 1 because we use a concrete value for the `uid` primary key field in the WHERE clause. What do you suggest instead? Throwing a \RuntimeException?
  11. Benni Mack uploaded patch set 6 CR -1 V +1 03/26/25, 11:00

  12. core-ci voted on patch set 6 V +1 03/26/25, 11:09 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/76367

  13. Stefan Bürk commented on patch set 6 03/27/25, 22:11

    SchedulerTaskRepository.php:462 Reply

    Benni changed it already. Not sure, if we really need a loop here with "updated = 1" as only abort condition, doing data retrival and blindly updating over and over again. Not tested, but maybe using the locking framework may be a usefull way to work with block/waiting lock aquiring instead if a loop may be a better (readable) solution here. (Just thinking).
  14. Adam Koppe commented on patch set 6 03/28/25, 09:48

    SchedulerTaskRepository.php:462 Reply

    The use of an application level lock (Core\Locking) around the SELECT and UPDATE statements would hurt the most common case. Which is that the first UPDATE statement modifies exactly one row and the loop exits within the first iteration. Getting into the second iteration of the loop is very rare. I wasn't able to get it reliably, even with my test setup https://github.com/adamkoppede/typo3-parallel-scheduler-task-processing-test.
  15. Adam Koppe commented on patch set 6 03/30/25, 08:07

    Reply

    Done

    SchedulerTaskRepository.php:503 Reply

    Thread marked as resolved, as @benni@typo3.org removed both usages of `assert()`.
  16. Christian Kuhn commented on patch set 6 CR -1 04/09/25, 10:17

    very clean change and well described. temp -1, reviewing in details now.
  17. Christian Kuhn commented on patch set 6 04/09/25, 10:37

    SchedulerTaskRepository.php:497

    this is pretty clever: adding serialized_executions to 'where' makes this de facto atomic, and the next loop run will try again if the first try failed. (just commenting this here in case anyone stumbles on this in the future again).
  18. Christian Kuhn commented on patch set 6 04/09/25, 10:38

    SchedulerTaskRepository.php:462 Reply

    i think the solution is clever and we can go with this.
  19. Christian Kuhn commented on patch set 6 04/09/25, 10:39

    SchedulerTaskRepository.php:550

    this is the same approach as in addExecutionToTask()
  20. Christian Kuhn commented on patch set 6 04/09/25, 10:39

    Reply

    i think it's fine. thank you!
  21. Christian Kuhn commented on patch set 6 04/09/25, 10:43

    SchedulerTaskRepository.php:466

    this exception is quite important too: in case a task has been removed in the first loop, the while would exit here if it can't find the row anymore.
  22. Christian Kuhn commented on patch set 6 CR +1 V +1 04/09/25, 10:50

    clean and clever patch! many thanks!

    Scheduler.php:196

    this is slightly convoluted but a working solution. i personally would probably not have worked with finally here and would have duplicated the finally code, but that's matter of taste. let's keep it like this!
  23. Stefan Bürk voted on patch set 6 CR +1 V +1 04/09/25, 10:51

  24. Christian Kuhn voted on patch set 6 CR +2 V +2 04/09/25, 10:51

  25. Christian Kuhn merged patch set 7 04/09/25, 11:12