Skip to content

Adminhtml observers being called during app:config:import #20806

@kassner

Description

@kassner

Preconditions

  1. Magento 2.2.7;
  2. composer require kassner/module-test-admin-observer:dev-master (source code)
  3. php bin/magento module:enable Kassner_TestAdminObserver

Steps to reproduce

  1. Manually edit the app/etc/config.php file adding a configuration (i.e.: https://gist.github.com/kassner/d1368133ec09541a7a0927cd3964c3e0)
  2. Run php bin/magento app:config:import

Expected result

The observer declared in the module is not executed:

www-data@ec46ba01ae51:~/magento$ php bin/magento app:config:import
Processing configurations data from configuration file...
System config was processed
www-data@ec46ba01ae51:~/magento$ 

Actual result

The observer declared in the module is executed:

www-data@ec46ba01ae51:~/magento$ php bin/magento app:config:import
Processing configurations data from configuration file...
string(64) "Kassner\TestAdminObserver\Observer\ModelSaveBeforeAdmin::execute"
System config was processed
string(64) "Kassner\TestAdminObserver\Observer\ModelSaveBeforeAdmin::execute"
www-data@ec46ba01ae51:~/magento$ 

Description

I did some extensive debugging and found out that during the config import, some methods are invoked using the emulateAreaCode for the adminhtml area. This is fine, but some classes, like Magento\Framework\Event\Config are populated with information related to the adminhtml area, but are never cleaned up or reinstantiated (given the way Magento dependency resolution works), making the next calls outside the emulation to use a dirty configuration, thus calling observers that were just declared for the adminhtml area.

I don't know a good solution for it right now, hence I come here to discuss this. Should we not have this emulation step at all during the app:config:import, or should we make sure that everything the emulateAreaCode steps touch are properly cleaned after?

I have a very cheap patch that I created to test this problem in other environments, and I would love some feedback from anyone that is currently running into those kind of issue.

Extra

This is also related to random failures during setup:upgrade or app:config:import with the message Import failed: Area code not set: Area code must be set before starting a session. for Magento Commerce installations. This is related to the model_save_before observer from Magento_AdminGws kicking in and a dependency of this observer uses the backend session.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: ConfigIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.Progress: ready for devReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releaseSeverity: 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 it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions