Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python 3.6.1 "Fatal Python error: take_gil: NULL tstate". Works with Python 3.6.14 #1751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DareDevilDenis opened this issue Apr 5, 2022 · 1 comment

Comments

@DareDevilDenis
Copy link

Can you confirm if Python 3.6 support is still being removed. Ref: #1640 "Drop Python 3.6 support"

The following code works with Python 3.6.14 but fails with Python 3.6.1 with message: Fatal Python error: take_gil: NULL tstate

Pythonnet version: 3.0.0a2
Python version: 3.6.1=Fail. 3.6.14=Pass
Operating System: Windows 10
.NET Runtime: .NET 6.0.3

The problems happens when .NET tries to callback to Python on line callback.DynamicInvoke(paramToPass);
Console output:

.NET: Got message: Hello from Python
PYTHON: Calling AskForCallback
.NET: AskForCallback
.NET: AskForCallback finished
PYTHON: Sleeping...
.NET: Calling callback
Fatal Python error: take_gil: NULL tstate

Thread 0x00003424 (most recent call first):
  File "my_python.py", line 29 in <module>

Python

import os
import clr
import time
from clr_loader import get_coreclr
from pythonnet import set_runtime

my_path = os.path.abspath(os.path.dirname(__file__))
dotnet_app_path = os.path.join(my_path, r'MyDotNetApp\out_path')

rt = get_coreclr(os.path.join(dotnet_app_path, 'MyApp.runtimeconfig.json'))
set_runtime(rt)

clr.FindAssembly(os.path.join(dotnet_app_path, 'MyApp'))
clr.AddReference(os.path.join(dotnet_app_path, 'MyApp'))

import System
from MyApp import MyClass

def my_callback(my_int, my_string):
    print(f'my_callback: my_int={my_int}, my_string={my_string}')

dot_net_obj = MyClass()
dot_net_obj.PrintToConsole('Hello from Python')

print('PYTHON: Calling AskForCallback')
dot_net_obj.AskForCallback(System.Action[System.Object, System.Object](my_callback))
print('PYTHON: Sleeping...')
time.sleep(10)
print('Finished')

.NET

using System;
using System.Threading;
using Python.Runtime;

namespace MyApp
{
    public class MyClass
    {
        public int PrintToConsole(string message)
        {
            Console.WriteLine(".NET: Got message: " + message);
            return 0;
        }

        public void AskForCallback(Delegate callback)
        {
            Console.WriteLine(".NET: AskForCallback");
            new Thread(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine(".NET: Calling callback");

                using (Py.GIL())
                {
                    Console.WriteLine(".NET: Aquired Py.GIL");
                    object[] paramToPass = new object[2];
                    paramToPass[0] = 12345;
                    paramToPass[1] = "This is a callback!";
                    Console.WriteLine($".NET: Calling Python");
                    callback.DynamicInvoke(paramToPass);
                    Console.WriteLine(".NET: Called Python");
                }
            }).Start();
            Console.WriteLine(".NET: AskForCallback finished");
        }
    }
}

Full solution attached: Pythonnet issue with Python 3.6.1.zip

@filmor
Copy link
Member

filmor commented Apr 5, 2022

Support for Python 3.6 will be removed, I would just like to keep it for 3.0. If this issue is not reproducible on the latest 3.6 patch release, it was likely a Python bug (when using the embedding of .NET into Python, we don't even touch the thread state). Thank you for the report, though :)

@filmor filmor closed this as completed Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants