Page MenuHome

2.8: Python API: crash due to missing window context in Application timer
Open, Confirmed, LowPublic

Description

System Information
Operating system: Windows 7, 64bit
Graphics card: N/A

Blender Version
Broken:
2.80, dd9cedddae69, win64

Short description of error

Error : EXCEPTION_ACCESS_VIOLATION
Address : 0x000000013F495468
Module : D:\Apps\blender-2.80.0-git.dd9cedddae69-windows64\blender.exe

This is related to T62051.

Exact steps for others to reproduce the error

import bpy


def add_cylinder():
    bpy.ops.mesh.primitive_cylinder_add(
        radius=1, depth=1,
        location=(1.5, 0.0, 1),
    )
    
    obj1 = bpy.context.scene.view_layers[0].objects.active
    obj1.select_set(True)
    bpy.ops.mesh.primitive_cylinder_add(
        radius=2, depth=1,
        location=(1.5, 0.0, 1),
    )
    obj2 = bpy.context.scene.view_layers[0].objects.active
    obj2.select_set(True)
    print(bpy.ops.object.join.poll())
    window = bpy.context.window_manager.windows[0]
    
    obs = [obj1, obj2]
    ctx = {'window': window, 'screen': window.screen}  
    ctx['object'] = obs[0]
    ctx['active_object'] = obs[0]
    ctx['selected_objects'] = obs
    ctx['selected_editable_objects'] = obs
    bpy.ops.object.mode_set(ctx, mode='OBJECT')
    bpy.ops.object.join(ctx)
    
    
# add_cylinder()   # this works
bpy.app.timers.register(add_cylinder)

Details

Type
Bug

Event Timeline

Marc (schlamar) renamed this task from 2.8: Python API: Access violoation calling bpy.ops.object.mode_set in Application timer to 2.8: Python API: Access violation calling bpy.ops.object.mode_set in Application timer.Mar 1 2019, 8:54 AM

System Information:
OS: Arch Linux with Xorg.

Blender Version:
Blender 2.80 (sub 45), Commit date: 2019-03-01 22:04, Hash 84820e7f5806

Short description of error:
Invoking a window manager popup menu through a timer results in a segmentation fault and a crash. It seems the context window is null at this point.

Exact steps for others to reproduce the error:

A minimal working script that produces the problem:

import bpy

def in_5_seconds():
    bpy.context.window_manager.popup_menu(lambda self, context: None)

bpy.app.timers.register(in_5_seconds, first_interval=5)

Backtrace:

./blender(BLI_system_backtrace+0x1d) [0x1bb4bdd]
./blender() [0x115ad19]
/usr/lib/libc.so.6(+0x37e00) [0x7f70b7b17e00]
./blender(WM_window_pixels_x+0x4) [0x117f8f4]
./blender() [0x12fb565]
./blender(UI_block_begin+0x11d) [0x12fe34d]
./blender(UI_popup_menu_begin_ex+0x55) [0x1336b65]
./blender(WindowManager_popmenu_begin__internal_call+0x13) [0x1b317c3]
./blender(RNA_function_call+0x12) [0x1a437d2]
./blender() [0x150f74a]
./blender(_PyObject_FastCallKeywords+0x104) [0x2f8f784]
./blender(_PyEval_EvalFrameDefault+0x465c) [0x115272c]
./blender(_PyEval_EvalCodeWithName+0xae3) [0x3043973]
./blender(_PyFunction_FastCallKeywords+0xa6) [0x2f8f0e6]
./blender(_PyEval_EvalFrameDefault+0x79c3) [0x1155a93]
./blender() [0x114d020]
./blender(_PyFunction_FastCallDict+0x2a6) [0x2f8f026]
./blender() [0x15175da]
./blender(BLI_timer_execute+0x4a) [0x1b72b2a]
./blender(wm_event_do_notifiers+0x2e) [0x11655fe]
./blender(WM_main+0x28) [0x115b738]
./blender(main+0x30a) [0x10c123a]
/usr/lib/libc.so.6(__libc_start_main+0xf3) [0x7f70b7b04223]
./blender(_start+0x2a) [0x115769a]
Jacques Lucke (JacquesLucke) lowered the priority of this task from Needs Triage by Developer to Confirmed, Low.Mar 4 2019, 5:22 PM

Can reproduce the crash.
Setting the priority to low for now.

As you've guessed correctly, there is no window context when the timer executes (obviously a window is needed to open a popup in Blender).

Can you check if it is possible to open the popup from within another operator which is called in the timer?
You need to pass some extra context into the operator as shown in T62078.

@Jacques Lucke (JacquesLucke) Yes, the following script works as expected:

import bpy

class TestOperator(bpy.types.Operator):
    bl_idname = "scene.test_operator"
    bl_label = "Test Operator"

    def execute(self, context):
        context.window_manager.popup_menu(lambda self, context: None)
        return {'FINISHED'}

bpy.utils.register_class(TestOperator)

def in_5_seconds():
    window = bpy.context.window_manager.windows[0]
    ctx = {'window': window, 'screen': window.screen}  
    bpy.ops.scene.test_operator(ctx)

bpy.app.timers.register(in_5_seconds, first_interval=5)

Since few days (around 11.june) timers will crash, if call for operator is performed inside. Simple blend file, just run script, and it will crash on line: 42

Blender Version: Broken: version: 2.80 (sub 74), branch: blender2.7, commit date: 2019-06-12 18:00, hash: rB2404220e80a9

def every_2_seconds():
    ctx = get_default_context()
    bpy.ops.object.simple_operator(ctx) #crash here
    return 2.0

Philipp Oeser (lichtwerk) renamed this task from 2.8: Python API: Access violation calling bpy.ops.object.mode_set in Application timer to 2.8: Python API: crash due to missing window context in Application timer.Tue, Nov 5, 10:39 AM