Skip to content

Fix key type mismatch in EntityUniqueKey #1647

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

Merged
merged 3 commits into from
Apr 10, 2018

Conversation

fredericDelaporte
Copy link
Member

@fredericDelaporte fredericDelaporte commented Apr 9, 2018

Fixes #1645.

@fredericDelaporte fredericDelaporte added this to the 5.1.1 milestone Apr 9, 2018
Fix key type mismatch in EntityUniqueKey
EntityUniqueKey was assuming the value to be always semi-resolved (to be
the identifier instead of the entity for an entity type). It was always
computing the value hashcode with the semi-resolved type, which is the
identifier type for an entity.
But this has changed with nhibernate#1492, which caches unique keys also for
already fully loaded entities. The value hashcode must then be computed
with the entity type, which accounts for proxies.
Otherwise the proxy loading may get triggered while the code is already
loading the proxy, which results in a stack overflow.

Add test case adapted from Michael Estermann repro

Fix nhibernate#1645

Co-authored-by: Michael Estermann <[email protected]>
@fredericDelaporte fredericDelaporte changed the title [WIP] One-to-one with property-ref stack overflow Fix key type mismatch in EntityUniqueKey Apr 9, 2018
@@ -34,7 +34,7 @@ public EntityUniqueKey(string entityName, string uniqueKeyName, object semiResol
this.entityName = entityName;
this.uniqueKeyName = uniqueKeyName;
key = semiResolvedKey;
this.keyType = keyType.GetSemiResolvedType(factory);
this.keyType = keyType;
Copy link
Member Author

@fredericDelaporte fredericDelaporte Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EntityUniqueKey was assuming the value to be always semi-resolved (to be the identifier instead of the entity for an entity type). It was always computing the value hashcode with the semi-resolved type, which is the identifier type for an entity.
But this has changed with #1492, which caches unique keys also for already fully loaded entities. The value hashcode must then be computed with the entity type, which accounts for proxies.
Otherwise the proxy loading may get triggered while the code is already loading the proxy, which results in a stack overflow.

The type to use is now fully computed before constructing EntityUniqueKey.
It is constructed in only two places: in the loader, see its changes; in the EntityType.LoadByUniqueKey, which uses GetIdentifierOrUniqueKeyType which yields types having themselves as semi-resolved types. (For being extra-safe, a call to GetSemiResolvedType could still be added in LoadByUniqueKey just in case.)

Ideally semiResolvedKey parameter should be renamed simply key (no more assumptions). But renaming a public method parameter is a gray area possible breaking change (if anyone has written a call explicitly naming parameters), and I avoid doing this one in patch releases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For being extra-safe, a call to GetSemiResolvedType could still be added in LoadByUniqueKey just in case.

Let's do this.

Ideally semiResolvedKey parameter should be renamed simply key (no more assumptions).

Please add a TODO for 6.0

// But since removing this call from EntityUniqueKey is done for a patch and that the code path here has
// no known bugs with this GetSemiResolvedType, moving its call here for avoiding altering this code
// path. See GH1645.
.GetSemiResolvedType(factory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the implementation, this actually could happen only if the primary or unique key is entity itself. Is it event possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that is the case in #1645, the unique key is an entity. (Property-ref cases.)

@fredericDelaporte fredericDelaporte merged commit 381f1dd into nhibernate:master Apr 10, 2018
@fredericDelaporte fredericDelaporte deleted the GH1645 branch April 10, 2018 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants