magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.54k stars 9.32k forks source link

Environment emulation nesting when sending invoice email #35603

Closed pumpe24 closed 1 year ago

pumpe24 commented 2 years ago

On Magento Open Source 2.4.4, $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true); is added to Magento\Sales\Model\Order\Email\Sender\InvoiceSender::send . While on this emulation there is a call to Magento\Payment\Helper\Data::getInfoBlockHtml where the emulation is started again.

This results in an error message main.ERROR: Environment emulation nesting is not allowed in system.log every time an invoice email is sent.

Preconditions (*)

  1. Magento Open Source 2.4.4

Steps to reproduce (*)

  1. send an invoice email

Expected result (*)

  1. it works, but system.log is flooded with the error message below

Actual result (*)

  1. system.log is flooded with main.ERROR: Environment emulation nesting is not allowed
m2-assistant[bot] commented 2 years ago

Hi @pumpe24. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

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.


: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

kassner commented 2 years ago

This was introduced at https://github.com/magento/magento2/commit/46b9d30e5c4b5a0499148968b8c708a7acbbf3da, which unfortunately missed the Magento\Sales\Model\Order\Email\Sender\OrderSender, so we can't just remove the call from getInfoBlockHtml.

m2-assistant[bot] commented 2 years ago

Hi @engcom-November. 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:

engcom-November commented 2 years ago

Verified the issue on Magento 2.4-develop branch and the issue is reproducible. [2022-06-16T13:23:41.336531+00:00] main.ERROR: Environment emulation nesting is not allowed. [] [] is triggered in system.log on sending invoice email. Hence confirming this issue.

nisha-vaghela commented 2 years ago

@magento i am working on this

simonmaass commented 2 years ago

can confirm this issue on 2.4.4 - not only on invoices but also on credit memos

drinkingsouls commented 2 years ago

I am seeing this error in my system.log too since upgrading to Magento 2.4.4

main.ERROR: Environment emulation nesting is not allowed. [] []

What was the solution for this?

xtremevision commented 2 years ago

Same here, also seeing it after upgrade to 2.4.4. I also think it's affecting cart integrity/behavior, as in, I place the order, it completes successfully, I see the error in the log, the cart shows it's empty, I got to a category page and the same product and suddenly the cart shows 2 items. If I proceed with the order and try to place it, I get an exception:

[2022-08-10T23:09:54.909522+00:00] main.ERROR: Magento2 v3.2.7: The cart details have changed. Please refresh the page and try again (1). [] [] [2022-08-10T23:09:54.909680+00:00] main.ERROR: Magento2 v3.2.7: #0 /home/michael/www/Farmacias-Roma-Magento/htdocs/vendor/stripe/module-payments/Model/PaymentMethod.php(180): StripeIntegration\Payments\Model\PaymentElement->updateFromOrder()

(I didn't paste the whole stack trace, too big)

If I refresh the page, I can place the order successfully, but the same issue happens again.

reense commented 2 years ago

We're having this issue with multiple shops as well.

kalalruturaj commented 2 years ago

any solution on this issue?

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-6611 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

LeoNun commented 2 years ago

Hello, having the same problem in magento 2.4.5.

Best regards, L Nunes

jdubbya commented 2 years ago

This is also occurring when sending out shipment notification emails in Magento 2.4.4-p1.

simonmaass commented 2 years ago

@nisha-vaghela are you working on this?

danielpugh commented 2 years ago

seeing this problem as well

i6media commented 2 years ago

Same here, possibly related to invoices being sent twice by cron (1 minute after each other)

MartynasKasp commented 1 year ago

The issue still exists in 2.4.5-p1 (community edition) when sending an email after shipping an order.

simonmaass commented 1 year ago

can confirm this for 2.4.5-p1

MikeDevs commented 1 year ago

Same issue on 2.4.4-p2

rofokken commented 1 year ago

Still exist on: Adobe Commerce ver. 2.4.5-p1

glo23503 commented 1 year ago

Internal team started working on the issue.

m2-assistant[bot] commented 1 year ago

Hi @glo23503! :wave: Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

m2-assistant[bot] commented 1 year ago

Hi @o-iegorov. Thank you for working on this issue. Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:


michi-z commented 1 year ago

Hello, when will this PR be handled?

hostep commented 1 year ago

@glo23503: this issue got closed without any comments, any idea why?

Might have been fixed by ACP2E-1341: [On-Premise] Merchant Informed Core Issue Magento 2.4.4 main.ERROR: Environment emulation nesting is not allowed, unfortunately I can't seem to find a clean merge commit

MTheProgrammer commented 1 year ago

Still present in 2.4.5-p1

peterjaap commented 1 year ago

@kassner @hostep @simonmaass anyone aware of a workaround? We're running into this as well.

simonmaass commented 1 year ago

@peterjaap hey peter! unfortunately not... currently living with the logspam... it doesnt seem to have an inpact on the actual functionality...

mv-ics commented 1 year ago

Same here. Any update on why this is closed? Is there a patch we can add to fix it temporarily?

peterjaap commented 1 year ago

I haven't tried this yet, but wouldn't adding $this->appEmulation->stopEnvironmentEmulation() right before starting it kill the current emulation, thereby avoiding nesting?

sebfie commented 1 year ago

Hello,

Can you please provide us a patch or any visibility on a PR?

ihor-sviziev commented 1 year ago

@sivaschenko @sidolov @kassner , why this issue was closed? If it was fixed, please provide link to commit / pull request

o-iegorov commented 1 year ago

Internal team already fixed the issue, merge commit id: https://github.com/magento/magento2/commit/3e5be99a940ef572e1a99fee8b9258fda5be9ff4

peterjaap commented 1 year ago

Thanks!

Relevant changes are here, here and here.

So the fix was moving the creation of the payment HTML block to outside the emulation which doesn't matter since that info is not store specific.

Here's the patch with just those changes:

@package magento/module-sales
@link https://github.com/magento/magento2/issues/35603
@link https://github.com/magento/magento2/commit/3e5be99a940ef572e1a99fee8b9258fda5be9ff4
@level 5

From f1538d3d453e9de884d78eb91c1177562d0e6a25 Mon Sep 17 00:00:00 2001
From: Sarmistha Martha <glo23503@adobe.com>
Date: Fri, 11 Nov 2022 13:11:19 +0530
Subject: [PATCH 12/51] ACP2E-1341: [On-Premise] Merchant Informed Core Issue
 Magento 2.4.4 main.ERROR: Environment emulation nesting is not allowed

---
 .../Order/Email/Sender/CreditmemoSender.php   |  3 +-
 .../Order/Email/Sender/InvoiceSender.php      |  3 +-
 .../Order/Email/Sender/ShipmentSender.php     |  3 +-

diff --git a/app/code/Magento/Sales/Model/Order/Email/Sender/CreditmemoSender.php b/app/code/Magento/Sales/Model/Order/Email/Sender/CreditmemoSender.php
index 19dc627725138..22d5bc2b7a562 100644
--- a/app/code/Magento/Sales/Model/Order/Email/Sender/CreditmemoSender.php
+++ b/app/code/Magento/Sales/Model/Order/Email/Sender/CreditmemoSender.php
@@ -118,6 +118,7 @@ public function send(Creditmemo $creditmemo, $forceSyncMode = false)

         if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
             $order = $creditmemo->getOrder();
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -126,7 +127,7 @@ public function send(Creditmemo $creditmemo, $forceSyncMode = false)
                 'creditmemo_id' => $creditmemo->getId(),
                 'comment' => $creditmemo->getCustomerNoteNotify() ? $creditmemo->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
diff --git a/app/code/Magento/Sales/Model/Order/Email/Sender/InvoiceSender.php b/app/code/Magento/Sales/Model/Order/Email/Sender/InvoiceSender.php
index 1ca2efef4d8f5..585da4d965840 100644
--- a/app/code/Magento/Sales/Model/Order/Email/Sender/InvoiceSender.php
+++ b/app/code/Magento/Sales/Model/Order/Email/Sender/InvoiceSender.php
@@ -126,6 +126,7 @@ public function send(Invoice $invoice, $forceSyncMode = false)
                 $order->setBaseTaxAmount((float) $invoice->getBaseTaxAmount());
                 $order->setBaseShippingAmount((float) $invoice->getBaseShippingAmount());
             }
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -134,7 +135,7 @@ public function send(Invoice $invoice, $forceSyncMode = false)
                 'invoice_id' => $invoice->getId(),
                 'comment' => $invoice->getCustomerNoteNotify() ? $invoice->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
diff --git a/app/code/Magento/Sales/Model/Order/Email/Sender/ShipmentSender.php b/app/code/Magento/Sales/Model/Order/Email/Sender/ShipmentSender.php
index ba6d65a40c653..78aaf1a696e9a 100644
--- a/app/code/Magento/Sales/Model/Order/Email/Sender/ShipmentSender.php
+++ b/app/code/Magento/Sales/Model/Order/Email/Sender/ShipmentSender.php
@@ -120,6 +120,7 @@ public function send(Shipment $shipment, $forceSyncMode = false)
         if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
             $order = $shipment->getOrder();
             $this->identityContainer->setStore($order->getStore());
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -128,7 +129,7 @@ public function send(Shipment $shipment, $forceSyncMode = false)
                 'shipment_id' => $shipment->getId(),
                 'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
ihor-sviziev commented 1 year ago

Thank you so much! Now it's absolutely clear that it was already fixed and not just closed by bot because of some glitch or whatever.

mobweb commented 1 year ago

And the patch below worked for me and I was able to patch the files in my vendor directory. Note that I had to change the paths of the files and add an empty newline at the very end of the patch.

Just copy and paste the contents below into a file named emulation.patch in your Magento base directory, then run git apply emulation.patch in that directory.

diff --git a/vendor/magento/module-sales/Model/Order/Email/Sender/CreditmemoSender.php b/vendor/magento/module-sales/Model/Order/Email/Sender/CreditmemoSender.php
index 19dc627725138..22d5bc2b7a562 100644
--- a/vendor/magento/module-sales/Model/Order/Email/Sender/CreditmemoSender.php
+++ b/vendor/magento/module-sales/Model/Order/Email/Sender/CreditmemoSender.php
@@ -118,6 +118,7 @@ public function send(Creditmemo $creditmemo, $forceSyncMode = false)

         if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
             $order = $creditmemo->getOrder();
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -126,7 +127,7 @@ public function send(Creditmemo $creditmemo, $forceSyncMode = false)
                 'creditmemo_id' => $creditmemo->getId(),
                 'comment' => $creditmemo->getCustomerNoteNotify() ? $creditmemo->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
diff --git a/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php b/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php
index 1ca2efef4d8f5..585da4d965840 100644
--- a/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php
+++ b/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php
@@ -126,6 +126,7 @@ public function send(Invoice $invoice, $forceSyncMode = false)
                 $order->setBaseTaxAmount((float) $invoice->getBaseTaxAmount());
                 $order->setBaseShippingAmount((float) $invoice->getBaseShippingAmount());
             }
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -134,7 +135,7 @@ public function send(Invoice $invoice, $forceSyncMode = false)
                 'invoice_id' => $invoice->getId(),
                 'comment' => $invoice->getCustomerNoteNotify() ? $invoice->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
diff --git a/vendor/magento/module-sales/Model/Order/Email/Sender/ShipmentSender.php b/vendor/magento/module-sales/Model/Order/Email/Sender/ShipmentSender.php
index ba6d65a40c653..78aaf1a696e9a 100644
--- a/vendor/magento/module-sales/Model/Order/Email/Sender/ShipmentSender.php
+++ b/vendor/magento/module-sales/Model/Order/Email/Sender/ShipmentSender.php
@@ -120,6 +120,7 @@ public function send(Shipment $shipment, $forceSyncMode = false)
         if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
             $order = $shipment->getOrder();
             $this->identityContainer->setStore($order->getStore());
+            $paymentHTML = $this->getPaymentHtml($order);
             $this->appEmulation->startEnvironmentEmulation($order->getStoreId(), Area::AREA_FRONTEND, true);
             $transport = [
                 'order' => $order,
@@ -128,7 +129,7 @@ public function send(Shipment $shipment, $forceSyncMode = false)
                 'shipment_id' => $shipment->getId(),
                 'comment' => $shipment->getCustomerNoteNotify() ? $shipment->getCustomerNote() : '',
                 'billing' => $order->getBillingAddress(),
-                'payment_html' => $this->getPaymentHtml($order),
+                'payment_html' => $paymentHTML,
                 'store' => $order->getStore(),
                 'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                 'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
peterjaap commented 1 year ago

@mobweb I formatted the patch to use with vaimo/composer-patches, so you don't have to change the file paths in the patch. That's what @level 5 at the top of the patch does.

Applying the patch they way you describe doesn't help when you deploy Magento - that only changes the files locally.

qaniel commented 1 year ago

So, when this is hitting a release or a quality patch? or is it already out there? (couldn't find it :( )

hostep commented 1 year ago

@qaniel: it's not included in Magento 2.4.6 which got released today, so you'll have to wait until version 2.4.7 which will be released in the year 2024 (I'm not joking). No idea if Adobe is going to turn this into a quality patch, maybe consider creating a request for it: https://github.com/magento/quality-patches/issues

But if you need it urgently, you'd probably want to follow @peterjaap's advise and use a custom patch.

drinkingsouls commented 1 year ago

@Yuwei9291

$order->getStoreId() ----> $order->createStoreId() it will solve the problem.

Where exactly did you add this?

Yuwei9291 commented 1 year ago

@Yuwei9291

$order->getStoreId() ----> $order->createStoreId() it will solve the problem.

Where exactly did you add this?

Sorry that's my fault. this issue happened when I use an old email template. please ignore and sorry for the misunderstanding.

For my issues, the designer of the email template use objectManager to load product

$product = $objectManagerHere->get('Magento\Catalog\Model\Product')->load($productId);

and this row cause the Environment emulation nesting error. and once i checked and edited to $product = $objectManagerHere->create('Magento\Catalog\Model\Product')->load($productId); the problem solved.

I improved the code by using dependency injection by ChatGPT so I forget the detailed solution of the problem, just remember to change get to create.

Sorry I'm a seller not a programmer, the problem happened in multiple store mode when I using jopin's email template and once I removed it, the problem does not recur.

I hope my solution will tip off some users who have this error