Skip to content

Exception thrown when initializing Python .NET after a domain unload #714

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
lassond opened this issue Aug 9, 2018 · 5 comments
Closed

Comments

@lassond
Copy link

lassond commented Aug 9, 2018

Environment

  • Pythonnet version: latest master commit as of 2018-08-09 (fb84cd2 )

  • Python version:
    Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32

  • Operating System:
    Windows 10

  • In the context of running in Unity, but also reproducible in a simple standalone app made from scratch

Details

Unity, whenever a C# script is changed, the AppDomain in which the Assemblies are loaded gets unloaded. Then, Unity creates a fresh AppDomain, recompiles the C# assemblies and loads them again.

The Python.Runtime assembly gets unloaded, but the Python interpreter survives (it is a native library). This results in an inconsistent state between the Python .NET Engine/Runtime classes (which are not initialized) and the Python interpreter (which is initialized).

An exception is raised when Python .NET accesses the interpreter (System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.)

  • Describe what you were trying to get done.

See above

I will attach my standalone application which reproduces this crash along with the repro steps. (either ere or on the pull request if I cannot attach here)

@lassond
Copy link
Author

lassond commented Aug 9, 2018

hotReloadCrashRepro.zip

(Note: with the upcoming fix, the application will still raise an exception, but on the second domain unload - a bit further. A fix for this second crash is under review)

@amos402
Copy link
Member

amos402 commented Sep 6, 2018

Python crashed because pythonnet's function pointers just leak in Python, you can just reset the Python GC avoid that, although it would make more memory leaks in unity editor.

You can modify Python like below:
amos402/cpython@3fb76b5

By the way, I'm working on a new binding mode for Python call that supported IL2CPP and with more higher performance, this pythonnet will a part of my unity package.
https://github.com/amos402/pythonnet/tree/fast-call

@benoithudson
Copy link
Contributor

This is fixed and the test case integrated in #752. But I don't have permissions to close the bug.

@filmor
Copy link
Member

filmor commented Oct 23, 2018

But I do, thanks again :)

@dyeo
Copy link

dyeo commented Jul 7, 2021

By the way, I'm working on a new binding mode for Python call that supported IL2CPP and with more higher performance, this pythonnet will a part of my unity package.
https://github.com/amos402/pythonnet/tree/fast-call

@amos402 did you ever make any headway with the new binding mode that enables IL2CPP support? If not, any chance I could see what you accomplished at the time?

I'm working on a unity project that compiles to IL2CPP on iOS and Android. Since iOS does not have Mono support this is not an option otherwise.

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

5 participants