Skip to content

Remove session finalizer #2811

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 2 commits into from
Jun 7, 2021
Merged

Remove session finalizer #2811

merged 2 commits into from
Jun 7, 2021

Conversation

bahusoid
Copy link
Member

@bahusoid bahusoid commented Jun 5, 2021

Turns out exception is possible from session Finalizer (regression made by #1984):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at NHibernate.Impl.AbstractSessionImpl.BeginContext() in /home/runner/work/nhibernate-core/nhibernate-core/src/NHibernate/Impl/AbstractSessionImpl.cs:line 419
   at NHibernate.Impl.SessionImpl.Dispose(Boolean isDisposing) in /home/runner/work/nhibernate-core/nhibernate-core/src/NHibernate/Impl/SessionImpl.cs:line 1531
   at NHibernate.Impl.SessionImpl.Finalize() in /home/runner/work/nhibernate-core/nhibernate-core/src/NHibernate/Impl/SessionImpl.cs:line 1491

Suggested fix: Let's simply drop finalizers. They shouldn't be created in a first place - we have no unmanaged resources inside session. So we add additional pressure on finalizer thread for nothing

@bahusoid bahusoid added this to the 5.3.9 milestone Jun 5, 2021
Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

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

I would either get rid of Dispose(bool isDisposing) in this PR too, or only remove the using (BeginContext()) we have in Dispose(bool isDisposing), which is undue. (Dispose() already handles it, and there is no reason to handle the context when finalizing, instead it must not be handled during finalization.)

With this current PR state, we end up with a partial dispose pattern implementation, which can be misleading for future changes. It looks like it handles finalization since it still has that disposing flag, but it does not.

I am for removing the finalizer and the isDisposing overload, at least for next minor. In this release branch, just removing the undue BeginContext will be enough, if not wiling to also remove the isDisposing overload.

@fredericDelaporte

This comment has been minimized.

@bahusoid
Copy link
Member Author

bahusoid commented Jun 6, 2021

we end up with a partial dispose pattern implementation, which can be misleading for future changes

I find confusing that this pattern is implemented but no unmanaged resources are handled by it. So I've added explicit comment about it. So IMHO no reasons to keep finalizers just to implement properly not needed pattern.

I would either get rid of Dispose(bool isDisposing) in this PR to

Yeah I didn't remove parameter to minimize changes as it's called from 2 places (from Dispose and CloseSessionFromSystemTransaction) and it's not straightforward change. Is it actually correct that CloseSessionFromSystemTransaction doesn't call full Dispose() method? It looks fishy to me...

it would be safer to just avoid calling BeginContext while finalizing.

Logger also can cause troubles - some loggers might be already disposed. Really I see zero reasons to keep finalizers. Let's drop them.

@fredericDelaporte
Copy link
Member

Is it actually correct that CloseSessionFromSystemTransaction doesn't call full Dispose() method? It looks fishy to me...

Dispose has already been called when CloseSessionFromSystemTransaction is called... All this is really fishy, but that is the legacy "flush on system transaction completion" feature which requires that.
Here is the whole pattern:

  • Open a transaction scope.
  • Open a session
  • Dispose the session. Actually the session disposal is delayed, the transaction context is flagged for disposing the session later.
  • Complete the transaction scope. The 2PC will then handle the session actual disposal by calling CloseSessionFromSystemTransaction.

@fredericDelaporte

This comment has been minimized.

@fredericDelaporte
Copy link
Member

Ok, forget it, I have re-read the thing, you are right.

@hazzik hazzik changed the title Get rid of session finalizers Remove session finializer Jun 7, 2021
@hazzik hazzik merged commit 4fa82e8 into nhibernate:5.3.x Jun 7, 2021
@hazzik hazzik added the r: Fixed label Jun 7, 2021
@fredericDelaporte fredericDelaporte changed the title Remove session finializer Remove session finalizer Jun 26, 2021
bahusoid added a commit to bahusoid/nhibernate-core that referenced this pull request Aug 10, 2021
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.

3 participants