| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6c1abfa commit 80ff754
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,5 +64,67 @@ public void TestClrObjectFullRelease() | |||
| 64 | 64 | ||
| 65 | 65 | Assert.IsFalse(weakRef.IsAlive, "Clr object should be collected."); | |
| 66 | 66 | } | |
| 67 | + | ||
| 68 | + [Test] | ||
| 69 | + [Ignore("For debug only")] | ||
| 70 | + public void TestExceptionMemoryLeak() | ||
| 71 | + { | ||
| 72 | + dynamic pymodule; | ||
| 73 | + PyObject gc; | ||
| 74 | + dynamic testmethod; | ||
| 75 | + | ||
| 76 | + var ts = PythonEngine.BeginAllowThreads(); | ||
| 77 | + IntPtr pylock = PythonEngine.AcquireLock(); | ||
| 78 | + | ||
| 79 | + #if NETCOREAPP | ||
| 80 | + const string s = "../../fixtures"; | ||
| 81 | + #else | ||
| 82 | + const string s = "../fixtures"; | ||
| 83 | + #endif | ||
| 84 | + string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s); | ||
| 85 | + | ||
| 86 | + IntPtr str = Runtime.Runtime.PyString_FromString(testPath); | ||
| 87 | + IntPtr path = Runtime.Runtime.PySys_GetObject("path"); | ||
| 88 | + Runtime.Runtime.PyList_Append(path, str); | ||
| 89 | + | ||
| 90 | + { | ||
| 91 | + PyObject sys = PythonEngine.ImportModule("sys"); | ||
| 92 | + gc = PythonEngine.ImportModule("gc"); | ||
| 93 | + | ||
| 94 | + pymodule = PythonEngine.ImportModule("MemoryLeakTest.pyraise"); | ||
| 95 | + testmethod = pymodule.test_raise_exception; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + PythonEngine.ReleaseLock(pylock); | ||
| 99 | + | ||
| 100 | + double floatarg1 = 5.1f; | ||
| 101 | + dynamic res = null; | ||
| 102 | + { | ||
| 103 | + for (int i = 1; i <= 10000000; i++) | ||
| 104 | + { | ||
| 105 | + using (Py.GIL()) | ||
| 106 | + { | ||
| 107 | + try | ||
| 108 | + { | ||
| 109 | + res = testmethod(Py.kw("number", floatarg1), Py.kw("astring", "bbc")); | ||
| 110 | + } | ||
| 111 | + catch (Exception e) | ||
| 112 | + { | ||
| 113 | + if (i % 10000 == 0) | ||
| 114 | + { | ||
| 115 | + TestContext.Progress.WriteLine(e.Message); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + if (i % 10000 == 0) | ||
| 121 | + { | ||
| 122 | + GC.Collect(); | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + PythonEngine.EndAllowThreads(ts); | ||
| 128 | + } | ||
| 67 | 129 | } | |
| 68 | 130 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + def test_raise_exception(number=3, astring='abc'): | ||
| 2 | + raise ValueError("testing for memory leak") | ||
| 3 | + return astring * int(number) | ||
| 4 | + | ||
| 5 | + def test_raise_exception2(number, astring): | ||
| 6 | + #raise ValueError("testing for memory leak") | ||
| 7 | + #astring * int(number) | ||
| 8 | + return "test" | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,10 +116,10 @@ internal static void debug(string msg) | |||
| 116 | 116 | Console.WriteLine(" {0}", msg); | |
| 117 | 117 | } | |
| 118 | 118 | ||
| 119 | - /// <summary> | ||
| 119 | + /// <summary> | ||
| 120 | 120 | /// Helper function to inspect/compare managed to native conversions. | |
| 121 | - /// Especially useful when debugging CustomMarshaler. | ||
| 122 | - /// </summary> | ||
| 121 | + /// Especially useful when debugging CustomMarshaler. | ||
| 122 | + /// </summary> | ||
| 123 | 123 | /// <param name="bytes"></param> | |
| 124 | 124 | [Conditional("DEBUG")] | |
| 125 | 125 | public static void PrintHexBytes(byte[] bytes) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,14 +40,20 @@ public PyIter(PyObject iterable) | |||
| 40 | 40 | } | |
| 41 | 41 | } | |
| 42 | 42 | ||
| 43 | - protected override void Dispose(bool disposing) | ||
| 43 | + protected internal override void Dispose(bool disposing) | ||
| 44 | 44 | { | |
| 45 | - if (null != _current) | ||
| 45 | + try | ||
| 46 | 46 | { | |
| 47 | - _current.Dispose(); | ||
| 48 | - _current = null; | ||
| 47 | + if (null != _current) | ||
| 48 | + { | ||
| 49 | + _current.Dispose(disposing); | ||
| 50 | + _current = null; | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + finally | ||
| 54 | + { | ||
| 55 | + base.Dispose(disposing); | ||
| 49 | 56 | } | |
| 50 | - base.Dispose(disposing); | ||
| 51 | 57 | } | |
| 52 | 58 | ||
| 53 | 59 | public bool MoveNext() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,39 +132,32 @@ public T As<T>() | |||
| 132 | 132 | /// to Python objects will not be released until a managed garbage | |
| 133 | 133 | /// collection occurs. | |
| 134 | 134 | /// </remarks> | |
| 135 | - protected virtual void Dispose(bool disposing) | ||
| 135 | + protected internal virtual void Dispose(bool disposing) | ||
| 136 | 136 | { | |
| 137 | 137 | if (!disposed) | |
| 138 | 138 | { | |
| 139 | 139 | disposed = true; | |
| 140 | + IntPtr objToDispose = obj; | ||
| 141 | + obj = IntPtr.Zero; | ||
| 140 | 142 | ||
| 141 | 143 | if (disposing) | |
| 142 | 144 | { | |
| 143 | - try | ||
| 145 | + if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing) | ||
| 144 | 146 | { | |
| 145 | - if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing) | ||
| 147 | + IntPtr gs = PythonEngine.AcquireLock(); | ||
| 148 | + try | ||
| 146 | 149 | { | |
| 147 | - IntPtr gs = PythonEngine.AcquireLock(); | ||
| 148 | - try | ||
| 149 | - { | ||
| 150 | - Runtime.XDecref(obj); | ||
| 151 | - obj = IntPtr.Zero; | ||
| 152 | - } | ||
| 153 | - finally | ||
| 154 | - { | ||
| 155 | - PythonEngine.ReleaseLock(gs); | ||
| 156 | - } | ||
| 150 | + Runtime.XDecref(objToDispose); | ||
| 151 | + } | ||
| 152 | + finally | ||
| 153 | + { | ||
| 154 | + PythonEngine.ReleaseLock(gs); | ||
| 157 | 155 | } | |
| 158 | - } | ||
| 159 | - catch | ||
| 160 | - { | ||
| 161 | - // Do nothing. | ||
| 162 | 156 | } | |
| 163 | 157 | } | |
| 164 | 158 | else | |
| 165 | 159 | { | |
| 166 | - _referenceDecrementer?.ScheduleDecRef(obj); | ||
| 167 | - obj = IntPtr.Zero; | ||
| 160 | + _referenceDecrementer?.ScheduleDecRef(objToDispose); | ||
| 168 | 161 | } | |
| 169 | 162 | } | |
| 170 | 163 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,14 @@ | |||
| 7 | 7 | ||
| 8 | 8 | namespace Python.Runtime | |
| 9 | 9 | { | |
| 10 | + /// <summary> | ||
| 11 | + /// Background python objects reference decrementer. | ||
| 12 | + /// !!! By some unknown reasons python references decrements have been performed in the GC thread - affects GC. !!! | ||
| 13 | + /// This component contains background thread with the queue of IntPtr python object references. | ||
| 14 | + /// Finalizers should schedule reference decrement operation in this component to avoid problem with GC. | ||
| 15 | + /// Each PythonEngine Init/Shutdown have it's own PyReferenceDecrementer, so PyObject should also have a field with the | ||
| 16 | + /// reference to the correct PyReferenceDecrementer. | ||
| 17 | + /// </summary> | ||
| 10 | 18 | internal class PyReferenceDecrementer : IDisposable | |
| 11 | 19 | { | |
| 12 | 20 | private static readonly DedicatedThreadTaskScheduler DedicatedThreadTaskScheduler = new DedicatedThreadTaskScheduler(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,7 +31,7 @@ public class PyScope : DynamicObject, IDisposable | |||
| 31 | 31 | /// <summary> | |
| 32 | 32 | /// the python Module object the scope associated with. | |
| 33 | 33 | /// </summary> | |
| 34 | - internal readonly IntPtr obj; | ||
| 34 | + internal IntPtr obj; | ||
| 35 | 35 | ||
| 36 | 36 | /// <summary> | |
| 37 | 37 | /// the variable dict of the scope. | |
@@ -522,34 +522,37 @@ protected virtual void Dispose(bool disposing) | |||
| 522 | 522 | { | |
| 523 | 523 | if (!_isDisposed) | |
| 524 | 524 | { | |
| 525 | - _isDisposed = true; | ||
| 526 | - | ||
| 525 | + IntPtr objToDispose = obj; | ||
| 527 | 526 | if (disposing) | |
| 528 | 527 | { | |
| 529 | 528 | try | |
| 530 | 529 | { | |
| 531 | - if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing) | ||
| 532 | - { | ||
| 533 | - IntPtr gs = PythonEngine.AcquireLock(); | ||
| 534 | - try | ||
| 535 | - { | ||
| 536 | - Runtime.XDecref(obj); | ||
| 537 | - } | ||
| 538 | - finally | ||
| 539 | - { | ||
| 540 | - PythonEngine.ReleaseLock(gs); | ||
| 541 | - } | ||
| 542 | - } | ||
| 530 | + OnDispose?.Invoke(this); | ||
| 531 | + } | ||
| 532 | + finally | ||
| 533 | + { | ||
| 534 | + _isDisposed = true; | ||
| 535 | + obj = IntPtr.Zero; | ||
| 543 | 536 | } | |
| 544 | - catch | ||
| 537 | + | ||
| 538 | + if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing) | ||
| 545 | 539 | { | |
| 546 | - // Do nothing. | ||
| 540 | + IntPtr gs = PythonEngine.AcquireLock(); | ||
| 541 | + try | ||
| 542 | + { | ||
| 543 | + Runtime.XDecref(objToDispose); | ||
| 544 | + } | ||
| 545 | + finally | ||
| 546 | + { | ||
| 547 | + PythonEngine.ReleaseLock(gs); | ||
| 548 | + } | ||
| 547 | 549 | } | |
| 548 | - OnDispose?.Invoke(this); | ||
| 549 | 550 | } | |
| 550 | 551 | else | |
| 551 | 552 | { | |
| 552 | - _referenceDecrementer?.ScheduleDecRef(obj); | ||
| 553 | + _isDisposed = true; | ||
| 554 | + obj = IntPtr.Zero; | ||
| 555 | + _referenceDecrementer?.ScheduleDecRef(objToDispose); | ||
| 553 | 556 | } | |
| 554 | 557 | } | |
| 555 | 558 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -598,7 +598,7 @@ internal GILState() | |||
| 598 | 598 | ||
| 599 | 599 | protected virtual void Dispose(bool disposing) | |
| 600 | 600 | { | |
| 601 | - //ReleeaseLock is thread bound and if it's called in finalizer thread it can wrongly release lock. | ||
| 601 | + //ReleaseLock is thread bound and if it's called in finalizer thread it can wrongly release lock. | ||
| 602 | 602 | if (disposing) | |
| 603 | 603 | { | |
| 604 | 604 | PythonEngine.ReleaseLock(state); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,9 +21,11 @@ public PythonException() | |||
| 21 | 21 | _referenceDecrementer = PythonEngine.CurrentRefDecrementer; | |
| 22 | 22 | IntPtr gs = PythonEngine.AcquireLock(); | |
| 23 | 23 | Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB); | |
| 24 | - Runtime.XIncref(_pyType); | ||
| 25 | - Runtime.XIncref(_pyValue); | ||
| 26 | - Runtime.XIncref(_pyTB); | ||
| 24 | + | ||
| 25 | + // Those references already owned by the caller | ||
| 26 | + ////Runtime.XIncref(_pyType); | ||
| 27 | + ////Runtime.XIncref(_pyValue); | ||
| 28 | + ////Runtime.XIncref(_pyTB); | ||
| 27 | 29 | if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero) | |
| 28 | 30 | { | |
| 29 | 31 | string type; | |
@@ -44,7 +46,13 @@ public PythonException() | |||
| 44 | 46 | } | |
| 45 | 47 | if (_pyTB != IntPtr.Zero) | |
| 46 | 48 | { | |
| 47 | - PyObject tb_module = PythonEngine.ImportModule("traceback"); | ||
| 49 | + | ||
| 50 | + if (_tbModule == null) | ||
| 51 | + { | ||
| 52 | + _tbModule = PythonEngine.ImportModule("traceback"); | ||
| 53 | + } | ||
| 54 | + PyObject tb_module = _tbModule; | ||
| 55 | + | ||
| 48 | 56 | Runtime.XIncref(_pyTB); | |
| 49 | 57 | using (var pyTB = new PyObject(_pyTB)) | |
| 50 | 58 | { | |
@@ -54,11 +62,14 @@ public PythonException() | |||
| 54 | 62 | PythonEngine.ReleaseLock(gs); | |
| 55 | 63 | } | |
| 56 | 64 | ||
| 65 | + private static PyObject _tbModule; | ||
| 66 | + | ||
| 57 | 67 | // Ensure that encapsulated Python objects are decref'ed appropriately | |
| 58 | 68 | // when the managed exception wrapper is garbage-collected. | |
| 59 | 69 | ||
| 60 | 70 | ~PythonException() | |
| 61 | 71 | { | |
| 72 | + | ||
| 62 | 73 | Dispose(false); | |
| 63 | 74 | } | |
| 64 | 75 | ||
@@ -152,49 +163,51 @@ protected void Dispose(bool disposing) | |||
| 152 | 163 | { | |
| 153 | 164 | disposed = true; | |
| 154 | 165 | ||
| 166 | + IntPtr pyTypeToDispose = _pyType; | ||
| 167 | + _pyType = IntPtr.Zero; | ||
| 168 | + | ||
| 169 | + IntPtr pyValueToDispose = _pyValue; | ||
| 170 | + _pyValue = IntPtr.Zero; | ||
| 171 | + | ||
| 172 | + IntPtr pyTBToDispose = _pyTB; | ||
| 173 | + _pyTB = IntPtr.Zero; | ||
| 174 | + | ||
| 155 | 175 | if (disposing) | |
| 156 | 176 | { | |
| 157 | 177 | if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing) | |
| 158 | 178 | { | |
| 179 | + IntPtr gs = PythonEngine.AcquireLock(); | ||
| 159 | 180 | try | |
| 160 | 181 | { | |
| 161 | - IntPtr gs = PythonEngine.AcquireLock(); | ||
| 162 | - try | ||
| 163 | - { | ||
| 164 | - Runtime.XDecref(_pyType); | ||
| 165 | - Runtime.XDecref(_pyValue); | ||
| 166 | - // XXX Do we ever get TraceBack? // | ||
| 167 | - if (_pyTB != IntPtr.Zero) | ||
| 168 | - { | ||
| 169 | - Runtime.XDecref(_pyTB); | ||
| 170 | - } | ||
| 171 | - } | ||
| 172 | - finally | ||
| 182 | + Runtime.XDecref(pyTypeToDispose); | ||
| 183 | + Runtime.XDecref(pyValueToDispose); | ||
| 184 | + // XXX Do we ever get TraceBack? // | ||
| 185 | + if (pyTBToDispose != IntPtr.Zero) | ||
| 173 | 186 | { | |
| 174 | - PythonEngine.ReleaseLock(gs); | ||
| 187 | + Runtime.XDecref(pyTBToDispose); | ||
| 175 | 188 | } | |
| 176 | 189 | } | |
| 177 | - catch | ||
| 190 | + finally | ||
| 178 | 191 | { | |
| 179 | - // Do nothing. | ||
| 192 | + PythonEngine.ReleaseLock(gs); | ||
| 180 | 193 | } | |
| 181 | 194 | } | |
| 182 | 195 | } | |
| 183 | 196 | else | |
| 184 | 197 | { | |
| 185 | - if (_pyType != IntPtr.Zero) | ||
| 198 | + if (pyTypeToDispose != IntPtr.Zero) | ||
| 186 | 199 | { | |
| 187 | - _referenceDecrementer?.ScheduleDecRef(_pyType); | ||
| 200 | + _referenceDecrementer?.ScheduleDecRef(pyTypeToDispose); | ||
| 188 | 201 | } | |
| 189 | 202 | ||
| 190 | - if (_pyValue != IntPtr.Zero) | ||
| 203 | + if (pyValueToDispose != IntPtr.Zero) | ||
| 191 | 204 | { | |
| 192 | - _referenceDecrementer?.ScheduleDecRef(_pyValue); | ||
| 205 | + _referenceDecrementer?.ScheduleDecRef(pyValueToDispose); | ||
| 193 | 206 | } | |
| 194 | 207 | ||
| 195 | - if (_pyTB != IntPtr.Zero) | ||
| 208 | + if (pyTBToDispose != IntPtr.Zero) | ||
| 196 | 209 | { | |
| 197 | - _referenceDecrementer?.ScheduleDecRef(_pyTB); | ||
| 210 | + _referenceDecrementer?.ScheduleDecRef(pyTBToDispose); | ||
| 198 | 211 | } | |
| 199 | 212 | } | |
| 200 | 213 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments