Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.9.13
  • Operating System: Windows 11
  • .NET Runtime: .NET Core 3.1

Details

I'm trying to prevent a long running method in C# from blocking execution in Python. I followed the guidance on https://github.com/pythonnet/pythonnet/wiki/Threading but I am getting a crash.

import pythonnet
pythonnet.load('coreclr')

import os
bin_path = os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + "/../LongRunningTask/bin/Debug/netcoreapp3.1")

import clr
clr.AddReference(os.path.join(bin_path, "LongRunningTask"))
clr.AddReference("System")

from LongRunningTask import LongRunningTask
from threading import Thread, Timer

t1 = Thread(target=LongRunningTask.Run)

t1.daemon = True
t1.start()
using System.Threading;
using Python.Runtime;

namespace LongRunningTask
{
    public class LongRunningTask
    {
        public static void Run()
        {
            var state = PythonEngine.BeginAllowThreads();
            Thread.Sleep(1000);
            PythonEngine.EndAllowThreads(state);
        }
    }
}
  • If there was a crash, please include the traceback here.
Fatal Python error: PyGILState_Release: thread state 000001D4A5CA2A40 must be current when releasing
Python runtime state: initialized

Thread 0x00001c08 (most recent call first):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 917 in run
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 980 in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 937 in _bootstrap

Current thread 0x00005f6c (most recent call first):
  File "C:\Users\shersh\AppData\Roaming\Python\Python39\site-packages\clr_loader\types.py", line 64 in __call__
  File "C:\Users\shersh\AppData\Roaming\Python\Python39\site-packages\pythonnet\__init__.py", line 157 in unload
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Python.Runtime.Runtime.PyGILState_Release(Python.Runtime.Native.PyGILState)
   at Python.Runtime.PythonEngine.ReleaseLock(Python.Runtime.Native.PyGILState)
   at Python.Runtime.Py+GILState.Dispose()
   at Python.Runtime.Loader.Shutdown(IntPtr, Int32)
You must be logged in to vote

Replies: 2 comments · 3 replies

Comment options

When Python calls C# GIL is not held in the first place, so no need to release anything.

You must be logged in to vote
1 reply
@sherryyshi
Comment options

I see, thanks.

Comment options

Hi @lostmsu. I'm confused by your comment:

"When Python calls C# GIL is not held in the first place, so no need to release anything"

This seems to contradict this page https://github.com/pythonnet/pythonnet/wiki/Threading which says:

"If you are calling C# from Python, and the C# code performs a long-running operation (e.g. computation, I/O, sleep, acquiring a lock, ...), release the GIL via PythonEngine.BeginAllowThreads(). Remember to restore it before returning control to Python"

The same question is here: #2424

You must be logged in to vote
2 replies
@lostmsu
Comment options

This might be confusing. I believe by default now GIL is not held when .NET code is called from Python. So actually in order to work with any Python objects you have to call and hold Py.GIL().

The wiki is outdated.

@DareDevilDenis
Comment options

Thanks for the clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2208 on August 04, 2023 22:07.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.