Skip to content

Many cron processes can wait locks and consume resources  #25987

Closed
@ilnytskyi

Description

@ilnytskyi

Preconditions (*)

magento 2.3.x

  1. Cron observer uses locks (db, cache, file or zookeper)
  2. The job is locked after cron:run is sent to shell
    see this method
    \Magento\Cron\Observer\ProcessCronQueueObserver::execute

The problem, I suppose, that magetno uses the same method to run all groups or one group
in \Magento\Cron\Observer\ProcessCronQueueObserver::execute
it creates commands in shell

            if ($this->_request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1'
                && $this->getCronGroupConfigurationValue($groupId, 'use_separate_process') == 1
            ) {
                $this->_shell->execute(
                    $phpPath . ' %s cron:run --group=' . $groupId . ' --' . Cli::INPUT_KEY_BOOTSTRAP . '='
                    . self::STANDALONE_PROCESS_STARTED . '=1',
                    [
                        BP . '/bin/magento'
                    ]
                );
                continue;
            }

and only then, it runs the command like this
php bin/magento cron:run --group=index --bootstrap=standaloneProcessStarted=1
it goes to this part

            $this->lockGroup(
                $groupId,
                function ($groupId) use ($currentTime, $jobsRoot) {
                    $this->cleanupJobs($groupId, $currentTime);
                    $this->generateSchedules($groupId);
                    $this->processPendingJobs($groupId, $jobsRoot, $currentTime);
                }
            );

Technically possible to remove all locks and run many bin/magento cron:run & and see dozens of created processes in shell
e.g. indexer php bin/magento cron:run --group=index --bootstrap=standaloneProcessStarted=1

Steps to reproduce (*)

  1. Run bin/magento cron:run & a few times in background
  2. See that it creates more than 1 process in shell
    eg php bin/magento cron:run --group=index --bootstrap=standaloneProcessStarted=1

Expected result (*)

  1. If in shell one cron command is running the second should not be created
  2. only one unique shell command should be processed
  3. Lock for process or group should be checked before the command goes to shell

Actual result (*)

  1. Magento can create many processes at time
  2. May simultaneously running processes check locks wait and consumes server resources
  3. Lock for process or group is checked after the command is in shell
    Selection_248

GIF:
cron-lock

WORKAROUND:
Install each cron group separetely with flock instead adding default cron:run in crontab.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CronComponent: LockIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentPriority: P3May be fixed according to the position in the backlog.Progress: doneReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S3Affects non-critical data or functionality and does not force users to employ a workaround.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itstale issue

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions