Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/embed_tests/TestGILState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Python.EmbeddingTest
{
using NUnit.Framework;
using Python.Runtime;

public class TestGILState
{
/// <summary>
/// Ensure, that calling <see cref="Py.GILState.Dispose"/> multiple times is safe
/// </summary>
[Test]
public void CanDisposeMultipleTimes()
{
using (var gilState = Py.GIL())
{
for(int i = 0; i < 50; i++)
gilState.Dispose();
}
}
}
}
6 changes: 5 additions & 1 deletion src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,8 @@ public static PyScope CreateScope(string name)

public class GILState : IDisposable
{
private IntPtr state;
private readonly IntPtr state;
private bool isDisposed;

internal GILState()
{
Expand All @@ -652,8 +653,11 @@ internal GILState()

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

PythonEngine.ReleaseLock(state);
GC.SuppressFinalize(this);
this.isDisposed = true;
}

~GILState()
Expand Down