GitHub
· Extracted in 1406 ms
Enable pythonnet to survive in the Unity3d editor environment. by benoithudson · Pull Request #752 · pythonnet/pythonnet
Source: https://github.com/pythonnet/pythonnet/pull/752
Repository navigation
Enable pythonnet to survive in the Unity3d editor environment. - #752
Merged
filmor merged 3 commits into
pythonnet:master from
Unity-Technologies:fix-domain-unload
Oct 18, 2018
Merged
Conversation
Copy link
Copy Markdown
Contributor
The Unity editor unloads the C# domain and creates a new domain every time it recompiles C# sources.
This caused two crashes.
-
After a domain unload, python is now pointing to a bunch of objects whose C# side is now garbage. Solution : Shutdown() the engine on domain reload, which calls Py_Finalize.
-
After a domain unload, Py_Finalize, and a new Py_Intialize, python still keeps pointers in gc for any python objects that were leaked. And pythonnet leaks. This means that python's gc will be calling tp_traverse, tp_clear, and tp_is_gc on types that are implemented in C#. This crashes because the implementation was freed. Solution : implement those calls in code that is not released on domain unload. Side effect: we leak a page on every domain unload. I suspect but didn't test that python gc performance will be slightly faster.
Changes required to implement and test:
3. Use python's platform package to determine what we're running on, so we can use the right mmap/mprotect or VirtualAlloc/VirtualProtect routines, the right flags for mmap, and (theoretically) the right native code. Side effect: to port to a new platform requires updating some additional code now.
- New unit tests. (a) A new test for domain reload. It doesn't run under .NET Core because you can't create and unload a domain in .NET Core. (b) New unit tests to help us find where to add support for new platforms.
Does this close any currently open issues?
#714 ; see also #745 and #751 .
Enable pythonnet to survive in the Unity3d editor environment.
02efcdf
The Unity editor unloads the C# domain and creates a new domain every time it recompiles C# sources.
This caused two crashes.
1. After a domain unload, python is now pointing to a bunch of objects whose C# side is now garbage. Solution: Shutdown() the engine on domain reload, which calls Py_Finalize.
2. After a domain unload, Py_Finalize, and a new Py_Intialize, python still keeps pointers in gc for any python objects that were leaked. And pythonnet leaks. This means that python's gc will be calling tp_traverse, tp_clear, and tp_is_gc on types that are implemented in C#. This crashes because the implementation was freed. Solution: implement those calls in code that is *not* released on domain unload. Side effect: we leak a page on every domain unload. I suspect but didn't test that python gc performance will be slightly faster.
Changes required to implement and test:
3. Use python's platform package to determine what we're running on, so we can use the right mmap/mprotect or VirtualAlloc/VirtualProtect routines, the right flags for mmap, and (theoretically) the right native code. Side effect: to port to a new platform requires updating some additional code now.
4. New unit tests. (a) A new test for domain reload. It doesn't run under .NET Core because you can't create and unload a domain in .NET Core. (b) New unit tests to help us find where to add support for new platforms.
Copy link
Copy Markdown
Contributor
Author
|
Now we see if I flubbed the merge...
|
Copy link
Copy Markdown
Contributor
|
@benoithudson just to be clear this enables pythonnet survival in Unity3D editor even when AppDomains are not available on that platform? Is AppDomain only used for testing?
|
Copy link
Copy Markdown
Merging #752 into master will decrease coverage by 0.05%.
The diff coverage is 83.96%.
@@ Coverage Diff @@
## master #752 +/- ##
==========================================
- Coverage 77.18% 77.12% -0.06%
==========================================
Files 62 62
Lines 5596 5688 +92
Branches 897 903 +6
==========================================
+ Hits 4319 4387 +68
- Misses 983 1004 +21
- Partials 294 297 +3
| Flag |
Coverage Δ |
|
| #setup_linux |
69.42% <ø> (ø) |
⬆️ |
| #setup_windows |
76.31% <83.96%> (-0.04%) |
⬇️ |
Continue to review full report at Codecov .
Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov . Last update 05a1451...072022b . Read the comment docs .
|
Copy link
Copy Markdown
Contributor
Author
@benoithudson just to be clear this enables pythonnet survival in Unity3D editor even when AppDomains are not available on that platform? Is AppDomain only used for testing?
Exactly the other way: Unity uses app domains, loads and unloads them frequently. And that caused a crash.
CI runs tests in a setting without app domains (and also with app domains). So the patch works either way.
You don't need domains for the fix I made; only the test of whether the system survives domain unload needs domains.
|
filmor
approved these changes
Oct 16, 2018
View reviewed changes
Copy link
Copy Markdown
Member
|
@denfromufa Good for merge?
|
Merge branch 'master' into fix-domain-unload
7222a2c
Copy link
Copy Markdown
Contributor
Merge branch 'master' into fix-domain-unload
072022b
Copy link
Copy Markdown
Contributor
den-run-ai
requested review from
den-run-ai ,
dmitriyse ,
vmuriart and
yagweb
October 18, 2018 05:55
Copy link
Copy Markdown
Member
|
The beauty of this solution is that it does not need all of the boilerplate that you have when using a proper DLL or .so. The code for the two functions is OS independent, it only depends on the CPU architecture. I'm okay with what is here, I would just like us to look into alternative solutions that don't leak the memory page (or at the very least only leak it once).
|
filmor
added this to the
2.4.0 milestone
Oct 18, 2018
filmor
added
the
next
label
Oct 18, 2018
den-run-ai
approved these changes
Oct 18, 2018
View reviewed changes
Copy link
Copy Markdown
Member
|
It seems it's complex to do that.
To prevent Unity crash, only just need to reset the gc.
amos402/cpython@ 3fb76b5
I made a modify in cpython before like above, it works well. I already use this for months.
I just made up a pure C# version by trick for reseting th GC. #754
I still havn't test it fully for this new version.
|
Copy link
Copy Markdown
Member
Copy link
Copy Markdown
Contributor
Author
|
Thanks! @amos402 should get credit for debugging this crash to the fact that python keeps its gc list around.
The main reason I didn't go with a .so/.dll/.dylib that we could dlopen is from failing to figure out the build system on the various platforms. I was also initially trying to stick to compile once, run anywhere so we could deploy a single .net dll.
I tried and failed to figure out how to figure out how to allocate the page once, stuff the address somewhere, and reuse it later. By design you can't really do that in .net ; and python also doesn't see any reason to make that easy. (In py27 you can via Py_SetProgramHome and friends, but that's gross, doesn't work if anyone else is pulling the same hack, and fails in py3x where they copy the strings.)
@amos402 's trick of modifying python itself looks great if it can pass the cpython test suite and get past review on cpython. I love the C# monkey-patch for backporting the fix into already-deployed cpython. I'm just worried about unintended consequences.
|
Copy link
Copy Markdown
Member
|
I've triggered the build for #754 again, it seems to not be quite sound with regard to memory handling (though that doesn't necessarily have to be due to this PR). I'm currently leaning towards merging this one (#752 ) first such that we have a chance of preparing a 2.4.0 release before end of the year and afterwards look into #754 again.
@denfromufa @benoithudson @amos402 do you agree?
|
Copy link
Copy Markdown
Contributor
Author
|
@filmor : I'm in favour of your plan. I won't be sad if my code gets buried as soon as we have a better alternative.
|
Copy link
Copy Markdown
Member
I've triggered the build for #754 again, it seems to not be quite sound with regard to memory handling (though that doesn't necessarily have to be due to this PR). I'm currently leaning towards merging this one (#752 ) first such that we have a chance of preparing a 2.4.0 release before end of the year and afterwards look into #754 again.
@denfromufa @benoithudson @amos402 do you agree?
Yes. I still need more tests for fixing that PR.
|
Copy link
Copy Markdown
Contributor
filmor
merged commit 784190a
into
pythonnet : master
Oct 18, 2018
Copy link
Copy Markdown
Member
|
Thanks a lot for your and your colleagues efforts :)
|
benoithudson
deleted the
fix-domain-unload
branch
October 18, 2018 19:14
benoithudson
mentioned this pull request
Oct 23, 2018
Exception thrown when initializing Python .NET after a domain unload
#714
Closed
filmor
mentioned this pull request
Jun 20, 2019
ImportError: System.NotImplementedException: No support for armv7l
#873
Closed
creative-swamy
mentioned this pull request
Aug 28, 2019
Py.GIL crash fixes not working with Python3.5(conda), windows 10 Home edition
#949
Closed
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment
Labels
None yet
4 participants
|
|