Exception thrown when initializing Python .NET after a domain unload

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)