mirror of
https://github.com/blender/blender
synced 2026-09-27 01:34:15 +03:00
In the #148959 report, it's possible to prevent the `bpy` module being freed when a property is stored in a Blender type. From investigating the issue this doesn't seem to be a reference counting issue - and the object is reachable via the GC (although bpy.types should be extended to visit all types, this doesn't fix the bug). Workaround the issue by unloading Blender when the modules are cleared. This is more reliable since Python modules overwrite all modules values on exit, whereas calling the `m_free` or `m_clear` isn't guaranteed (see Python documentation **Bugs and caveats** section of `Py_FinalizeEx` docs). This only impacts Blender when it is built as a Python module. Note that clearing Blender could be performed earlier using `atexit` but this risks order-of-deinitialization issues if Blender is freed while PyObject's remain which reference Blender data. ---- See: **Bugs and caveats:** in https://docs.python.org/3/c-api/init.html#c.Py_FinalizeEx which states: > The destruction of modules and objects in modules is done in random order; this may cause destructors (`__del__()` methods) to fail when they depend on other objects (even functions) or modules. **Dynamically loaded extension modules loaded by Python are not unloaded.** Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). **Memory tied up in circular references between objects is not freed.** Interned strings will all be deallocated regardless of their reference count. **Some memory allocated by extension modules may not be freed.** --- - It looks like this caveat may cause #148959: **Memory tied up in circular references between objects is not freed.** ...since it doesn't seem like a resource leak or GC error. - From reading the CPython 3.11 module cleanup code: dictionaries are now ordered and the code takes advantage of that, I'd guess the Python developers don't want to make the order of freeing part of the API spec. Co-authored-by: Brecht Van Lommel <brecht@blender.org> Pull Request: https://projects.blender.org/blender/blender/pulls/149162
25 lines
859 B
CMake
25 lines
859 B
CMake
# SPDX-FileCopyrightText: 2021 Blender Authors
|
|
#
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
function(add_blender_as_python_module_test testname testscript)
|
|
if(NOT TEST_PYTHON_EXE)
|
|
message(FATAL_ERROR "No Python configured for running tests, set TEST_PYTHON_EXE.")
|
|
endif()
|
|
|
|
add_test(
|
|
NAME ${testname}
|
|
COMMAND ${TEST_PYTHON_EXE} ${TEST_PYTHON_EXE_EXTRA_ARGS} ${CMAKE_CURRENT_LIST_DIR}/${testscript} ${ARGN}
|
|
)
|
|
|
|
# On macOS, asan library must be loaded early.
|
|
if(APPLE AND WITH_COMPILER_ASAN)
|
|
set_tests_properties(
|
|
${testname}
|
|
PROPERTIES ENVIRONMENT DYLD_INSERT_LIBRARIES=${COMPILER_ASAN_LIBRARY}
|
|
)
|
|
endif()
|
|
endfunction()
|
|
|
|
add_blender_as_python_module_test(import_bpy import_bpy.py ${CMAKE_INSTALL_PREFIX_WITH_CONFIG})
|
|
add_blender_as_python_module_test(cleanup_bpy cleanup_bpy.py ${CMAKE_INSTALL_PREFIX_WITH_CONFIG})
|