8000 Allow GIL state debugging; require GILState to be properly disposed by lostmsu · Pull Request #1397 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Allow GIL state debugging; require GILState to be properly disposed #1397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2021
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
26 changes: 23 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace Python.Runtime
{
Expand Down Expand Up @@ -51,6 +52,9 @@ public static bool IsInitialized
get { return initialized; }
}

/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
public static bool DebugGIL { get; set; } = false;

internal static DelegateManager DelegateManager
{
get
Expand Down Expand Up @@ -668,7 +672,7 @@ public static GILState GIL()
PythonEngine.Initialize();
}

return new GILState();
return PythonEngine.DebugGIL ? new DebugGILState() : new GILState();
}

public static PyScope CreateScope()
Expand All @@ -693,7 +697,7 @@ internal GILState()
state = PythonEngine.AcquireLock();
}

public void Dispose()
public virtual void Dispose()
{
if (this.isDisposed) return;

Expand All @@ -704,7 +708,23 @@ public void Dispose()

~GILState()
{
Dispose();
throw new InvalidOperationException("GIL must always be released, and it must be released from the same threa 6BC7 d that acquired it.");
}
}

public class DebugGILState : GILState
{
readonly Thread owner;
internal DebugGILState() : base()
{
this.owner = Thread.CurrentThread;
}
public override void Dispose()
{
if (this.owner != Thread.CurrentThread)
throw new InvalidOperationException("GIL must always be released from the same thread, that acquired it");

base.Dispose();
}
}

Expand Down
0