Regression: Crash when running modal operator in load_post handler #107759

Closed
opened 2023-05-08 21:39:04 +02:00 by Ryan Guy · 6 comments

System Information
Operating system: Windows-10-10.0.22621-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 4090/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 528.49

Blender Version
Broken: version: 3.6.0 Alpha, branch: main, commit date: 2023-05-08 10:47, hash: f6b492c43dbd
Worked: 3.5.0, branch: blender-v3.5-release, commit date: 2023-03-29 02:56, hash: 1be25cfff18b

Caused by 46be42f6b1

Short description of error

I have an addon that starts running a modal timer operator within the load_post handler. In Blender 3.6, this results in a crash. This has worked without crash in previous versions of Blender 3.5 or earlier.

Here is a minimal addon script that uses the operator_modal_timer.py template operator:

bl_info = {
    "name": "Modal Timer Add-on",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy

class ModalTimerOperator(bpy.types.Operator):
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None

    def modal(self, context, event):
        if event.type == 'TIMER':
            print("Running Modal...")
        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(0.5, window=context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)

@bpy.app.handlers.persistent
def load_post_func(scene):
    print("Load Post")
    bpy.ops.wm.modal_timer_operator()
    
def register():
    bpy.utils.register_class(ModalTimerOperator)
    bpy.app.handlers.load_post.append(load_post_func)

def unregister():
    bpy.utils.unregister_class(ModalTimerOperator)
    bpy.app.handlers.load_post.remove(load_post_func)

This is the EXCEPTION_ACCESS_VIOLATION message:

Error   : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FF715339725
Module  : blender.exe
Thread  : 000012dc
Writing: C:\Users\ryanl\AppData\Local\Temp\blender.crash.txt

Crash log: blender.crash.txt

If the modal operator is run using bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT'), a crash does not occur, but the following error message is displayed which prevents the operator from running:

Traceback (most recent call last):
  File "C:\Users\ryanl\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\modal_timer_addon.py", line 33, in load_post_func
    bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT')
  File "C:\Program Files\blender\3.6.0-alpha+main.f6b492c43dbd-windows.amd64-release\3.6\scripts\modules\bpy\ops.py", line 111, in __call__
    ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.wm.modal_timer_operator.poll() Missing 'window' in context

Exact steps for others to reproduce the error

  1. Open Blender
  2. Install and enable the modal_timer_addon.py` addon
  3. Open a Blend file to run the load_post handler (File > New > General)

Result: Blender crash

**System Information** Operating system: Windows-10-10.0.22621-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 4090/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 528.49 **Blender Version** Broken: version: 3.6.0 Alpha, branch: main, commit date: 2023-05-08 10:47, hash: `f6b492c43dbd` Worked: 3.5.0, branch: blender-v3.5-release, commit date: 2023-03-29 02:56, hash: `1be25cfff18b` Caused by 46be42f6b16314f59a37ebb430d77d12e7a88461 **Short description of error** I have an addon that starts running a modal timer operator within the ```load_post``` handler. In Blender 3.6, this results in a crash. This has worked without crash in previous versions of Blender 3.5 or earlier. Here is a minimal addon script that uses the ```operator_modal_timer.py``` template operator: ```python bl_info = { "name": "Modal Timer Add-on", "blender": (2, 80, 0), "category": "Object", } import bpy class ModalTimerOperator(bpy.types.Operator): bl_idname = "wm.modal_timer_operator" bl_label = "Modal Timer Operator" _timer = None def modal(self, context, event): if event.type == 'TIMER': print("Running Modal...") return {'PASS_THROUGH'} def execute(self, context): wm = context.window_manager self._timer = wm.event_timer_add(0.5, window=context.window) wm.modal_handler_add(self) return {'RUNNING_MODAL'} def cancel(self, context): wm = context.window_manager wm.event_timer_remove(self._timer) @bpy.app.handlers.persistent def load_post_func(scene): print("Load Post") bpy.ops.wm.modal_timer_operator() def register(): bpy.utils.register_class(ModalTimerOperator) bpy.app.handlers.load_post.append(load_post_func) def unregister(): bpy.utils.unregister_class(ModalTimerOperator) bpy.app.handlers.load_post.remove(load_post_func) ``` This is the EXCEPTION_ACCESS_VIOLATION message: ``` Error : EXCEPTION_ACCESS_VIOLATION Address : 0x00007FF715339725 Module : blender.exe Thread : 000012dc Writing: C:\Users\ryanl\AppData\Local\Temp\blender.crash.txt ``` Crash log: [blender.crash.txt](/attachments/2a08cf98-868c-431a-9cee-4d178d9f9c02) If the modal operator is run using ```bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT')```, a crash does not occur, but the following error message is displayed which prevents the operator from running: ``` Traceback (most recent call last): File "C:\Users\ryanl\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\modal_timer_addon.py", line 33, in load_post_func bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT') File "C:\Program Files\blender\3.6.0-alpha+main.f6b492c43dbd-windows.amd64-release\3.6\scripts\modules\bpy\ops.py", line 111, in __call__ ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo) RuntimeError: Operator bpy.ops.wm.modal_timer_operator.poll() Missing 'window' in context ``` **Exact steps for others to reproduce the error** 1. Open Blender 2. Install and enable the [modal_timer_addon.py](/attachments/7159bf7c-397e-4147-892b-ded1739785bd)` addon 3. Open a Blend file to run the ```load_post``` handler (File > New > General) Result: Blender crash
Ryan Guy added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-05-08 21:39:04 +02:00
Iliya Katushenock changed title from EXCEPTION_ACCESS_VIOLATION crash when running modal operator in load_post handler in Blender 3.6 to Regression: Crash when running modal operator in load_post handler 2023-05-08 21:39:56 +02:00
Iliya Katushenock added the
Interest
Python API
Interest
Undo
labels 2023-05-08 21:40:08 +02:00
Member

From the look of it, you may need to copy a context beforehand and pass it to the invocation, otherwise the window/windowmanager might not be valid. The crash seems to be a null window manager at the time your operator is called. Not sure why previously it worked.

From the look of it, you may need to copy a context beforehand and pass it to the invocation, otherwise the window/windowmanager might not be valid. The crash seems to be a null window manager at the time your operator is called. Not sure why previously it worked.
Author

Thanks for the tip! It appears that bpy.context.window is None at the time of load_post in Blender 3.6, but is not None in earlier versions.

I'm not very familiar with how overriding the context works, but this seems to work as a way to call the operator:

def load_post_func(scene):
    with bpy.context.temp_override(window=bpy.context.window_manager.windows[0]):
        bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT')
Thanks for the tip! It appears that ```bpy.context.window``` is None at the time of ```load_post``` in Blender 3.6, but is not None in earlier versions. I'm not very familiar with how overriding the context works, but this seems to work as a way to call the operator: ```python def load_post_func(scene): with bpy.context.temp_override(window=bpy.context.window_manager.windows[0]): bpy.ops.wm.modal_timer_operator('INVOKE_DEFAULT') ```
Member

@rlguy If that solves your need then it's good. I'll tag scripting module and see if the developers have additional input on whether this is intentional or accidental.

@rlguy If that solves your need then it's good. I'll tag scripting module and see if the developers have additional input on whether this is intentional or accidental.
YimingWu added
Module
Python API
Status
Needs Info from Developers
and removed
Status
Needs Triage
labels 2023-05-09 09:33:36 +02:00
Member

I'll check when this broke.

I'll check when this broke.
Member

Caused by 46be42f6b1

CC @ideasman42

Caused by 46be42f6b16314f59a37ebb430d77d12e7a88461 CC @ideasman42
Philipp Oeser added
Priority
High
Status
Confirmed
and removed
Priority
Normal
Status
Needs Info from Developers
labels 2023-05-09 14:38:56 +02:00
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2023-05-12 09:29:25 +02:00

Will reopen as the fix caused some other issues and needed to be reverted.

Will reopen as the fix caused some other issues and needed to be reverted.
Blender Bot added
Status
Needs Triage
and removed
Status
Resolved
labels 2023-05-12 20:36:45 +02:00
Blender Bot added
Status
Resolved
and removed
Status
Needs Triage
labels 2023-05-13 09:55:51 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107759
No description provided.