Skip to content

Fix AsyncLocal leak in SystemTransactionContext #1596

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 4 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1594/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1594
{
class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections;
using System.Threading;

namespace NHibernate.Test.NHSpecificTest.GH1594
{
public static class ExecutionContextExtensions
{
public static int LocalValuesCount(this ExecutionContext c)
{
#if NETFX
const string localValuesFieldName = "_localValues";
#else
const string localValuesFieldName = "m_localValues";
#endif
var f = typeof(ExecutionContext).GetField(localValuesFieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var d = (IDictionary) f.GetValue(c);
return d?.Count ?? 0;
}
}
}
72 changes: 72 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1594/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Linq;
using System.Threading;
using System.Transactions;
using NHibernate.Engine;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1594
{
[TestFixture]
public class Fixture : BugTestCase
Copy link
Member

Choose a reason for hiding this comment

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

Since it uses system transaction, the fixture must be disabled if the environment does not support them. Add:

protected override bool AppliesTo(ISessionFactoryImplementor factory) =>
	factory.ConnectionProvider.Driver.SupportsSystemTransactions;

(Since currently this is false for all .Net Core / .Standard drivers, the private field fix is in fact not really needed, but it does not harm to have it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks for describing.

{
protected override bool AppliesTo(ISessionFactoryImplementor factory) =>
factory.ConnectionProvider.Driver.SupportsSystemTransactions;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new GH1594.Entity {Name = "Bob"};
session.Save(e1);

var e2 = new GH1594.Entity {Name = "Sally"};
session.Save(e2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void ExecutionContextLocalValuesLeak()
{
using (var session = OpenSession())
{
RunInTransaction(session);
var localValuesCountAfterFirstCall = ExecutionContext.Capture().LocalValuesCount();
RunInTransaction(session);
var localValuesCountAfterSecondCall = ExecutionContext.Capture().LocalValuesCount();
Assert.AreEqual(localValuesCountAfterFirstCall, localValuesCountAfterSecondCall);
}
}

private void RunInTransaction(ISession session)
{
using (var ts = new TransactionScope())
{
var result = from e in session.Query<GH1594.Entity>()
where e.Name == "Bob"
select e;

result.ToList();
ts.Complete();
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1594/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1594">

<class name="Entity">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public class SystemTransactionContext : ITransactionContext, IEnlistmentNotifica
private readonly ManualResetEventSlim _lock = new ManualResetEventSlim(true);
private volatile bool _needCompletionLocking = true;
// Required for not locking the completion phase itself when locking session usages from concurrent threads.
private readonly AsyncLocal<bool> _bypassLock = new AsyncLocal<bool>();
private static readonly AsyncLocal<bool> _bypassLock = new AsyncLocal<bool>();
private readonly int _systemTransactionCompletionLockTimeout;

/// <summary>
Expand Down Expand Up @@ -269,6 +269,7 @@ protected virtual void Lock()
protected virtual void Unlock()
{
_lock.Set();
_bypassLock.Value = false;
}

/// <summary>
Expand Down