Skip to content
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

Doubled MySQL connection issue when runnig console command via magerun #667

Closed
niclashoyer opened this issue Nov 26, 2020 · 5 comments
Closed

Comments

@niclashoyer
Copy link

Describe the bug

We noticed a strange behavior when starting a console command via magerun2. The command itself is implemented in an extension and imports products into the magento database using the ProductRepository-Interfaces. So, we load the product

$productObject = $this->_productRepository->get($product['sku'], true, Data::DEFAULT_STORE_ID, true);

then update / add data and later save it again

$this->_productRepository->save($productObject);

If we are running this via bin/magento example:import everything works as expected. But if we are starting this command via magerun2 example:import the import hangs on the save call and after some time we get the MySQL error

lock wait timeout exceeded

Expected behaviour

The command should run the same way as if started with bin/magento.

Steps to reproduce the issue

We can't provide a minimal example at this point, so we'd first need to figure out how to reproduce this with minimal effort.

Technical details

  • Host Machine OS (Windows/Linux/Mac): Ubuntu 20.04
  • MariaDB 10.3.25
  • PHP 7.1 (will be upgraded soon)
  • Magento 2 2.2.11
  • magerun2 3.2.0 (as we are still on Magento 2.2.x)

Additional context

Upon debugging this we found out that if the command is run via magerun that two connections are opened to the database, which then lock themselves (in the log below ID 96 and 97). If we are using bin/magento directly it only opens one connection.

show queries
/usr/sbin/mysqld, Version: 10.3.25-MariaDB-0ubuntu0.20.04.1 (Ubuntu 20.04). started with:
Tcp port: 3306  Unix socket: /run/mysqld/mysqld.sock
Time                Id Command  Argument
201126 14:16:36     96 Connect  magento2@localhost as anonymous on magento2
                    96 Query    SET SQL_MODE=''
                    96 Query    SET time_zone = '+00:00'
                    96 Query    SET NAMES utf8
                    96 Query    SELECT `store_website`.* FROM `store_website`
                    96 Query    SELECT `store_group`.* FROM `store_group`
                    96 Query    SELECT `store`.* FROM `store`
201126 14:16:37     97 Connect  magento2@localhost as anonymous on magento2
                    97 Query    SET SQL_MODE=''
                    97 Query    SET time_zone = '+00:00'
                    97 Query    SET NAMES utf8
                    97 Query    SELECT `store_website`.* FROM `store_website`
                    97 Query    SELECT `store_group`.* FROM `store_group`
                    97 Query    SELECT `store`.* FROM `store`
                    96 Query    SELECT `mview_state`.* FROM `mview_state` WHERE (`mview_state`.`view_id`='customer_dummy')
                    96 Query    SELECT `indexer_state`.* FROM `indexer_state` WHERE (`indexer_state`.`indexer_id`='customer_grid')
                    96 Query    START TRANSACTION
                    96 Query    COMMIT
                    96 Query    SELECT `mview_state`.* FROM `mview_state` WHERE (`mview_state`.`view_id`='design_config_dummy')
                    96 Query    SELECT `indexer_state`.* FROM `indexer_state` WHERE (`indexer_state`.`indexer_id`='design_config_grid')
                    96 Query    START TRANSACTION
                    96 Query    COMMIT
…
                    97 Query    SELECT `eav_attribute`.`attribute_code` FROM `eav_attribute` WHERE (frontend_input = 'media_image')
                    96 Query    SELECT `catalog_product_entity_varchar`.`value_id`, `catalog_product_entity_varchar`.`value` FROM `catalog_product_entity_varchar` WHERE (attribute_id='87' AND entity_id='250063' AND store_id=0)
                    96 Query    SELECT `catalog_product_entity_varchar`.* FROM `catalog_product_entity_varchar` WHERE (attribute_id = '87') AND (entity_id = '250063') AND (store_id <> 0) LIMIT 1
                    96 Query    INSERT  INTO `catalog_product_entity_varchar` (`attribute_id`,`store_id`,`entity_id`,`value`) VALUES ('87', '0', '250063', '/1/4/1427269.jpg') ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)

@cmuench
Copy link
Member

cmuench commented Nov 26, 2020

@niclashoyer thanks for the report. Sadly it will be hard to reproduce the behavior in a vanilla Magento installation. If we can reporoduce the issue in a Magento 2.3 or 2.4 we will deploy a fix for that. The Magento 2.x is is out of support at Magento and we decided that the current n98-magerun2 release will only test and support the official supported Magento versions.
But I guess that the issue is also reproducable in Magento 2.3/2.4.

Can you give me some informations about the custom command. How do you inject the "$_productRepository"? Is it using the __constructor injection or the n98-magerun2 inject method?

@niclashoyer
Copy link
Author

The repository is injected via constructor injection using DI (not in the console command itself, but in another class that is then injected to the console command via constructor injection):

    public function __construct(
        ProductRepositoryInterface $productRepository,

    ) {
        $this->_productRepository          = $productRepository;
…
    }

@niclashoyer
Copy link
Author

Oh as I now saw the importer class is actually "injected" via objectManager in the execute-method:

   protected function execute(
        InputInterface $input,
        OutputInterface $output
    ) {
        $this->_import = $this->_objectManager->get('\Example\ImportCatalog\Model\ImportCatalog');
…

which obviously is bad practice, but may explain the strange behavior. I'll see if the issue is gone if I change that to constructor DI.

@niclashoyer
Copy link
Author

No, unfortunately the issue is still there when using only DI constructor injection.

@cmuench
Copy link
Member

cmuench commented May 10, 2022

We found an issue that we initialized Magento twice. That issue is fixed in the latest n98-magerun2 v5.1.0.
This issue is very old and we do not support Magento 2.2 anymore. I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants