Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0c25c1

Browse files
committed
finalizer does not attempt to finalize objects when runtime is shut down
1 parent d6a853f commit b0c25c1

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/runtime/finalizer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public ErrorArgs(Exception error)
3434
[DefaultValue(DefaultThreshold)]
3535
public int Threshold { get; set; } = DefaultThreshold;
3636

37+
bool started;
38+
3739
[DefaultValue(true)]
3840
public bool Enable { get; set; } = true;
3941

@@ -105,7 +107,7 @@ internal IncorrectRefCountException(IntPtr ptr)
105107
internal void ThrottledCollect()
106108
{
107109
_throttled = unchecked(this._throttled + 1);
108-
if (!Enable || _throttled < Threshold) return;
110+
if (!started || !Enable || _throttled < Threshold) return;
109111
_throttled = 0;
110112
this.Collect();
111113
}
@@ -131,9 +133,15 @@ internal void AddFinalizedObject(ref IntPtr obj)
131133
obj = IntPtr.Zero;
132134
}
133135

136+
internal static void Initialize()
137+
{
138+
Instance.started = true;
139+
}
140+
134141
internal static void Shutdown()
135142
{
136143
Instance.DisposeAll();
144+
Instance.started = false;
137145
}
138146

139147
private void DisposeAll()

src/runtime/runtime.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
149149
}
150150
else
151151
{
152-
PyCLRMetaType = MetaType.Initialize(); // Steal a reference
152+
PyCLRMetaType = MetaType.Initialize();
153153
ImportHook.Initialize();
154154
}
155+
Finalizer.Initialize();
155156
Exceptions.Initialize();
156157

157158
// Need to add the runtime directory to sys.path so that we
@@ -286,12 +287,13 @@ internal static void Shutdown(ShutdownMode mode)
286287
ClassManager.DisposePythonWrappersForClrTypes();
287288
TypeManager.RemoveTypes();
288289

290+
Finalizer.Shutdown();
291+
289292
MetaType.Release();
290293
PyCLRMetaType.Dispose();
291294
PyCLRMetaType = null!;
292295

293296
Exceptions.Shutdown();
294-
Finalizer.Shutdown();
295297
InternString.Shutdown();
296298

297299
if (mode != ShutdownMode.Normal && mode != ShutdownMode.Extension)

0 commit comments

Comments
 (0)