8000 allow GIL state debugging; require GILState to be properly disposed · pythonnet/pythonnet@1e5338f · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 1e5338f

Browse files
committed
allow GIL state debugging; require GILState to be properly disposed
#1389 (comment)
1 parent 4afa4bd commit 1e5338f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/runtime/pythonengine.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Runtime.InteropServices;
7+
using System.Threading;
78

89
namespace Python.Runtime
910
{
@@ -51,6 +52,9 @@ public static bool IsInitialized
5152
get { return initialized; }
5253
}
5354

55+
/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
56+
public static bool DebugGIL { get; set; } = false;
57+
5458
internal static DelegateManager DelegateManager
5559
{
5660
get
@@ -633,7 +637,7 @@ public static GILState GIL()
633637
PythonEngine.Initialize();
634638
}
635639

636-
return new GILState();
640+
return PythonEngine.DebugGIL ? new DebugGILState() : new GILState();
637641
}
638642

639643
public static PyScope CreateScope()
@@ -658,7 +662,7 @@ internal GILState()
658662
state = PythonEngine.AcquireLock();
659663
}
660664

661-
public void Dispose()
665+
public virtual void Dispose()
662666
{
663667
if (this.isDisposed) return;
664668

@@ -669,7 +673,23 @@ public void Dispose()
669673

670674
~GILState()
671675
{
672-
Dispose();
676+
throw new InvalidOperationException("GIL must always be released, and it must be released from the same thread that acquired it.");
677+
}
678+
}
679+
680+
public class DebugGILState : GILState
681+
{
682+
readonly Thread owner;
683+
internal DebugGILState() : base()
684+
{
685+
this.owner = Thread.CurrentThread;
686+
}
687+
public override void Dispose()
688+
{
689+
if (this.owner != Thread.CurrentThread)
690+
throw new InvalidOperationException("GIL must alwa 538D ys be released from the same thread, that acquired it");
691+
692+
base.Dispose();
673693
}
674694
}
675695

0 commit comments

Comments
 (0)
0