Fix T80694: Crash reloading scripts from the Python console

Running `bpy.ops.script.reload()` from Python was crashing
since the operator being called was it's self freed.

Change the reload operator to defer execution - as supporting
re-registration during execution is quite involved for a corner-case.
This commit is contained in:
Campbell Barton 2020-09-14 17:50:01 +10:00 committed by Jeroen Bakker
parent 2c8f8765a1
commit 27ea086242
Notes: blender-bot 2023-02-14 09:24:53 +01:00
Referenced by issue #81112, Reloading Scripts causes SyntaxError (error only in 2.90.1, fix ready)
Referenced by issue #80694, bpy.ops.script.reload() seg fault
3 changed files with 39 additions and 8 deletions

View File

@ -283,6 +283,8 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
del _initialize
if reload_scripts:
_bpy.context.window_manager.tag_script_reload()
import gc
print("gc.collect() -> %d" % gc.collect())

View File

@ -115,15 +115,32 @@ static int script_reload_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
WM_script_tag_reload();
/* TODO(campbell): this crashes on netrender and keying sets, need to look into why
* disable for now unless running in debug mode. */
/* It would be nice if we could detect when this is called from the Python
* only postponing in that case, for now always do it. */
if (true) {
/* Postpone when called from Python so this can be called from an operator
* that might be re-registered, crashing Blender when we try to read from the
* freed operator type which, see T80694. */
BPY_execute_string(C,
(const char *[]){"bpy", NULL},
"def fn():\n"
" bpy.utils.load_scripts(reload_scripts=True)\n"
" return None\n"
"bpy.app.timers.register(fn)");
}
else {
WM_cursor_wait(true);
BPY_execute_string(
C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
WM_cursor_wait(false);
}
/* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
* any additional updates required by this operator should go there. */
/* TODO, this crashes on netrender and keying sets, need to look into why
* disable for now unless running in debug mode */
WM_cursor_wait(1);
BPY_execute_string(
C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
WM_cursor_wait(0);
WM_event_add_notifier(C, NC_WINDOW, NULL);
return OPERATOR_FINISHED;
#else
UNUSED_VARS(C, op);

View File

@ -558,6 +558,12 @@ static void rna_WindowManager_print_undo_steps(wmWindowManager *wm)
BKE_undosys_print(wm->undo_stack);
}
static void rna_WindowManager_tag_script_reload(void)
{
WM_script_tag_reload();
WM_main_add_notifier(NC_WINDOW, NULL);
}
static PointerRNA rna_WindoManager_operator_properties_last(const char *idname)
{
wmOperatorType *ot = WM_operatortype_find(idname, true);
@ -913,6 +919,12 @@ void RNA_api_wm(StructRNA *srna)
RNA_def_function(srna, "print_undo_steps", "rna_WindowManager_print_undo_steps");
/* Used by (#SCRIPT_OT_reload). */
func = RNA_def_function(srna, "tag_script_reload", "rna_WindowManager_tag_script_reload");
RNA_def_function_ui_description(
func, "Tag for refreshing the interface after scripts have been reloaded");
RNA_def_function_flag(func, FUNC_NO_SELF);
parm = RNA_def_property(srna, "is_interface_locked", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_ui_text(
parm,