File tree 2 files changed +26
-1
lines changed
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)
643
643
644
644
public class GILState : IDisposable
645
645
{
646
- private IntPtr state ;
646
+ private readonly IntPtr state ;
647
+ private bool isDisposed ;
647
648
648
649
internal GILState ( )
649
650
{
@@ -652,8 +653,11 @@ internal GILState()
652
653
653
654
public void Dispose ( )
654
655
{
656
+ if ( this . isDisposed ) return ;
657
+
655
658
PythonEngine . ReleaseLock ( state ) ;
656
659
GC . SuppressFinalize ( this ) ;
660
+ this . isDisposed = true ;
657
661
}
658
662
659
663
~ GILState ( )
You can’t perform that action at this time.
0 commit comments