Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating unmanaged proxy instances for referred-to entities during merge() #10791

Merged
merged 2 commits into from
Jun 25, 2023

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Jun 23, 2023

This PR tries to improve the situation/problem explained in #3037:

Under certain conditions – there may be multiple and not all are known/well-understood – we may get inconsistencies between the \Doctrine\ORM\UnitOfWork::$entityIdentifiers and \Doctrine\ORM\UnitOfWork::$identityMap arrays.

Since the ::$identityMap is a plain array holding object references, objects contained in it cannot be garbage-collected.
::$entityIdentifiers, however, is indexed by spl_object_id values. When those objects are destructed and/or garbage-collected, the OID may be reused and reassigned to other objects later on.

When the OID re-assignment happens to be for another entity, the UoW may assume incorrect entity states and, for example, miss INSERT or UPDATE operations.

One cause for such inconsistencies is replacing identity map entries with other object instances: This makes it possible that the old object becomes GC'd, while its OID is not cleaned up. Since that is not a use case we need to support (IMHO), #10785 is about adding a safeguard against it.

In this test shown here, the merge() operation is currently too eager in creating a proxy object for another referred-to entity. This proxy represents an entity already present in the identity map at that time, potentially leading to this problem later on.

Cross-reference #3037 (comment).

Closes #7407, closes #8994.

Not enough information in #3037 to tell whether it would be fixed.

@mpdude mpdude changed the title Avoid creating unmanaged proxy instances for referred-to entities during merge() Avoid creating unmanaged proxy instances for referred-to entities during merge() Jun 23, 2023
@mpdude mpdude force-pushed the understand-3037 branch 3 times, most recently from f96dcfa to 4a16b33 Compare June 23, 2023 18:27
@mpdude
Copy link
Contributor Author

mpdude commented Jun 23, 2023

@Ocramius Your thoughts would be very welcome

@greg0ire
Copy link
Member

Blocked by #10790

…ing merge()

This PR tries to improve the situation/problem explained in doctrine#3037:

Under certain conditions – there may be multiple and not all are known/well-understood – we may get inconsistencies between the `\Doctrine\ORM\UnitOfWork::$entityIdentifiers` and `\Doctrine\ORM\UnitOfWork::$identityMap` arrays.

Since the `::$identityMap` is a plain array holding object references, objects contained in it cannot be garbage-collected.
`::$entityIdentifiers`, however, is indexed by `spl_object_id` values. When those objects are destructed and/or garbage-collected, the OID may be reused and reassigned to other objects later on.

When the OID re-assignment happens to be for another entity, the UoW may assume incorrect entity states and, for example, miss INSERT or UPDATE operations.

One cause for such inconsistencies is _replacing_ identity map entries with other object instances: This makes it possible that the old object becomes GC'd, while its OID is not cleaned up. Since that is not a use case we need to support (IMHO), doctrine#10785 is about adding a safeguard against it.

In this test shown here, the `merge()` operation is currently too eager in creating a proxy object for another referred-to entity. This proxy represents an entity already present in the identity map at that time, potentially leading to this problem later on.
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
@greg0ire greg0ire added this to the 2.15.4 milestone Jun 25, 2023
@greg0ire greg0ire added the Bug label Jun 25, 2023
@greg0ire greg0ire merged commit b17e52b into doctrine:2.15.x Jun 25, 2023
@greg0ire
Copy link
Member

Thanks @mpdude !

@mpdude mpdude deleted the understand-3037 branch June 28, 2023 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants