Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions src/runtime/finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,9 @@ public void Collect(bool forceDispose = true)
if (Instance._finalizerTask != null
&& !Instance._finalizerTask.IsCompleted)
{
using (Py.GIL())
{
var ts = PythonEngine.BeginAllowThreads();
Instance._finalizerTask.Wait();
PythonEngine.EndAllowThreads(ts);
}
var ts = PythonEngine.BeginAllowThreads();
Instance._finalizerTask.Wait();
PythonEngine.EndAllowThreads(ts);
}
else if (forceDispose)
{
Expand Down Expand Up @@ -147,8 +144,11 @@ private void AddPendingCollect()

_finalizerTask = Task.Factory.StartNew(() =>
{
Instance.DisposeAll();
_pending = false;
using (Py.GIL())
{
Instance.DisposeAll();
_pending = false;
}
});
}
}
Expand All @@ -169,27 +169,24 @@ private void DisposeAll()
lock (_queueLock)
#endif
{
using (Py.GIL())
{
#if FINALIZER_CHECK
ValidateRefCount();
ValidateRefCount();
#endif
IPyDisposable obj;
while (_objQueue.TryDequeue(out obj))
IPyDisposable obj;
while (_objQueue.TryDequeue(out obj))
{
try
{
try
{
obj.Dispose();
Runtime.CheckExceptionOccurred();
}
catch (Exception e)
obj.Dispose();
Runtime.CheckExceptionOccurred();
}
catch (Exception e)
{
// We should not bother the main thread
ErrorHandler?.Invoke(this, new ErrorArgs()
{
// We should not bother the main thread
ErrorHandler?.Invoke(this, new ErrorArgs()
{
Error = e
});
}
Error = e
});
}
}
}
Expand Down