A very similar problem like #2807567: Checkout fails when Editor module enabled. arises to me after updating from pre-beta version to beta2.

TypeError: Argument 1 passed to _editor_get_file_uuids_by_field() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /var/www/drupalvm/web/core/modules/editor/editor.module on line 381 in _editor_get_file_uuids_by_field() (line 479 of core/modules/editor/editor.module).

Steps to reproduce:

  1. Editor module is turned on
  2. Make sure, no active cart exists
  3. Add a product to the cart

This leads to nested save and load calls of the order entity, leading to stale data during the order refresh process. Shortly explained, these save calls happen:

  1. The cart provider is creating a new order entity and saving it.
  2. The entity itself is setting the order number to its id in postSave()
  3. The storage loads the entity in its pre save hook
  4. OrderRefresh process is started on load, saving the order entity again
  5. Somewhere must be the place, where the data gets stale. the entity is missing its id and order number now, at least also the "original" property added on presave in the storage, gets lost. that's why later on the editor's entity update hook fails
  6. That's also why you the postSave() function of the order entity again tries to set the order number again and does an additional save call

Really strange thing here.. Maybe the OrderRefresh is working on the unchanged entity too?? I haven't found out until now. The only thing I know, is that getting rid of a single save() call makes the problem go away. That also solved the related issue, but still means that it could happen at any time on any place again. E.g. you could repeat the setting of the order number in a preSave() hook again, which seems to prevent one save() action on stale data later.

Comments

agoradesign created an issue. See original summary.

agoradesign’s picture

The following PR adds editor module as a test dependency in order to let AddToCartFormTest fail:

https://github.com/drupalcommerce/commerce/pull/536

agoradesign’s picture

updated the PR - after discussion with mglaman decided to use a custom hook_entity_update() implementation in the test module instead of depending on editor

agoradesign’s picture

The more I think about that, I'm suspect more and more one thing, I've already mentioned in the issue summary:

I guess OrderRefresh is acting also on loading the unchanged entity, which is added as $entity->original by the storage - because loadUnchanged() only resets the static cache and then does call normal load methods, including firing load events of course. However OrderRefresh should never ever act on that. Maybe the order storage needs to override loadUnchanged() and set a flag or so... This smells like a workaround too, but at least it would/could work atm. Further discussion would have to been taken with Core maintainers.

PS: don't have the possibility right now to proof that, neither do I see the backtrace, but I'm very convinced that this could be the cause!

agoradesign’s picture

Status: Active » Needs review
StatusFileSize
new1.95 KB

Seems that my theory was right. See the PR and/or the attached patch. I've added a check to order storage's postLoad hook, in order to prevent that the order refresh process is called on unchanged loads ($entity->original).

attention: only verified it so far on my added test, which failed before and now passes. Did no tests in a real environment,...

agoradesign’s picture

StatusFileSize
new3.07 KB

next try...

agoradesign’s picture

StatusFileSize
new2.93 KB

tiny change to the test module... now it should be ok

bojanz’s picture

Status: Needs review » Needs work

I've commented on the PR, we're almost there.

bojanz’s picture

Title: Add item to a fresh cart fails if Editor module is enabled » The order refresh can cause a save-within-a-save in certain circumstances
Priority: Critical » Major

Discussing this further we realized that the order frequency protects against this, agoradesign was still seeing the problem because his site didn't have order frequency set (due to the update from alpha). The problem is visible in tests because orders stay loaded for longer than 30s (default frequency), an order is loaded, then saved a whole minute later, which causes the orderRefresh in loadUnchanged() to trigger.

Degrading priority to Major since not every install is affected.

Wrapping up the commit.

  • bojanz committed 0dfb84b on 8.x-2.x authored by agoradesign
    Issue #2820034 by agoradesign, bojanz: The order refresh can cause a...
bojanz’s picture

Status: Needs work » Fixed

Boom!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.