Skip to content

Object.Finalize should not be proxiable #3205

@ssharunas

Description

@ssharunas

Currently in DefaultDynamicProxyMethodCheckerExtensions methods IsProxiable and ShouldBeProxiable are treating overridden Finalize methods as proxiable:

...
&& (method.DeclaringType != typeof(object) || !"finalize".Equals(method.Name, StringComparison.OrdinalIgnoreCase))
...

Both of these methods are used for lazy loading.
If during finalization object is not yet loaded, lazy proxy attempts to pull it from DB.
Since Finalize is called by GC at random times and random threads, it is unlikely that session will still be open during Finalize.
In most cases Finalize for lazy not yet loaded proxies throws an exception "Could not initialize proxy - no Session".

This bug occurs only if proxyable class has a base class, that is overriding Finalize, ie:

class Base { ~Base() { } }
class LazyChild : Base { } // this class will not be finalized

I've created a small demonstration project: https://github.com/ssharunas/nhibernate-finalization-bug-demonstration

IMHO Finalize should not be proxyable even in delivered classes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions