Skip to content

1213 Deadlock found when trying to get lock #8933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
w130pmpo opened this issue Mar 17, 2017 · 113 comments · Fixed by #28007
Closed

1213 Deadlock found when trying to get lock #8933

w130pmpo opened this issue Mar 17, 2017 · 113 comments · Fixed by #28007
Labels
Component: Catalog Fixed in 2.3.x The issue has been fixed in 2.3 release line Fixed in 2.4.x The issue has been fixed in 2.4-develop branch help wanted Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@w130pmpo
Copy link

w130pmpo commented Mar 17, 2017

Preconditions

  1. Magento 2.1.3
  2. Php 7.0.13-1 and Apache2

Steps to reproduce

  1. Importing Product (Saving to Magento) from 3rd party API from CLI Script
  2. Run above process in parallel since too many products exist
  3. Index - Update by Schedule. (Meaning no index works when importing product)

Expected result

  1. Product Should be successfully saved to the system

Actual result

  1. Following problem happen oftenly,
  • PDOException: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock
  1. If I try to edit product related value on Admin page while importing product, that error happen too.
@southerncomputer
Copy link
Contributor

The indexer runs on a schedule by cron! you need to disable that or they need a lock to prevent concurrent access!

If you don't mind modifying the database code you can modify aoe's magenta 1 plugin to function the same. I have and it does not solve the longer issues, but can be a lifesaver due to the little short issues!

You should really batch all of your product imports when the store is in maintenance mode and then reindex before taking it out of maintenance mode at midnight!

At least until magento can develop a decent locking system to work around the cron indexer!
https://github.com/AOEpeople/Aoe_DbRetry/blob/master/app/code/community/Aoe/DbRetry/Resource/Db/Pdo/Mysql/Adapter.php

@w130pmpo
Copy link
Author

w130pmpo commented Mar 17, 2017

Thanks for reply.

  • I forgot to mention that we for now even disabled cron job too. So, no index work when importing product.

  • Let me tell you my scenario here
    -- There are lots of products on 3rd party system to import.
    -- Average 30k products exist on that system. On that system, 3k of products deleted and 3k of new products created every week.
    -- To manage a price and stock of existing product and newly added product, we need to run this process at least 1-2 per week.
    -- Importing Products to Magento system is slow. Especially creating new product is much slower than updating.
    -- Because of reason above (many products and slow), we have to run this process most time of week. So we cannot even turn our website in maintenance mode. That's why we need to run this script in parallel to get it done faster.

Thank you

@southerncomputer
Copy link
Contributor

Sounds like you need to use a dedicated import script. I can import 1M products in about 6 hours time using dedicated script that does direct access to database, but as you have figured out- you cannot manually muck with the categories/products during the same time! IMO the best route would be to offload the product attributes/categories to a nosql based solution so you can side-load the products. I've only seen this done with magento 1 using smile-sa (GitHub) mongodb driver!

But if you use smile-sa's elastic suite - the indexing goes much faster! check it out it is free!

While reindexing the catalog stays online since it reindexes to a new catalog and does a switcheroo when done!

@w130pmpo
Copy link
Author

Hello,

Could you please specify "nosql based solution" ?

Thank you

@southerncomputer
Copy link
Contributor

store all product attributes in mongodb which is far better than EAV-style datastore for tons of products. Better yet - imo - would be to store the attributes in elasticsearch itself!

@w130pmpo
Copy link
Author

Thanks for reply. I have another question regarding indexing.

  • Currently Indexer has been set as "UPDATE BY SCHEDULE"

  • Cron job currently disabled

  • Create Product using php script on CLI and through Product Model (Product->save())

=> expected result, newly added product should not be in catalog_product_flat table
=> actual result, newly added product is in catalog_product_flat_table (look system reindex after product save)

@southerncomputer
Copy link
Contributor

I'm not sure, are you sure that cron.php is not being called by crond?!

I assumed reindexing was to move from database (flat, or eav) to solr/elasticsearch models on schedule. I would have thought the flat table was directly written to.

I've been using elasticsearch since v1 - so i'm not sure how the flat tables get indexed if not real time?! mysql doesn't need any work to add text based indexing!

@ooples
Copy link

ooples commented Apr 21, 2017

I'm having this same issue on magento 2.1.6 and I'm using elasticsearch but my issue seems to be when magento is running a reindex

@magento-engcom-team magento-engcom-team added G1 Passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed G1 Passed labels Sep 5, 2017
@Nix-id
Copy link

Nix-id commented Oct 18, 2017

Confirm this bug.
v.2.1.8

@Kipperlenny
Copy link

and with v.2.21

@farhadfrz
Copy link

Confirm
Magento 2.1.10
In our case, when reindex is running, no order can be placed (lock on sales_order table)

@yj4189
Copy link

yj4189 commented Jan 3, 2018

Magento Version: 2.1.9|
Mode: Production
Indexing Mode: Update on save

Total Products: 18k
Total Customers: 54k
Total Orders: 18k

my client site is marketplace site so, on this site, there are lots of products, customers, and orders adding or updating.

But when I tried to reindexing all than after some time exception.log file generated including the following error in this file and server was stuck and then need to restart the server and then after the site is working as normal.

"PDOException: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction"

so please suggest me how can I fix it and what is the best way to reindexing manually or using cron?

@mslabko
Copy link
Member

mslabko commented Jan 19, 2018

Hi @yj4189 , if you set indexers to "Update on Save" mode, then you should not do full reindex.
I suggest you change indexing mode to "update on schedule" + setup crons.

If you still have deadlock, please provide information from mysql command SHOW ENGINE INNODB STATUS

@aliasifmse
Copy link

Same error happening for me on 2.2.2

@Top-CMS
Copy link

Top-CMS commented Apr 13, 2018

Same error happening for me on 2.2.3

@dimple-vasoya
Copy link

We are facing deadlock issue during catalog_product_attribute reindexing.

It gives SQLSTATE[HY000]: General error: 2006 MySQL server has gone away error as MySQL database CPU is 100% utilized.

Magento version: 2.1.7
No of SKUs: 6 Lacs
No of Attributes: 110
No of stores: 4

Please suggest what can be done?

@mslabko
Copy link
Member

mslabko commented May 18, 2018

@dimple-ambab , it can be another issue, not related to a deadlock. Please check after error happening status of section LATEST DETECTED DEADLOCK with command "SHOW ENGINE INNODB STATUS"
Also check all mysql error logs for reason of error

@MrDaar
Copy link

MrDaar commented May 18, 2018

I've experienced similar issues...

@jakoblundsted
Copy link

This is still an issue on 2.2.4, but only when trying to save products while the indexing is running.

@mcyrulik
Copy link

Same here - We receive that error if a product is saving when the index is running.

@StrategicMindInc
Copy link

We have the same error with 2.2.5 and PHP 7.1. Below is the cron log.
[Zend_Db_Statement_Exception]
SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction, query was: INSERT INTO cron_schedule (job_code, status, created_at, scheduled_at) VALUES (?, ?, '2018-07-23 12:16:02', '2018-07-23 12:19:00')

[PDOException]
SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

cron:run [--group GROUP] [--bootstrap BOOTSTRAP]

[Zend_Db_Adapter_Exception]
SQLSTATE[HY000] [2002] No such file or directory

[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

cron:run [--group GROUP] [--bootstrap BOOTSTRAP]

@StrategicMindInc
Copy link

In addition, the error message displayed in the browser is:
Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later

@ghost ghost self-assigned this Aug 31, 2018
@ghost ghost added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Aug 31, 2018
@magento-engcom-team
Copy link
Contributor

Hi @w130pmpo.

Thank you for your report and collaboration!

The issue was fixed by Magento team.

The fix will be available with the upcoming 2.3.5 release.

@magento-engcom-team
Copy link
Contributor

Hi @w130pmpo.

Thank you for your report and collaboration!

The issue was fixed by Magento team. The fix was delivered into magento/magento2:2.3-develop branch(es).
Related commit(s):

The fix will be available with the upcoming 2.3.5 release.

@fritzmg
Copy link

fritzmg commented Apr 25, 2020

@magento-engcom-team links to the commits do not seem to be working

@magento-engcom-team
Copy link
Contributor

Hi @w130pmpo.

Thank you for your report and collaboration!

The issue was fixed by Magento team.

The fix will be available with the upcoming 2.4.1 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Jun 5, 2020
@erikhansen
Copy link
Contributor

@magento-engcom-team Can you provide working links to the commits in 2.3.5 that fix this issue, as the commits you posted in your April 24th comment no longer work?

@sunnetmedia
Copy link

sunnetmedia commented Nov 18, 2020

I face a Similar issue on Magento 2.3.2 PHP 7.3 nginx, mariadb,

`root@ip-172-31-XXXXX:/var/www/html/XXXXXX# ### sudo php bin/magento cron:run

In Mysql.php line 589:

SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction, query was: DELETE FROM cron_schedule WHERE (status = 'pending') AND (job_code in
('consumers_runner')) AND (created_at < '2020-11-14 02:35:41')

In Mysql.php line 110:

SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction, query was: DELETE FROM cron_schedule WHERE (status = 'pending') AND (job_code in
('consumers_runner')) AND (created_at < '2020-11-14 02:35:41')

In Mysql.php line 91:
SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction
cron:run [--group GROUP] [--bootstrap BOOTSTRAP]

^C`

### Solution
Problem is I start my exportProcessor before manual cron:run. reboot and restart cron:run again its sucess

#sudo php bin/magento queue:consumers:start exportProcessor
#sudo reboot
&
sudo php bin/magento cron:run
Ran jobs by schedule.

fine

@avra911
Copy link

avra911 commented Apr 19, 2021

... the message keeps repeating The fix will be available with the upcoming 2.X.X release. over and over again since 2.1 up to 2.4.3. Can you guys change at least the words and use the proper probably?

This is particular interesting to address my concerns about the support.

Why this issue is closed when 2.4.3 was not released?

Where are the Adobe geniuses?

@mrtuvn
Copy link
Contributor

mrtuvn commented Apr 21, 2021

I'm not definitely geniues but this fix already confirm delivered to 2.4-develop branch via commit #28007 by @driskell.
https://github.com/magento/magento2/search?q=MC-25132&type=commits
It's will available in 2.4.3 release official in October 2021
So this ticket should closed.
If you can reproduce the problem on a Magento 2.4-develop installation, I'd strongly suggest you open a new issue with detailed steps about how to reproduce it (even though that's probably hard to get right).
In the mean time create composer patch if you need deliver it sooner for apply fixes

@DuySexy
Copy link

DuySexy commented Jun 16, 2021

temporary solution is truncate table cron_schedule , issue solved !

@groomershop-mt
Copy link

In our case the problem still occur after upgrading to Magento 2.4.3, so it's probably still not solved.

@Veltix
Copy link

Veltix commented Nov 19, 2021

Magento 2.4.3-p1 still this error exists...

@florianComp
Copy link

Magento 2.4.3 i have the same error when i import product with Php

@OvalMedia
Copy link

2.4.3-p1 same error. Happens with regular import/updates of ~4000+ products.

@hostep
Copy link
Contributor

hostep commented Sep 24, 2022

If you have an import that runs from within a PHP script, try to lock the indexer cronjobs before you start the import.
If those are locked, it means the import will run and block the indexers from running at the same time. When the import is finished, unlock the locks and the indexers will be able to run again.
Also, when the indexers are running while the import starts, doing this will prevent the import from starting up until the indexers stop running or when the lock timeout amount of seconds (300 seconds in the example below) expires and if that happens, throw an exception so it doesn't run.
The idea here is to prevent indexers and imports to run at the exact same time, which can lead to deadlocks.

Here's an example of a Helper class you can use for this, just call the executeAndLockIndexers with a callback method which will run your import code:

<?php

namespace Vendor\Import\Helper;

use Vendor\Import\Exception\LockException;
use Magento\Cron\Observer\ProcessCronQueueObserver;
use Magento\Framework\Lock\LockManagerInterfaceFactory;

class Lock
{
    private $lockManagerFactory;

    public function __construct(
        LockManagerInterfaceFactory $lockManagerFactory
    ) {
        $this->lockManagerFactory = $lockManagerFactory;
    }

    private function executeAndLockIndexers(callable $callback)
    {
        // try to get a lock of the index cronjob group & specific indexer cronjobs

        $lockManager = $this->lockManagerFactory->create();
        $lockTimeout = 300; // 5 minutes
        $cronIndexGroupId = 'index';

        $lockName1 = ProcessCronQueueObserver::LOCK_PREFIX . $cronIndexGroupId;
        $lockName2 = ProcessCronQueueObserver::LOCK_PREFIX . md5($cronIndexGroupId . '_' . 'indexer_reindex_all_invalid');
        $lockName3 = ProcessCronQueueObserver::LOCK_PREFIX . md5($cronIndexGroupId . '_' . 'indexer_update_all_views');

        // when we were able to create the lock, the cronjob will not start the indexer jobs (until the import is finished)
        // when the indexers are already running by the cronjob, we will wait the amount of seconds specified in the $lockTimeout variable before giving it up
        if ($lockManager->lock($lockName1, $lockTimeout)) {
            try {
                if ($lockManager->lock($lockName2, $lockTimeout)) {
                    try {
                        if ($lockManager->lock($lockName3, $lockTimeout)) {
                            try {
                                call_user_func_array($callback, []);
                            } finally {
                                $lockManager->unlock($lockName3);
                            }
                        } else {
                            throw new LockException("The $lockName3 was locked for more than $lockTimeout seconds, so giving up on this import");
                        }
                    } finally {
                        $lockManager->unlock($lockName2);
                    }
                } else {
                    throw new LockException("The $lockName2 was locked for more than $lockTimeout seconds, so giving up on this import");
                }
            } finally {
                $lockManager->unlock($lockName1);
            }
        } else {
            throw new LockException("The $lockName1 was locked for more than $lockTimeout seconds, so giving up on this import");
        }
    }
}

It might look a bit ugly, but we have something like this setup on multiple projects (both 2.3.x and 2.4.x Magento versions) and never see a deadlock anymore during imports.

As an additional hint: setup your lock provider to use the filesystem instead of the default database, because the database one can fail when you have a high load on your server and/or if your mysql server restarts. See the documentation about how to do this.

Hope this helps 🙂

@mahmoud-abdelmalek
Copy link

Magento 2.4.4 this error still exists

@ctadlock
Copy link

Still an issue in Magento 2.4.7-p5.

In case you havent figured it out out Magento is a joke. The lack of willingness to fix a now 8 years old issue with 150+ comments is one of many you will find when trying to us it.

@coresh
Copy link

coresh commented Apr 25, 2025

The issue: still in Magento v2.4.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Catalog Fixed in 2.3.x The issue has been fixed in 2.3 release line Fixed in 2.4.x The issue has been fixed in 2.4-develop branch help wanted Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet