-
Notifications
You must be signed in to change notification settings - Fork 934
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5e5926
GH1594: AsyncLocal leak in SystemTransactionContext
pecanw 8721f5a
AsyncLocal leak in SystemTransactionContext review fixes
pecanw 093d598
AsyncLocal leak in SystemTransactionContext test adjustment for .NET …
pecanw d47cdfc
Merge branch 'master' into GH1594
pecanw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/NHibernate.Test/NHSpecificTest/GH1594/ExecutionContextExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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
10
src/NHibernate.Test/NHSpecificTest/GH1594/Mappings.hbm.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
(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.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks for describing.