For some reason, I tried to create custom entities with Linked List data structure. Each entity has referenced field to children entities and each children entity has referenced field to parent entity. In this case when I import those entities, the batch script is stuck. I suppose it tries to find the dipest entity in a stack but it goes to the infinite loop throug refereces I described.

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

lobodacyril created an issue. See original summary.

lobodakyrylo’s picture

StatusFileSize
new2.11 KB

I've found that it's realy infinite loop. I created patch and it works for me.

lobodakyrylo’s picture

Status: Needs work » Needs review
blanca.esqueda’s picture

Status: Needs review » Needs work

Hi @lobodacyril

Thanks for the patch
I added content reference fields to the basic page and article content types:

  1. Basic Page (Content Type)
    • Reference Field to Article
  2. Article (Content Type)
    • Reference Field to Basic Page

You right, it causes an infinite cycle.
The patch fixes the cycle, but the reference is not correct for the first node.

When the first node is being imported let's say the basic page: the field reference on the basic page pointing to the article will be empty, because the article doesn't exist yet.
Then when the article node is imported the field reference pointing to the basic page is correct because the page exists at this point.

--
Note:
If the import is run again, then the article reference on the basic page will be updated because now the article exists too.

This is like the chicken or the egg.. the entity reference has to exist in order to be set ><

guiu.rocafort.ferrer’s picture

StatusFileSize
new1.32 KB

I took a different approach to the patch above, where I use the already existing variable $visited and I store the current entity before calling the recursive function again.

What I found is the following:
Asuming we have an entity A with reference to entity B, and B has a reference to entity A.
Entity A is imported, and the reference to entity B is correct.
Entity B is imported, but the reference to entity A is empty.

I believe that what is happening is that the entity is created, but drupal is not accepting the id value as the referenced entity does not exists yet. As @Blanca.Esquerda says, executing the import a second time solves the problem, as the entity was created during the previous import.

A possible approach I am thinking about, involves creating a new Batch operation type, maybe "update_entityreference", that executes at the end, and fixes the references that have been refused by Drupal due to the entity not existing. I am still not sure how difficult it would be to implement something like that yet but i will try to figure it out.

lobodakyrylo’s picture

#5 You are a genius. I tried to avoid duplicates the same way but it didn't work so I sent duplicates array to the recursion. Your patch works for me. Gracias.

lobodakyrylo’s picture

@Blanca.Esqueda to avoid double loading maybe try to add the entity twice to the dependency list?

blanca.esqueda’s picture

@lobodacyril
The solution will require a bit more than adding the entity twice to the dependency list.
For efficiency, there should be a flag to check if an entity has already been imported and avoid re-importing it along with all its dependencies - except when the entity has a dependency and that dependency points back.
The logic should include when the entity has to be re-added to the queue and if it has been triggered more than once.

lobodakyrylo’s picture

StatusFileSize
new2.92 KB

I tried to recreate my supposition and it works. I add an entity twice to the dependency list to save it again when all the dependency entities are loaded. It works for me and I don't need to synchronize everything twice. Patch attached.
The problem is it will not work if you have references like these: A -> B, B -> C, C -> A, because I'm looking for just the next refereces: A -> B, B -> A.
I defined hasBackReference function and didn't have enough time to solve this issue and I suppose it should be also recursive.

lobodakyrylo’s picture

@Blanca.Esqueda my patch does exactly what you say but not sure that this solution is elegant and it still has a bug with a different type of reference.

guiu.rocafort.ferrer’s picture

Assigned: Unassigned » guiu.rocafort.ferrer
Status: Needs work » Needs review
StatusFileSize
new7.91 KB

I've been working on a solution to the empty references after the import. This patch develops further the idea of importing the fields involved in the dependency loop, after all the content has been imported.

It is not fully tested, and probably it can be improved further, but it is working ( at least ) in the following conditions:

* A->B, B->A ( Entity A has reference to B, Entity B has reference to A ).
* A->B->C, and C->A (
* A->A ( A has a reference to itself ).

It also has a few issues to address, for now I identify those 2:

* When updating the field reference values, the changed entity attribute gets updated too. This causes the files to be listed as modified all the time.
* If there is a dependency loop, but the entity already exists ( it is a "modify" operation ), the late field update strategy is unnecesary. ( not 100% sure about this one ).
* For now, I use $_SESSION variable to pass variables between Batch Operations. I know this is not an ideal way to to this, and some other method should be used instead.

I'm marking this as needs review, and assigning the issue to me. Can someone review the patch and give some feedback ? I am going to invest some more time on the patch, and refine it further (hopefully ) during the next 2 weeks.

fhebert’s picture

@guiu.rocafort.ferrer I understand your logic but what if there is a required reference, like media uid field referencing an user?
I've tested it won't work. You need to modify table schema at runtime before entity get saved. I'm trying a complementary patch.

mkalkbrenner’s picture

Title: Infinite loop when import for the Linked List data structure » Imports of circular entity references lead to infinite loop
Priority: Major » Critical
StatusFileSize
new3.17 KB

I don't think that a generic solution will solve all issues. You need to know how these entities handle their circular references themselves.

For example there's a circular reference between commerce_product and commerce_product_variation.
The reference from commerce_product to commerce_product_variation gets updated in a post_save hook of commerce_product_variation. Using this knowledge we could simply ignore the reference from commerce_product to commerce_product_variation.

I think the only robust solution here is to maintain a list of references to be ignored instead of writing complicated magic code.

I started it in this patch.

mkalkbrenner’s picture

I created a merge request for the patch in #13 and also merged #3114499: Drupal 9 Readiness to get it to work in Drupal 9.1.

guiu.rocafort.ferrer’s picture

Thank you for the information and the merge request @mkalkbrenner, i was not aware of commerce_product and commerce_product_variation having that behaviour. I agree with you, with the idea of the most effective way of dealing with this issue is adding a list of references to be ignored.

I will give a go to your merge request changes.

calte’s picture

I experienced this issue while working with paragraphs module. I implemented fix from https://www.drupal.org/project/content_sync/issues/3128436#comment-13617612 (https://www.drupal.org/files/issues/2020-05-15/issue-3128436-2.patch)

Relevant sections of my composer.json in case this helps anyone else.

{
    "require": {
        ...
        "drupal/content_sync": "2.x-dev"
    },
    ...
    "extra": {
        "enable-patching": true,
        "patches": {
            "drupal/content_sync": {
              "Imports of circular entity references lead to infinite loop": "https://www.drupal.org/files/issues/2020-05-15/issue-3128436-2.patch"
            }
        }
    }
}
blanca.esqueda’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
blanca.esqueda’s picture

Thank you lobodacyril, mkalkbrenner, and guiu.rocafort.ferrer for your help analyzing this issue.

I've updated the resolver, I think it considers all the cases:
* A->B, B->A ( Entity A has reference to B, Entity B has reference to A ).
* A->B->C, and C->A ( There is a circular reference in the Entity trail).
* A->A ( A has a reference to itself ).
* A<-B->C ( entity references its children and its parent - as paragraphs: https://www.drupal.org/project/content_sync/issues/3049969)

If someone has a chance to try/test it, that would be great.
This patch should be on top of 3.0.x-dev, and ideally the same version should be on target and the source sites - so they have the same normalize/denormalize data.

--
Things to consider as part of other issues:
* What to do when a dependency doesn't exist?
Entity(ies) having a missing dependency on its trail shouldn't be imported, or try to import it even whit a missing dependency - if it imports successfully good, and if not show a notice/error about that the missing dependency is a required one. what would be better to do?

* For specific cases of circular references as the one that mkalkbrenner explained of commerce_product and commerce_product_variation, I didn't try this patch with commerce entities yet so in case that there are still issues, it will be better to update a normalizer to not include as dependency something that it is not necessary during the content entity export or to remove the dependency that it is not necessary before the content entity import. Version 3.0.x-dev has some normalizers of entities and fields that I've committed recently that show how dependencies are included, and even there is a patch in this issue that shows how a parent field is included as a dependency for paragraphs https://www.drupal.org/project/content_sync/issues/3049969.

blanca.esqueda’s picture

oh, here is the patch...

blanca.esqueda’s picture

Profile module creates a dependency profile <-> user
Patch #20 resolved the dependency loop for the attached issue.

juan.silva’s picture

Status: Needs review » Reviewed & tested by the community

  • Blanca.Esqueda authored e1ef4b5 on 3.0.x
    Issue #3128436 by mkalkbrenner, lobodacyril, guiu.rocafort.ferrer,...
blanca.esqueda’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 3.0.x-dev

Thank you!

Status: Fixed » Closed (fixed)

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