File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 1+ namespace Python . EmbeddingTest
2+ {
3+ using NUnit . Framework ;
4+ using Python . Runtime ;
5+
6+ public class TestGILState
7+ {
8+ /// <summary>
9+ /// Ensure, that calling <see cref="Py.GILState.Dispose"/> multiple times is safe
10+ /// </summary>
11+ [ Test ]
12+ public void CanDisposeMultipleTimes ( )
13+ {
14+ using ( var gilState = Py . GIL ( ) )
15+ {
16+ for ( int i = 0 ; i < 50 ; i ++ )
17+ gilState . Dispose ( ) ;
18+ }
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change @@ -643,7 +643,8 @@ public static PyScope CreateScope(string name)
643643
644644 public class GILState : IDisposable
645645 {
646- private IntPtr state ;
646+ private readonly IntPtr state ;
647+ private bool isDisposed ;
647648
648649 internal GILState ( )
649650 {
@@ -652,8 +653,11 @@ internal GILState()
652653
653654 public void Dispose ( )
654655 {
656+ if ( this . isDisposed ) return ;
657+
655658 PythonEngine . ReleaseLock ( state ) ;
656659 GC . SuppressFinalize ( this ) ;
660+ this . isDisposed = true ;
657661 }
658662
659663 ~ GILState ( )
You can’t perform that action at this time.
0 commit comments