Skip to content
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

GIL state when calling C# from Python is documented incorrectly #2272

Open
SuuperW opened this issue Oct 22, 2023 · 2 comments
Open

GIL state when calling C# from Python is documented incorrectly #2272

SuuperW opened this issue Oct 22, 2023 · 2 comments

Comments

@SuuperW
Copy link

SuuperW commented Oct 22, 2023

I recently ran into the same issue as #795. (To summarize: I obtained the GIL in .NET, executed Python code that called a .NET method that called Python. This caused a AccessViolationException exception.) That issue was closed with a "by design" comment, with no explanation as to why it is designed this way nor any reference to documentation.

Issue #2208 is similar and was also closed, with a comment saying "When Python calls C# GIL is not held in the first place".

This behavior is counter-intuitive, and documentation even seems to say that this is not expected behavior. The threading wiki page states that "If you are calling C# from Python ... release the GIL" implying that the GIL is still held when Python calls .NET. In fact, calling PythonEngine.BeginAllowThreads() as it recommends throws an exception.

I would suggest that the threading page be updated, and the README be updated to clarify this where it says "All calls to python should be inside a using (Py.GIL()) {/* Your code here */} block.".

@filmor
Copy link
Member

filmor commented Oct 22, 2023

  1. The threading wiki is a wiki, it's not official documentation. If you want to change something, change it
  2. PRs are always welcome. It is however not quite true that all calls to Python need to be wrapped, you can use the ForbidPythonThreads attribute to enter a C# function with the GIL being held.

@SuuperW
Copy link
Author

SuuperW commented Nov 7, 2023

Regarding point 2, that attribute is internal so I cannot use it.

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