Skip to content

Commit 95a22a3

Browse files
committed
Revert "Merge pull request pythonnet#1712 from losttech/bugs/ShutdownSegFault"
This reverts commit 1cf616d, reversing changes made to 0e57cdd.
1 parent 7247da5 commit 95a22a3

7 files changed

Lines changed: 24 additions & 37 deletions

File tree

pythonnet.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Repo", "Repo", "{441A0123-F
2525
EndProject
2626
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{D301657F-5EAF-4534-B280-B858D651B2E5}"
2727
ProjectSection(SolutionItems) = preProject
28-
.github\workflows\ARM.yml = .github\workflows\ARM.yml
2928
.github\workflows\main.yml = .github\workflows\main.yml
3029
.github\workflows\nuget-preview.yml = .github\workflows\nuget-preview.yml
3130
EndProjectSection

pythonnet/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def set_runtime(runtime):
1616

1717

1818
def set_default_runtime() -> None:
19-
if sys.platform == "win32":
19+
if sys.platform == 'win32':
2020
set_runtime(clr_loader.get_netfx())
2121
else:
2222
set_runtime(clr_loader.get_mono())
@@ -36,23 +36,22 @@ def load():
3636
set_default_runtime()
3737

3838
dll_path = join(dirname(__file__), "runtime", "Python.Runtime.dll")
39-
39+
4040
_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)
4141

4242
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
43-
if func(b"") != 0:
43+
if func(''.encode("utf8")) != 0:
4444
raise RuntimeError("Failed to initialize Python.Runtime.dll")
4545

4646
import atexit
47-
4847
atexit.register(unload)
4948

5049

5150
def unload():
5251
global _RUNTIME
5352
if _LOADER_ASSEMBLY is not None:
5453
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"]
55-
if func(b"full_shutdown") != 0:
54+
if func(b"") != 0:
5655
raise RuntimeError("Failed to call Python.NET shutdown")
5756

5857
if _RUNTIME is not None:

src/runtime/Runtime.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ private static string GetDefaultDllName(Version version)
5454
}
5555

5656
private static bool _isInitialized = false;
57+
5758
internal static bool IsInitialized => _isInitialized;
58-
private static bool _typesInitialized = false;
59-
internal static bool TypeManagerInitialized => _typesInitialized;
6059
internal static readonly bool Is32Bit = IntPtr.Size == 4;
6160

6261
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
@@ -152,7 +151,6 @@ internal static void Initialize(bool initSigs = false)
152151
ClassManager.Reset();
153152
ClassDerivedObject.Reset();
154153
TypeManager.Initialize();
155-
_typesInitialized = true;
156154

157155
// Initialize modules that depend on the runtime class.
158156
AssemblyManager.Initialize();
@@ -275,7 +273,6 @@ internal static void Shutdown()
275273
NullGCHandles(ExtensionType.loadedExtensions);
276274
ClassManager.RemoveClasses();
277275
TypeManager.RemoveTypes();
278-
_typesInitialized = false;
279276

280277
MetaType.Release();
281278
PyCLRMetaType.Dispose();
@@ -296,10 +293,9 @@ internal static void Shutdown()
296293
Finalizer.Shutdown();
297294
InternString.Shutdown();
298295

299-
ResetPyMembers();
300-
301296
if (!HostedInPython)
302297
{
298+
ResetPyMembers();
303299
GC.Collect();
304300
GC.WaitForPendingFinalizers();
305301
PyGILState_Release(state);
@@ -316,6 +312,7 @@ internal static void Shutdown()
316312
}
317313
else
318314
{
315+
ResetPyMembers();
319316
PyGILState_Release(state);
320317
}
321318
}

src/runtime/TypeManager.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,18 @@ internal static void Initialize()
5151

5252
internal static void RemoveTypes()
5353
{
54-
if (Runtime.HostedInPython)
54+
foreach (var type in cache.Values)
5555
{
56-
foreach (var holder in _slotsHolders)
56+
if (Runtime.HostedInPython
57+
&& _slotsHolders.TryGetValue(type, out var holder))
5758
{
5859
// If refcount > 1, it needs to reset the managed slot,
5960
// otherwise it can dealloc without any trick.
60-
if (holder.Key.Refcount > 1)
61+
if (Runtime.Refcount(type) > 1)
6162
{
62-
holder.Value.ResetSlots();
63+
holder.ResetSlots();
6364
}
6465
}
65-
}
66-
67-
foreach (var type in cache.Values)
68-
{
6966
type.Dispose();
7067
}
7168
cache.Clear();
@@ -510,7 +507,7 @@ internal static PyType CreateMetaType(Type impl, out SlotsHolder slotsHolder)
510507
{
511508
throw PythonException.ThrowLastAsClrException();
512509
}
513-
510+
514511
BorrowedReference dict = Util.ReadRef(type, TypeOffset.tp_dict);
515512
using (var mod = Runtime.PyString_FromString("clr._internal"))
516513
Runtime.PyDict_SetItemString(dict, "__module__", mod.Borrow());
@@ -729,7 +726,6 @@ internal static void CopySlot(BorrowedReference from, BorrowedReference to, int
729726

730727
internal static SlotsHolder CreateSlotsHolder(PyType type)
731728
{
732-
type = new PyType(type);
733729
var holder = new SlotsHolder(type);
734730
_slotsHolders.Add(type, holder);
735731
return holder;
@@ -832,7 +828,6 @@ public void ResetSlots()
832828
var metatype = Runtime.PyObject_TYPE(Type);
833829
ManagedType.TryFreeGCHandle(Type, metatype);
834830
}
835-
Runtime.PyType_Modified(Type);
836831
}
837832

838833
public static IntPtr GetDefaultSlot(int offset)

src/runtime/Types/ClassDerived.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ protected override NewReference NewObjectToPython(object obj, BorrowedReference
5959
// Decrement the python object's reference count.
6060
// This doesn't actually destroy the object, it just sets the reference to this object
6161
// to be a weak reference and it will be destroyed when the C# object is destroyed.
62-
Runtime.XDecref(self.Steal());
62+
if (!self.IsNull())
63+
{
64+
Runtime.XDecref(self.Steal());
65+
}
6366

6467
return Converter.ToPython(obj, type.Value);
6568
}
@@ -939,16 +942,13 @@ internal static void Finalize(IntPtr derived)
939942

940943
var type = Runtime.PyObject_TYPE(@ref.Borrow());
941944

942-
if (!Runtime.HostedInPython || Runtime.TypeManagerInitialized)
943-
{
944-
// rare case when it's needed
945-
// matches correspdonging PyObject_GC_UnTrack
946-
// in ClassDerivedObject.tp_dealloc
947-
Runtime.PyObject_GC_Del(@ref.Steal());
945+
// rare case when it's needed
946+
// matches correspdonging PyObject_GC_UnTrack
947+
// in ClassDerivedObject.tp_dealloc
948+
Runtime.PyObject_GC_Del(@ref.Steal());
948949

949-
// must decref our type
950-
Runtime.XDecref(StolenReference.DangerousFromPointer(type.DangerousGetAddress()));
951-
}
950+
// must decref our type
951+
Runtime.XDecref(StolenReference.DangerousFromPointer(type.DangerousGetAddress()));
952952
}
953953

954954
internal static FieldInfo? GetPyObjField(Type type) => type.GetField(PyObjName, PyObjFlags);

src/runtime/Types/ModuleObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ public static Assembly AddReference(string name)
542542
/// <returns>The Type object</returns>
543543

544544
[ModuleFunction]
545+
[ForbidPythonThreads]
545546
public static Type GetClrType(Type type)
546547
{
547548
return type;

tests/test_engine.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,3 @@ def test_run_string():
4141
assert sys.multiline_worked == 1
4242

4343
PythonEngine.ReleaseLock()
44-
45-
def test_leak_type():
46-
import clr
47-
sys._leaked_intptr = clr.GetClrType(System.IntPtr)

0 commit comments

Comments
 (0)