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 @@ -645,7 +645,8 @@ public static PyScope CreateScope(string name)
645
645
646
646
public class GILState : IDisposable
647
647
{
648
- private IntPtr state ;
648
+ private readonly IntPtr state ;
649
+ private bool isDisposed ;
649
650
650
651
internal GILState ( )
651
652
{
@@ -654,8 +655,11 @@ internal GILState()
654
655
655
656
public void Dispose ( )
656
657
{
658
+ if ( this . isDisposed ) return ;
659
+
657
660
PythonEngine . ReleaseLock ( state ) ;
658
661
GC . SuppressFinalize ( this ) ;
662
+ this . isDisposed = true ;
659
663
}
660
664
661
665
~ GILState ( )
You can’t perform that action at this time.
0 commit comments