magento2
magento2 copied to clipboard
[2.3.0-2.4.4] Changing the config scope while within a plugin breaks the after plugins
Preconditions and environment
Any Magento version between 2.3.0 and 2.4.4. (probably older versions as well)
Steps to reproduce
- Write a public function that calls
\Magento\Framework\Config\ScopeInterface->setCurrentScopeand set a different area then you are currently in (I personally experienced this when we switched from crontab to frontend). - Write a global plugin for above function, it should at least have an after.
Expected result
The after triggers.
Actual result
You get an exception in the exception log saying that the index for the class you are trying to plugin is missing. And the after hasn't triggered.
[2022-06-14 08:13:08] main.CRITICAL: Notice: Undefined index: [class you are trying to plugin] in ./vendor/magento/framework/Interception/PluginList/PluginList.php on line 176 {"exception":"[object] (Exception(code: 0): Notice: Undefined index: [class you are trying to plugin] ./vendor/magento/framework/Interception/PluginList/PluginList.php on line 176 at ./vendor/magento/framework/App/ErrorHandler.php:63)"} []
Additional information
What's happening is that the changing of the currentScope resets the pluginlist, because it's now loaded the pluginlist for the new scope. And the moment the code passes $this->pluginList->getNext it does not to add the plugin back to the pluginlist. If the function is later called again in the same request it will work because the scope is still switched.
Adding
if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) {
$this->_inheritPlugins($type);
}
to pluginList->getPlugin similar to pluginList->getNext would fix the problem
Release note
This bug is very obscure and unlikely to happen, mostly because one rarely changes the currentScope. When it does happen it only happens the first time the function is called during a request.
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.
- [X] 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”.
Hi @Hexmage. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:
- Summary of the issue
- Information on your environment
- Steps to reproduce
- Expected and actual results
Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:
@magento give me 2.4-develop instance - upcoming 2.4.x release
For more details, review the Magento Contributor Assistant documentation.
Add a comment to assign the issue: @magento I am working on this
To learn more about issue processing workflow, refer to the Code Contributions.
- Join Magento Community Engineering Slack and ask your questions in #github channel.
:warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
:clock10: You can find the schedule on the Magento Community Calendar page.
:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.
:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel
Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
-
[ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
Details
If the issue has a valid description, the labelIssue: Format is validwill be added to the issue automatically. Please, edit issue description if needed, until labelIssue: Format is validappears. -
[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
Issue: Clear Descriptionlabel to the issue by yourself. -
[ ] 3. Add
Component: XXXXXlabel(s) to the ticket, indicating the components it may be related to. -
[ ] 4. Verify that the issue is reproducible on
2.4-developbranchDetails
- Add the comment@magento give me 2.4-develop instanceto deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-developbranch, please, add the labelReproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here! -
[ ] 5. Add label
Issue: Confirmedonce verification is complete. -
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hello @Hexmage,
Thanks for the report and collaboration!
We have tried to reproduce the issue in Magento 2.4-develop branch but for us the issue is not reproducible. We have made a custom module for the plugin afterAddProduct, but no error is logged here in the log file.
I am attaching the module prepared by us in order to reproduce the issue. Please review and let us know in case we have missed anything. Magz.zip
Thanks
@engcom-Hotel Your module is not doing what I described.
class A
function aFunction() {
$this->_scopeInterface->setCurrentScope(Area::AREA_ADMINHTML);
}
class PluginOnA
function afterAFunction() {
//this doesn't work
}
Hello @Hexmage,
Thanks for the reply!
We have made the changes as per your comment. Now we have called $this->_scopeInterface->setCurrentScope(Area::AREA_ADMINHTML) method in app/code/Magento/Checkout/Model/Cart:addProduct() line no. 390. And remove it from the plugin.
But still, we are unable to reproduce the issue. For us, the exception log is empty after adding a product to the cart. let us know in case we have missed anything.
Thanks
@engcom-Hotel
I'm trying to think of an easy way to reproduce the issue, because the code that was executing setCurrentScope function was quite a bit more complicated. And the content of the pluginlist was suddenly emptied and not fully rebuild.
The scenario that has to be created is one where the pluginlist is first fully generated, then the currentScope switched which caused the pluginlist to lose some of its content. I'll see if I can take some screenshots of it happening on my end from before I fixed it in my module.
Thanks @Hexmage
We will wait for your reply!
hello
issue confirmed on M2.4.3.-p2
having an afterApplyDataPatch plugin on \Magento\Framework\Setup\Patch\PatchApplier
and a data patch doing
try {
$this->appState->getAreaCode();
} catch (LocalizedException $e) {
$this->appState->setAreaCode(FrontNameResolver::AREA_CODE);
}
=>
Notice: Undefined index: [...]PatchApplier in /app/vendor/magento/framework/Interception/PluginList/PluginList.php on line 174
fixed by changing \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE to \Magento\Framework\App\Area::AREA_GLOBAL
Hello @aurmil,
Thanks for the contribution!
It seems your's and @Hexmage's steps are different in order to reproduce the issue. As per my understanding @Hexmage tries to call setCurrentScope in a public function and then make a plugin on that. But for you, facing some different issue.
So request you to please provide us more information, use cases etc.
Thanks
@engcom-Hotel No my code was calling setCurrentScope within a function that was called indirectly by a plugin and that function called a different plugin that broke.
Hello @Hexmage,
Thanks for the clarification!
Can you please let us know a bit more in order to reproduce the issue as you mentioned here https://github.com/magento/magento2/issues/35617#issuecomment-1196364761?
Thanks
if ($product->getTypeId() !== 'configurable') {
$code = Area::AREA_FRONTEND;
$currentScope = $this->configScope->getCurrentScope();
$this->configScope->setCurrentScope($code);
$price = $this->appState->emulateAreaCode(
$code,
[$this, 'getProductPrice'],
[$product, $group->getId()]
);
$this->configScope->setCurrentScope($currentScope);
} else {
...
}
This was the exact piece of code that was causing the issue for me. This code was called within a piece of code that set Customer Group Prices for a custom elasticsearch indexer. The setCurrentScope was used to get the correct configuration settings, because they would apparently not load the correct ones in the currentScope, resulting in incorrect prices.
If I recall correctly a plugin triggered by the emulated getProductPrice eventually caused the issue. It tried to find the subject class for an after plugin but couldn't find it because the plugin list got emptied. The pluginlist was build way earlier in the process by another plugin being triggered, but because it was already instantiated then it didn't try to recreate it. Removing the set and get currentScope functions stopped the issue from occurring.
Hello @Hexmage,
Thanks for the reply!
Let me understand more about this issue. So the above piece of code is written in a class and on that class, you have made a plugin. Like below:
class A
function aFunction() {
if ($product->getTypeId() !== 'configurable') {
$code = Area::AREA_FRONTEND;
$currentScope = $this->configScope->getCurrentScope();
$this->configScope->setCurrentScope($code);
$price = $this->appState->emulateAreaCode(
$code,
[$this, 'getProductPrice'],
[$product, $group->getId()]
);
$this->configScope->setCurrentScope($currentScope);
} else {
...
}
}
class PluginOnA
function afterAFunction() {
//this doesn't work
}
Is that correct?
Thanks
@engcom-Hotel
Sort of, class A isn't the class that has the after plugin on it. It was more like this (might be even deeper nested)
Class C
function cFunction() {
ClassB->bfunction() {
}
Class B
function bFunction() {
ClassA aFunction() {
}
Class A
function aFunction() {
if ($product->getTypeId() !== 'configurable') {
$code = Area::AREA_FRONTEND;
$currentScope = $this->configScope->getCurrentScope();
$this->configScope->setCurrentScope($code);
$price = $this->appState->emulateAreaCode(
$code,
[$this, 'getProductPrice'],
[$product, $group->getId()]
);
$this->configScope->setCurrentScope($currentScope);
} else {
...
}
}
class PluginOnC
function afterCFunction() {
//this doesn't work
}
Hello @Hexmage,
We have tried to reproduce the issue in the latest 2.4-develop branch but the issue is not reproducible for us. As I can see now the 2.4.5 and further released have happened. So we request you to please try to reproduce the issue in the latest released branch or 2.4-develop branch and let us know if the issue is still reproducible for you.
Thanks
Dear @Hexmage,
We have noticed that this issue has not been updated for a period of 14 Days. Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.
Regards