- Notifications
You must be signed in to change notification settings - Fork752
PyObject finalizer#692
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
29 commits Select commitHold shift + click to select a range
8083f3b
Finalizer for PyObject
amos402af33e74
Avoid test interdependency
amos4026d9f897
Add source to .csproj
amos4027140fd0
Make sure recover the environment
amos402f66697d
Add StackTrace of C# exception
amos402799d37e
Clean up the test and interface
amos402cb55163
Update CHANGELOG.md
amos402bfc0392
Mono doesn't have GC.WaitForFullGCComplete
amos40259b614d
Fixed PythonException leak
amos402569cd94
Merge branch 'master' into pyobject-finalizer
den-run-aif4f5032
Fixed nPython.exe crash on Shutdown
amos4020967a12
Merge branch 'master' into pyobject-finalizer
filmorf071c55
Add error handler
amos402cfda491
Merge branch 'master' into pyobject-finalizer
amos402f6c6e42
Merge branch 'master' into pyobject-finalizer
filmor0d96641
Make collect callback without JIT
amos40234713f7
Merge branch 'master' into pyobject-finalizer
filmorb4e30ac
Merge branch 'master' into pyobject-finalizer
filmorf836ffa
Merge remote-tracking branch 'remotes/upstream/master' into pyobject-…
amos402a4bb82d
Add pending marker
amos40221da86d
Merge branch 'master' into pyobject-finalizer
amos4025254c65
Remove PYTHONMALLOC setting
amos402916e85e
Merge remote-tracking branch 'remotes/upstream/master' into pyobject-…
amos402eee3683
Fix ref count error
amos402cee8e17
Add ref count check for helping discover the bugs of decref too much
amos402247e2d9
Fix ref count error
amos40290c67ca
typo error
amos4026d68d70
Merge branch 'master' into pyobject-finalizer
filmor4eff81e
Merge branch 'master' into pyobject-finalizer
filmorFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsCHANGELOG.md
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
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
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
239 changes: 239 additions & 0 deletionssrc/embed_tests/TestFinalizer.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,239 @@ | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
namespace Python.EmbeddingTest | ||
{ | ||
public class TestFinalizer | ||
{ | ||
private int _oldThreshold; | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_oldThreshold = Finalizer.Instance.Threshold; | ||
PythonEngine.Initialize(); | ||
Exceptions.Clear(); | ||
} | ||
[TearDown] | ||
public void TearDown() | ||
{ | ||
Finalizer.Instance.Threshold = _oldThreshold; | ||
PythonEngine.Shutdown(); | ||
} | ||
private static void FullGCCollect() | ||
{ | ||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
[Test] | ||
public void CollectBasicObject() | ||
{ | ||
Assert.IsTrue(Finalizer.Instance.Enable); | ||
int thId = Thread.CurrentThread.ManagedThreadId; | ||
Finalizer.Instance.Threshold = 1; | ||
bool called = false; | ||
EventHandler<Finalizer.CollectArgs> handler = (s, e) => | ||
{ | ||
Assert.AreEqual(thId, Thread.CurrentThread.ManagedThreadId); | ||
Assert.GreaterOrEqual(e.ObjectCount, 1); | ||
called = true; | ||
}; | ||
WeakReference shortWeak; | ||
WeakReference longWeak; | ||
{ | ||
MakeAGarbage(out shortWeak, out longWeak); | ||
} | ||
FullGCCollect(); | ||
// The object has been resurrected | ||
Assert.IsFalse(shortWeak.IsAlive); | ||
Assert.IsTrue(longWeak.IsAlive); | ||
{ | ||
var garbage = Finalizer.Instance.GetCollectedObjects(); | ||
Assert.NotZero(garbage.Count); | ||
Assert.IsTrue(garbage.Any(T => ReferenceEquals(T.Target, longWeak.Target))); | ||
} | ||
Assert.IsFalse(called); | ||
Finalizer.Instance.CollectOnce += handler; | ||
try | ||
{ | ||
Finalizer.Instance.CallPendingFinalizers(); | ||
} | ||
finally | ||
{ | ||
Finalizer.Instance.CollectOnce -= handler; | ||
} | ||
Assert.IsTrue(called); | ||
} | ||
private static void MakeAGarbage(out WeakReference shortWeak, out WeakReference longWeak) | ||
{ | ||
PyLong obj = new PyLong(1024); | ||
shortWeak = new WeakReference(obj); | ||
longWeak = new WeakReference(obj, true); | ||
obj = null; | ||
} | ||
private static long CompareWithFinalizerOn(PyObject pyCollect, bool enbale) | ||
{ | ||
// Must larger than 512 bytes make sure Python use | ||
string str = new string('1', 1024); | ||
Finalizer.Instance.Enable = true; | ||
FullGCCollect(); | ||
FullGCCollect(); | ||
pyCollect.Invoke(); | ||
Finalizer.Instance.Collect(); | ||
Finalizer.Instance.Enable = enbale; | ||
// Estimate unmanaged memory size | ||
long before = Environment.WorkingSet - GC.GetTotalMemory(true); | ||
for (int i = 0; i < 10000; i++) | ||
{ | ||
// Memory will leak when disable Finalizer | ||
new PyString(str); | ||
} | ||
FullGCCollect(); | ||
FullGCCollect(); | ||
pyCollect.Invoke(); | ||
if (enbale) | ||
{ | ||
Finalizer.Instance.Collect(); | ||
} | ||
FullGCCollect(); | ||
FullGCCollect(); | ||
long after = Environment.WorkingSet - GC.GetTotalMemory(true); | ||
return after - before; | ||
} | ||
/// <summary> | ||
/// Because of two vms both have their memory manager, | ||
/// this test only prove the finalizer has take effect. | ||
/// </summary> | ||
[Test] | ||
[Ignore("Too many uncertainties, only manual on when debugging")] | ||
public void SimpleTestMemory() | ||
{ | ||
bool oldState = Finalizer.Instance.Enable; | ||
try | ||
{ | ||
using (PyObject gcModule = PythonEngine.ImportModule("gc")) | ||
using (PyObject pyCollect = gcModule.GetAttr("collect")) | ||
{ | ||
long span1 = CompareWithFinalizerOn(pyCollect, false); | ||
long span2 = CompareWithFinalizerOn(pyCollect, true); | ||
Assert.Less(span2, span1); | ||
} | ||
} | ||
finally | ||
{ | ||
Finalizer.Instance.Enable = oldState; | ||
} | ||
} | ||
class MyPyObject : PyObject | ||
{ | ||
public MyPyObject(IntPtr op) : base(op) | ||
{ | ||
} | ||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
GC.SuppressFinalize(this); | ||
throw new Exception("MyPyObject"); | ||
} | ||
internal static void CreateMyPyObject(IntPtr op) | ||
{ | ||
Runtime.Runtime.XIncref(op); | ||
new MyPyObject(op); | ||
} | ||
} | ||
[Test] | ||
public void ErrorHandling() | ||
{ | ||
bool called = false; | ||
EventHandler<Finalizer.ErrorArgs> handleFunc = (sender, args) => | ||
{ | ||
called = true; | ||
Assert.AreEqual(args.Error.Message, "MyPyObject"); | ||
}; | ||
Finalizer.Instance.Threshold = 1; | ||
Finalizer.Instance.ErrorHandler += handleFunc; | ||
try | ||
{ | ||
WeakReference shortWeak; | ||
WeakReference longWeak; | ||
{ | ||
MakeAGarbage(out shortWeak, out longWeak); | ||
var obj = (PyLong)longWeak.Target; | ||
IntPtr handle = obj.Handle; | ||
shortWeak = null; | ||
longWeak = null; | ||
MyPyObject.CreateMyPyObject(handle); | ||
obj.Dispose(); | ||
obj = null; | ||
} | ||
FullGCCollect(); | ||
Finalizer.Instance.Collect(); | ||
Assert.IsTrue(called); | ||
} | ||
finally | ||
{ | ||
Finalizer.Instance.ErrorHandler -= handleFunc; | ||
} | ||
} | ||
[Test] | ||
public void ValidateRefCount() | ||
{ | ||
if (!Finalizer.Instance.RefCountValidationEnabled) | ||
{ | ||
Assert.Pass("Only run with FINALIZER_CHECK"); | ||
} | ||
IntPtr ptr = IntPtr.Zero; | ||
bool called = false; | ||
Finalizer.IncorrectRefCntHandler handler = (s, e) => | ||
{ | ||
called = true; | ||
Assert.AreEqual(ptr, e.Handle); | ||
Assert.AreEqual(2, e.ImpactedObjects.Count); | ||
// Fix for this test, don't do this on general environment | ||
Runtime.Runtime.XIncref(e.Handle); | ||
return false; | ||
}; | ||
Finalizer.Instance.IncorrectRefCntResolver += handler; | ||
try | ||
{ | ||
ptr = CreateStringGarbage(); | ||
FullGCCollect(); | ||
Assert.Throws<Finalizer.IncorrectRefCountException>(() => Finalizer.Instance.Collect()); | ||
Assert.IsTrue(called); | ||
} | ||
finally | ||
{ | ||
Finalizer.Instance.IncorrectRefCntResolver -= handler; | ||
} | ||
} | ||
private static IntPtr CreateStringGarbage() | ||
{ | ||
PyString s1 = new PyString("test_string"); | ||
// s2 steal a reference from s1 | ||
PyString s2 = new PyString(s1.Handle); | ||
return s1.Handle; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletionssrc/embed_tests/TestPyAnsiString.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
1 change: 1 addition & 0 deletionssrc/embed_tests/TestPyFloat.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
2 changes: 2 additions & 0 deletionssrc/embed_tests/TestPyInt.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
2 changes: 2 additions & 0 deletionssrc/embed_tests/TestPyLong.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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.