-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
python: Fully destroy the intepreter on re-evaluation #6227
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
base: master
Are you sure you want to change the base?
Conversation
c8579cb to
f0056c2
Compare
|
would be awesome if you managed to really reinitizlize python without a segfault. |
|
What I can say is that this seems to work in basic tests. I haven't tried numpy at all. |
|
Yeah, (which is numpy/numpy#28271) |
|
The C documentation for
|
|
Sub-interpreters would be an option, but these are Python 3.12+ (released 2023). Any thoughts about adding that restriction to more recent versions of Python? |
Yes, fine by me. It will make some builds a bit more difficult and we probably need to disable python for older distributions. But if that means we get clean environments, that's worth it in my view. |
|
I noticed some build environments are newer, but the appimage builder image (manually uploaded by Torsten to Docker, I'm not sure from where) has something older than Python 3.13 (because my draft PR failed only for appimage), so you'll need to update at least that. But, you know, perfect enemy of the good and all. |
|
Yep, correct. This will need to build a newer Python for the AppImage build-env. https://github.com/openscad/openscad/wiki/Build-Matrix - but if this will allow clean restarts, it's very much worth the extra effort in my view. |
This builds a string that is not used anywhere, as far as I can tell.
|
I reworked this based on sub-interpreters. It turns out that sub-interpreters have been there forever, it is sub-interpreters with restrictions and independent GILs that are new to Python 3.12. |
|
Marked as draft because this makes the situation with numpy worse. Now it doesn't load at all, not even once. |
|
Right, a query for Py_mod_multiple_interpreters shows a lot of So is actually forking a separate process the only option? I suppose not supporting numpy would be a huge drawback for the specific use in OpenSCAD. Looking at numpy/numpy#24755 and numpy/numpy#28271 there's ongoing work towards sub-interpreters but it also sounds like there's still some way to go. |
|
Just randomly stumbled upon this issue. I have done the same thing for work one or two years ago and I can attest that it is simply impossible to initialize Python cleanly a second time in the same process (because of popular 3rd party libraries, Python itself is fine). Basically numpy people say nobody uses sub-interpreters, so no need to support them. And nobody uses sub-interpreters because numpy doesn't work in them, Catch 22. With Python 3.14 sub-interpreters came into the focus once more as they are now available within Python itself (previously one had to use the C API). Maybe that finally moves the goalpost so far that numpy has to get its act together 🤷♂️ |
|
I did a simple test and forking and running python against 2 different numpy versions (one installed via Debian, the other via venv) and that worked. Now the question would be what other problems will that drag in. It certainly makes other things more difficult, but as the forked process has all the other stuff too, maybe it's just a matter of getting the result back into the main process? |
|
I like the forking option as it removes the need to do
python-re-initialization . I believe we can even use the SAME memory for
parent and child and just need to ensure proper synchronization of data.
But I am not sure how that works with the Windows platform. Does Windows
API support "forking" ?
…On Tue, Oct 21, 2025 at 10:23 AM Torsten Paul ***@***.***> wrote:
*t-paul* left a comment (openscad/openscad#6227)
<#6227 (comment)>
I did a simple test and forking and running python against 2 different
numpy versions (one installed via Debian, the other via venv) and that
worked. Now the question would be what other problems will that drag in. It
certainly makes other things more difficult, but as the forked process has
all the other stuff too, maybe it's just a matter of getting the result
back into the main process?
—
Reply to this email directly, view it on GitHub
<#6227 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACCO4MVMNQHREHQRRJZP4E33YXUP5AVCNFSM6AAAAACHND74Z2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTIMRVGM3TOMBQGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I have seen a crash in the current code when importing a file that is not part of a package, like:
scad.py:
Would crash
openscadon re-evaluation of thescad.pyfile.The crashdump is in
initPython, somewhere where it tries to empty thesys.modulesdict:Proposed solution
Instead of trying to manually reinit the environment, which is messy and doesn't reverts all the side-effects that could have happened (like modifying
sys.pathabove), tear down the interpreter withPy_FinalizeEx(available since Python 3.6, released in 2016).Note that the patch is messy but trivial: it is literally just removing a bunch of code from
initPython. Look at it with "Hide whitespace" enabled in the diff view (orgit show -win the CLI).