[Bf-blender-cvs] [87d3f4aff32] blender-v2.92-release: Fix T82675: Crash on exit when Blender is built as a Python module

Campbell Barton noreply at git.blender.org
Thu Jan 28 04:55:57 CET 2021


Commit: 87d3f4aff3225104cbb8be41ac0339c6a1cd9a85
Author: Campbell Barton
Date:   Thu Jan 28 14:38:08 2021 +1100
Branches: blender-v2.92-release
https://developer.blender.org/rB87d3f4aff3225104cbb8be41ac0339c6a1cd9a85

Fix T82675: Crash on exit when Blender is built as a Python module

===================================================================

M	source/blender/python/intern/bpy_interface.c
M	source/blender/windowmanager/CMakeLists.txt
M	source/blender/windowmanager/intern/wm_init_exit.c

===================================================================

diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index c54c78ae389..64e992bd76f 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -442,6 +442,13 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
   py_tstate = PyGILState_GetThisThreadState();
   PyEval_ReleaseThread(py_tstate);
 #endif
+
+#ifdef WITH_PYTHON_MODULE
+  /* Disable all add-ons at exit, not essential, it just avoids resource leaks, see T71362. */
+  BPY_run_string_eval(C,
+                      (const char *[]){"atexit", "addon_utils", NULL},
+                      "atexit.register(addon_utils.disable_all)");
+#endif
 }
 
 void BPY_python_end(void)
diff --git a/source/blender/windowmanager/CMakeLists.txt b/source/blender/windowmanager/CMakeLists.txt
index 6de36acb343..0f26ec50816 100644
--- a/source/blender/windowmanager/CMakeLists.txt
+++ b/source/blender/windowmanager/CMakeLists.txt
@@ -168,6 +168,9 @@ if(WITH_PYTHON)
     ../python
   )
   add_definitions(-DWITH_PYTHON)
+  if(WITH_PYTHON_MODULE)
+    add_definitions(-DWITH_PYTHON_MODULE)
+  endif()
 endif()
 
 if(WITH_BUILDINFO)
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 608aa116239..918836041df 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -523,11 +523,15 @@ void WM_exit_ex(bContext *C, const bool do_python)
     }
   }
 
-#ifdef WITH_PYTHON
+#if defined(WITH_PYTHON) && !defined(WITH_PYTHON_MODULE)
   /* Without this, we there isn't a good way to manage false-positive resource leaks
    * where a #PyObject references memory allocated with guarded-alloc, T71362.
    *
-   * This allows add-ons to free resources when unregistered (which is good practice anyway). */
+   * This allows add-ons to free resources when unregistered (which is good practice anyway).
+   *
+   * Don't run this code when built as a Python module as this runs when Python is in the
+   * process of shutting down, where running a snippet like this will crash, see T82675.
+   * Instead use the `atexit` module, installed by #BPY_python_start */
   BPY_run_string_eval(C, (const char *[]){"addon_utils", NULL}, "addon_utils.disable_all()");
 #endif



More information about the Bf-blender-cvs mailing list