magento2: [2.4.6-p2] Regression, can trigger error 'Connection "default" is not defined' again

Preconditions and environment

  • Magento version 2.4.6-p2 (pre-release which isn’t available on github here yet) but can also be reproduced on 2.4-develop

Steps to reproduce

  1. Setup a clean Magento
  2. Ensure there is a file app/etc/config.php
  3. Remove the file app/etc/env.php
  4. <del>Make sure you empty the current database, it should hold no data or structure (this might be optional, I haven’t checked with an existing database)</del> (Update: confirmed that database doesn’t need to be emptied)
  5. Run:
# replace the db parameters with the ones from your own database

./bin/magento setup:install \
    --admin-firstname=Firstname \
    --admin-lastname=Lastname \
    --admin-email=email@example.com \
    --admin-user=admin \
    --admin-password=admin124 \
    --base-url=https://url.example/ \
    --base-url-secure=https://url.example/ \
    --backend-frontname=admin \
    --db-host={dbhost} \  # your db host
    --db-name={dbname} \  # your db name
    --db-user={dbuser} \  # your db user
    --db-password={dbpass} \  # your db user's password
    --language=en_US \
    --currency=EUR \
    --timezone=Europe/Brussels \
    --use-rewrites=1 \
    --use-secure=1 \
    --use-secure-admin=1 \
    --admin-use-security-key=1 \
    --session-save=files \
    --cleanup-database \
    -vvv

Expected result

Works without issues

Actual result

Getting

...
[Progress: 861 / 864]
Disabling Maintenance Mode:

In ResourceConnection.php line 162:

  [DomainException]
  Connection "default" is not defined


Exception trace:
  at lib/internal/Magento/Framework/App/ResourceConnection.php:162
 Magento\Framework\App\ResourceConnection->getConnectionByName() at generated/code/Magento/Framework/App/ResourceConnection/Interceptor.php:41
 Magento\Framework\App\ResourceConnection\Interceptor->getConnectionByName() at lib/internal/Magento/Framework/App/ResourceConnection.php:111
 Magento\Framework\App\ResourceConnection->getConnection() at generated/code/Magento/Framework/App/ResourceConnection/Interceptor.php:23
 Magento\Framework\App\ResourceConnection\Interceptor->getConnection() at lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php:312
 Magento\Framework\Model\ResourceModel\Db\AbstractDb->getConnection() at app/code/Magento/MessageQueue/Model/ResourceModel/Lock.php:103
 Magento\MessageQueue\Model\ResourceModel\Lock->releaseOutdatedLocks() at app/code/Magento/MessageQueue/Model/Plugin/ResourceModel/Lock.php:39
 Magento\MessageQueue\Model\Plugin\ResourceModel\Lock->afterSet() at lib/internal/Magento/Framework/Interception/Interceptor.php:146
 Magento\Framework\App\MaintenanceMode\Interceptor->Magento\Framework\Interception\{closure}() at lib/internal/Magento/Framework/Interception/Interceptor.php:153
 Magento\Framework\App\MaintenanceMode\Interceptor->___callPlugins() at generated/code/Magento/Framework/App/MaintenanceMode/Interceptor.php:32
 Magento\Framework\App\MaintenanceMode\Interceptor->set() at setup/src/Magento/Setup/Model/Installer.php:1503
 Magento\Setup\Model\Installer->setMaintenanceMode() at n/a:n/a
 call_user_func_array() at setup/src/Magento/Setup/Model/Installer.php:388
 Magento\Setup\Model\Installer->install() at setup/src/Magento/Setup/Console/Command/InstallCommand.php:238
 Magento\Setup\Console\Command\InstallCommand->execute() at vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at vendor/symfony/console/Application.php:1040
 Symfony\Component\Console\Application->doRunCommand() at vendor/symfony/console/Application.php:301
 Symfony\Component\Console\Application->doRun() at lib/internal/Magento/Framework/Console/Cli.php:116
 Magento\Framework\Console\Cli->doRun() at vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at bin/magento:23

Additional information

This problem does not occur with Magento 2.4.6-p1, so this is a regression bug. Issue is most likely to happen in CI pipelines, to run integration tests, or whatever else needs a database.

This can be solved

  • by removing the app/etc/config.php before running bin/magento setup:install (but that can lead to other problems)
  • by running bin/magento setup:install twice (where you account for the first one to fail, the first execution will generate a env.php file, so the second run can load this config in memory)

This bug has something to do with these changes that were added in Magento 2.4.6-p2: ACP2E-1958: avoid reloading configs for each requested key (cc @bubasuma & @o-iegorov)

It has something to do with trying to fetch the configuration for key db which it doesn’t find, because it has only loaded the config from config.php file and not from the env.php file. So most likely once the db configuration is written to env.php file, it’s not being refreshed in memory and leads to this problem.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 24 (17 by maintainers)

Commits related to this issue

Most upvoted comments

@alzota: please don’t close issues without leaving feedback about which commit(s) fixed it.

Can you provide these? Thanks!

@hostep @convenient Internal team is already working on the issue.

In our cases we see this as part of the process of running the integration tests, this is because the integration tests need to run setup:install under the hood.

I’ve distilled what we do into this (super hacky) script that reproduces the issue.

When I set MAGE_MODE=2.4.6 the integration tests run fine.

run_integration_tests - start
-rw-r--r-- 1 lukerodgers staff   926 Jul 31 15:56 app/etc/NonComposerComponentRegistration.php
-rw-rw-r-- 1 lukerodgers staff 13946 Jul 31 15:56 app/etc/config.php
-rw-rw-r-- 1 lukerodgers staff    78 Jul 31 15:57 app/etc/env.php
-rw-r--r-- 1 lukerodgers staff   516 Jul 31 15:56 app/etc/registration_globlist.php
-rw-r--r-- 1 lukerodgers staff    70 Jul 31 15:56 app/etc/vendor_path.php
pwd
PHPUnit 9.6.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.2
Configuration: /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/dev/tests/integration/phpunit.xml.dist
Warning:       Using a custom test suite loader is deprecated

Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MarkAsReadTest::testAclHasAccess' started
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MarkAsReadTest::testAclHasAccess' ended
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MarkAsReadTest::testAclNoAccess' started
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MarkAsReadTest::testAclNoAccess' ended
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MassMarkAsReadTest::testAclHasAccess' started
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MassMarkAsReadTest::testAclHasAccess' ended
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MassMarkAsReadTest::testAclNoAccess' started
Test 'Magento\AdminNotification\Controller\Adminhtml\Notification\MassMarkAsReadTest::testAclNoAccess' ended


Time: 00:07.258, Memory: 319.50 MB

OK (4 tests, 6 assertions)

=== Memory Usage System Stats ===
Memory usage (OS):	357.81M (113.41% of 315.50M reported by PHP)
Estimated memory leak:	42.31M (11.83% of used memory)

run_integration_tests - end

When I set MAGE_MODE=2.4.6-p2 it fails with

Module 'Temando_ShippingRemover':
[Progress: 1413 / 1419]
Enabling caches:
Current status:
layout: 1
block_html: 1
full_page: 1
[Progress: 1414 / 1419]
Installing admin user...
[Progress: 1415 / 1419]
Caches clearing:
Cache cleared successfully
[Progress: 1416 / 1419]
Disabling Maintenance Mode:

In ResourceConnection.php line 148:

  [DomainException]
  Connection "default" is not defined


Exception trace:
  at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/App/ResourceConnection.php:148
 Magento\Framework\App\ResourceConnection->getConnectionByName() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/App/ResourceConnection.php:97
 Magento\Framework\App\ResourceConnection->getConnection() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php:312
 Magento\Framework\Model\ResourceModel\Db\AbstractDb->getConnection() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/module-message-queue/Model/ResourceModel/Lock.php:103
 Magento\MessageQueue\Model\ResourceModel\Lock->releaseOutdatedLocks() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/module-message-queue/Model/Plugin/ResourceModel/Lock.php:39
 Magento\MessageQueue\Model\Plugin\ResourceModel\Lock->afterSet() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/Interception/Interceptor.php:146
 Magento\Framework\App\MaintenanceMode\Interceptor->Magento\Framework\Interception\{closure}() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/Interception/Interceptor.php:153
 Magento\Framework\App\MaintenanceMode\Interceptor->___callPlugins() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/generated/code/Magento/Framework/App/MaintenanceMode/Interceptor.php:23
 Magento\Framework\App\MaintenanceMode\Interceptor->set() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/setup/src/Magento/Setup/Model/Installer.php:1503
 Magento\Setup\Model\Installer->setMaintenanceMode() at n/a:n/a
 call_user_func_array() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/setup/src/Magento/Setup/Model/Installer.php:388
 Magento\Setup\Model\Installer->install() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/setup/src/Magento/Setup/Console/Command/InstallCommand.php:237
 Magento\Setup\Console\Command\InstallCommand->execute() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/symfony/console/Application.php:1040
 Symfony\Component\Console\Application->doRunCommand() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/symfony/console/Application.php:301
 Symfony\Component\Console\Application->doRun() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/magento/framework/Console/Cli.php:116
 Magento\Framework\Console\Cli->doRun() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /Users/lukerodgers/src/magento2-vanilla-versions/instances/broken-magento-install/bin/magento:23

Reproduction script and usage

It’s not a great script but it does repro for me. Using

  • local elastic search at port 9200
  • local mysql at configurable MYSQL_PORT and MYSQL_HOST (edit at top of script)
  • packagist repo for the composer repository, but if you have your auth configured it should be swappable for repo.magento.com
  • MAGE_VERSION='2.4.6-p2' can be swapped around

This script will create a rm -rf and create a subdir broken-magento-install from the place you’re running it.

#!/bin/bash
COMPOSER_REPOSITORY=https://repo.packagist.com/your_org_here/
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='24012'
MYSQL_TEST_HOST_FULL="$MYSQL_HOST:$MYSQL_PORT"
MAGE_VERSION='2.4.6-p2'

DIR_BASE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -E
err_report() {
  trap - ERR
  echo "Error on line $1"
  exit 1;
}
trap 'err_report $LINENO' ERR

cd $DIR_BASE

function deps() {
    echo "verify dependencies - start"
    # verify elastic is available
    curl http://0.0.0.0:9200

    # verify php 8.2
    php --version | grep 8.2

    # verify mysql
    mysql  -uroot -h$MYSQL_HOST --port=$MYSQL_PORT -e 'select 1'
    echo "verify dependencies - end"
}

function install_magento() {
    echo "install_magento - start"
    rm -rf ./broken-magento-install

    composer create-project --repository="$COMPOSER_REPOSITORY" magento/project-community-edition:"$MAGE_VERSION" ./broken-magento-install --no-install --no-plugins
    cd broken-magento-install

    #composer require n98/magerun2:"*" --dev --no-interaction --no-update
    composer config --unset repo.0
    composer config repo.composerrepository composer "$COMPOSER_REPOSITORY"
    #composer config repositories.packagist.org false
    #cat composer.json
    composer install
    php bin/magento module:enable --all
    echo "install_magento - end"
}

function setup_integration_tests() {
    echo "setup_integration_tests - start"
    cd $DIR_BASE/broken-magento-install

    printf "<?php\n"                                                                              > dev/tests/integration/etc/install-config-mysql.php
    printf "return [\n"                                                                        >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'search-engine' => 'elasticsearch7',\n"                                        >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'elasticsearch-host'    => 'localhost',\n"                                     >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'elasticsearch-port'    => 9200,\n"                                            >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'db-host' => '$MYSQL_TEST_HOST_FULL',\n"                                       >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'db-user' => 'root',\n"                                                        >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'db-password' => '',\n"                                                        >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'db-name' => 'magento_integration_tests',\n"                                   >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'db-prefix' => 'trv_',\n"                                                      >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'backend-frontname' => 'backend',\n"                                           >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'admin-user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,\n"               >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'admin-password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,\n"       >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'admin-email' => \Magento\TestFramework\Bootstrap::ADMIN_EMAIL,\n"             >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'admin-firstname' => \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME,\n"     >> dev/tests/integration/etc/install-config-mysql.php
    printf "    'admin-lastname' => \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME,\n"       >> dev/tests/integration/etc/install-config-mysql.php
    printf "];\n"                                                                              >> dev/tests/integration/etc/install-config-mysql.php

    php -l dev/tests/integration/etc/install-config-mysql.php

    rm -rf dev/tests/integration/tmp/sandbox-*;
    rm -rf generated/code;
    mysql  -uroot -h$MYSQL_HOST --port=$MYSQL_PORT -e 'drop database if exists magento_integration_tests; create database magento_integration_tests'
    mysql  -uroot -h$MYSQL_HOST --port=$MYSQL_PORT magento_integration_tests -e 'show databases' | grep magento

    test -f app/etc/config.php

    rm -rf var/cache;
    php bin/magento setup:di:compile
    rm -rf var/cache;

    echo "setup_integration_tests - end"
}

function run_integration_tests() {
    echo "run_integration_tests - start"
    cd $DIR_BASE/broken-magento-install

    ls -l app/etc/*.php

    set -v
    vendor/bin/phpunit -c $(pwd)/dev/tests/integration/phpunit.xml.dist --filter MarkAsReadTest -vvv --debug
    set +v

    echo "run_integration_tests - end"
}

deps;
install_magento;
setup_integration_tests;
run_integration_tests;

@hostep fix is implemented, but not merged yet. Will come back with a commit id on this issue once the fix is merged. Thank you!

Hi @hostep ,

Thanks for Contribution and collaboration!

Verified the issue in 2.4-develop instance as per your last comments and issue can be reproducible,Hence we confirming the issue.Kindly refer the below screenshot: Screenshot 2023-08-01 at 1 42 47 PM

@hostep @engcom-Charlie I think we should add some automated test coverage for such case to prevent such regressions in the future

Today, after trying out the commit mentioned above, I can confirm that it fixes this issue for us. Thanks for the quick solution guys!

Thanks for confirming @alzota, that means we can close this ticket!

Possibly fixed by ACP2E-2198: fix scenario where configs are not reloaded? Need to find some time to test it out…

@hostep indeed that is the fix.

Here is a patch with a workaround to get the integration tests running again until a proper fix is released:

@package magento/framework
@level 4

Index: vendor/magento/framework/App/DeploymentConfig.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/magento/framework/App/DeploymentConfig.php b/vendor/magento/framework/App/DeploymentConfig.php
--- a/vendor/magento/framework/App/DeploymentConfig.php
+++ b/vendor/magento/framework/App/DeploymentConfig.php	(date 1692798116511)
@@ -94,9 +94,7 @@
         }
         $result = $this->getByKey($key);
         if ($result === null) {
-            if (empty($this->flatData) || count($this->getAllEnvOverrides())) {
-                $this->reloadData();
-            }
+            $this->reloadData();
             $result = $this->getByKey($key);
         }
         return $result ?? $defaultValue;
@@ -184,9 +182,7 @@
      */
     private function reloadInitialData(): void
     {
-        if (empty($this->readerLoad) || empty($this->data) || empty($this->flatData)) {
-            $this->readerLoad = $this->reader->load();
-        }
+        $this->readerLoad = $this->reader->load();
         $this->data = array_replace(
             $this->readerLoad,
             $this->overrideData ?? [],

@hostep @engcom-Charlie I think we should add some automated test coverage for such case to prevent such regressions in the future

Fully agree with @ihor-sviziev

I think its weird that my automated testing which covers the following flakes periodically as new versions of magento are released, seems like it would be trivial enough to set up internally within adobe.

  • composer create project magento/project-community-edition
  • php bin/magento module:enable --all
  • setup and config to then run the integration tests

@hostep thanks for confirmation. the label “Progress: PR in progress” made me assume that you might be working on it, that’s why I wanted to confirm.

@alzota: what do you mean? I’m not working on this issue, did I say I would maybe?

I see a label got added Progress: PR in progress but I have no idea why or how?