Skip to content

Commit be2eefa

Browse files
committed
ClassManager illegally decrefed ClassObject's refcount on shutdown
addresses #1561
1 parent 09ecf1b commit be2eefa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/embed_tests/pyinitialize.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ public static void LoadSpecificArgs()
4646
}
4747
}
4848

49+
// regression test for https://github.com/pythonnet/pythonnet/issues/1561
50+
[Test]
51+
public void ImportClassShutdownRefcount()
52+
{
53+
PythonEngine.Initialize();
54+
55+
PyObject ns = Py.Import(typeof(ImportClassShutdownRefcountClass).Namespace);
56+
PyObject cls = ns.GetAttr(nameof(ImportClassShutdownRefcountClass));
57+
ns.Dispose();
58+
59+
Assert.Less(cls.Refcount, 256);
60+
61+
PythonEngine.Shutdown();
62+
Assert.Greater(cls.Refcount, 0);
63+
}
64+
4965
/// <summary>
5066
/// Failing test demonstrating current issue with OverflowException (#376)
5167
/// and ArgumentException issue after that one is fixed.
@@ -182,4 +198,6 @@ public static void TestRunExitFuncs()
182198
Assert.True(called);
183199
}
184200
}
201+
202+
public class ImportClassShutdownRefcountClass { }
185203
}

src/runtime/classmanager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ internal static void DisposePythonWrappersForClrTypes()
6767
// since others may still referencing it.
6868
cls.CallTypeTraverse(TraverseTypeClear, visitedPtr);
6969
cls.CallTypeClear();
70-
cls.DecrRefCount();
7170
}
7271
}
7372
finally

0 commit comments

Comments
 (0)