Skip to content

Commit 4772448

Browse files
fredericDelaportehazzik
authored andcommitted
NH-4076 - Prevent usage of garbage collected session from DefaultQueryProvider
1 parent 55ca5d6 commit 4772448

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/NHibernate.Test/Linq/ExpressionSessionLeakTest.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Reflection;
34
using NHibernate.DomainModel.Northwind.Entities;
45
using NHibernate.Linq;
56
using NUnit.Framework;
@@ -20,10 +21,12 @@ public void SessionGetsCollected()
2021

2122
private WeakReference DoLinqInSeparateSession()
2223
{
24+
// Using SessionFactory of another session dodge TestCase session tracking, which is needed for having session garbage collected
25+
// Otherwise DebugSessionFactory session tracking should be changed to use WeakReference too.
2326
using (var leakTest = session.SessionFactory.OpenSession())
2427
{
2528
// It appears linq expressions will (or might) contain a reference to the
26-
// IQueryable. At time of writing, linq expressions are helt within NhLinqExpression,
29+
// IQueryable. At time of writing, linq expressions are held within NhLinqExpression,
2730
// which in turn will be held in the query plan cache. Since the IQueryable will
2831
// be an NhQueryable, which holds a reference to the SessionImpl instance,
2932
// we will be leaking session instances.
@@ -33,5 +36,71 @@ private WeakReference DoLinqInSeparateSession()
3336
return new WeakReference(leakTest, false);
3437
}
3538
}
39+
40+
static readonly PropertyInfo SessionProperty = typeof(DefaultQueryProvider).GetProperty(
41+
"Session",
42+
BindingFlags.NonPublic | BindingFlags.Instance);
43+
44+
[Theory]
45+
public void SessionIsNotNullOrResurrected(bool? disposeSession)
46+
{
47+
// Must do in a separated method, local variables seem not collected otherwise.
48+
var provider = GetProviderFromSeparateSession(disposeSession);
49+
50+
if (provider == null)
51+
Assert.Ignore("Another query provider than NHibernate default one is used");
52+
Assert.That(SessionProperty, Is.Not.Null, $"Session property on {nameof(DefaultQueryProvider)} is not found.");
53+
54+
// Force collection of no more strongly referenced objects.
55+
// Do not wait for pending finalizers
56+
GC.Collect();
57+
58+
try
59+
{
60+
var s = SessionProperty.GetValue(provider);
61+
Assert.Fail($"Getting provider Session property did not failed. Obtained {(s == null ? "null" : s.GetType().Name)}.");
62+
}
63+
catch (TargetInvocationException tie)
64+
{
65+
Assert.That(tie.InnerException, Is.TypeOf<InvalidOperationException>().And.Message.Contains("garbage coll"));
66+
}
67+
}
68+
69+
[Theory]
70+
public void QueryFailsProperlyOnDereferencedSession(bool? disposeSession)
71+
{
72+
// Must do in a separated method, local variables seem not collected otherwise.
73+
var query = GetQueryFromSeparateSession(disposeSession);
74+
75+
// Force collection of no more strongly referenced objects.
76+
// Do not wait for pending finalizers
77+
GC.Collect();
78+
79+
Assert.That(() => query.FirstOrDefault(), Throws.InvalidOperationException.And.Message.Contains("garbage coll"));
80+
}
81+
82+
IQueryable<Customer> GetQueryFromSeparateSession(bool? disposeSession)
83+
{
84+
// Using SessionFactory of another session dodge DebugSessionFactory session tracking, which is needed for having session garbage collected.
85+
// Otherwise DebugSessionFactory session tracking should be changed to use WeakReference too.
86+
var s = session.SessionFactory.OpenSession();
87+
try
88+
{
89+
return s.Query<Customer>();
90+
}
91+
finally
92+
{
93+
if (disposeSession == true)
94+
s.Dispose();
95+
else if (disposeSession == false)
96+
s.Close();
97+
}
98+
}
99+
100+
DefaultQueryProvider GetProviderFromSeparateSession(bool? disposeSession)
101+
{
102+
var queryable = GetQueryFromSeparateSession(disposeSession);
103+
return queryable.Provider as DefaultQueryProvider;
104+
}
36105
}
37106
}

src/NHibernate/Linq/DefaultQueryProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ public class DefaultQueryProvider : INhQueryProvider
2323
{
2424
private static readonly MethodInfo CreateQueryMethodDefinition = ReflectHelper.GetMethodDefinition((INhQueryProvider p) => p.CreateQuery<object>(null));
2525

26-
private readonly WeakReference _session;
26+
private readonly WeakReference<ISessionImplementor> _session;
2727

2828
public DefaultQueryProvider(ISessionImplementor session)
2929
{
30-
_session = new WeakReference(session, true);
30+
// Short reference (no trackResurrection). If the session gets garbage collected, it will be in an unpredictable state:
31+
// better throw rather than resurrecting it.
32+
// https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/weak-references
33+
_session = new WeakReference<ISessionImplementor>(session);
3134
}
3235

3336
protected virtual ISessionImplementor Session
3437
{
35-
get { return _session.Target as ISessionImplementor; }
38+
get
39+
{
40+
if (!_session.TryGetTarget(out var target))
41+
throw new InvalidOperationException("Session has already been garbage collected");
42+
return target;
43+
}
3644
}
3745

3846
public virtual object Execute(Expression expression)

0 commit comments

Comments
 (0)