Generated with the help of an LLM.

I was working on something for Group and when I asked an LLM to review the tests I wrote, it caught this core bug. It seems like an interesting one that shouldn't cause much harm other than frustrate the end user.

One could argue the bug described below is in NodeForm, but I agree with the LLM that it's more of an underlying issue because other forms may mimic the guards that NodeForm has in place.

Problem/Motivation

  1. SqlContentEntityStorage::save() rolls back the transaction when saving throws an exception. The in-memory entity is not reset. It keeps the ID and revision ID from the rolled-back insert.
  2. EntityStorageBase::doPostSave() calls enforceIsNew(FALSE) before it invokes the insert hooks. An exception in hook_node_insert() therefore leaves an entity that is not new but does not exist.

NodeForm::save() catches the exception and rebuilds the form. The cached form object keeps this entity. The rebuilt form shows the revision options of an existing node. On the next submit, ContentEntityStorageBase::doPreSave() throws "Update existing 'node' entity while changing the ID is not supported." Every later submit fails the same way. The user must reload the form and loses their input.

Steps to reproduce

  1. Implement hook_node_insert() so it throws an exception on the first call only.
  2. Submit the form at node/add/page.
  3. The form shows "The content could not be saved."
  4. Submit the rebuilt form again.
  5. Saving fails with the exception above. The log contains it.

Proposed resolution

When an insert gets rolled back, SqlContentEntityStorage::save() should put the entity's ID, revision ID and "new" state back to what they were before the save. Then the entity really is new again, and the next submit inserts it normally.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3642084

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kristiaanvandeneynde created an issue. See original summary.

kristiaanvandeneynde’s picture

Issue summary: View changes
kristiaanvandeneynde’s picture

Some more notes from the LLM that didn't fit in the IS format:

Updates with a new revision fail in a similar way: the revision ID and loaded revision ID both point to a rolled-back revision. But restoring those needs a setter for the loaded revision ID that doesn't exist yet. That's better as a follow-up than something that slows this issue down.

One open edge case: if rollBack() itself throws TransactionOutOfOrderException, it isn't certain the data is gone. I'd still reset the entity, since the save is reported as failed either way. Reviewers may want to discuss it.

kristiaanvandeneynde’s picture

Status: Active » Needs review

MR also generated with an LLM, but I reviewed it manually and challenged it on some questionable stuff.

Furthermore, the test is in the node module, but the root cause is in the entity system. Can always be moved to use one of the fake entities from the entity_test test module, I suppose.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

For the test coverage since the fix is outside the node module could we add the test to SqlContentEntityStorageTest and use entity_test_rev that's there?