Python Operators that add/remove ID data without an undo step crash Blender #77557

Open
opened 2020-06-07 06:38:16 +02:00 by Radu Popovici · 51 comments

System Information
Operating system: Win 10
Graphics card: GTX 1070

Blender Version
Broken: 2.8.3, 2.9 aed11c673e
Worked: 2.8.0

Undoing after object linking to the scene crashes blender. I used the latest daily build

Exact steps for others to reproduce the error

  1. Load the crash blend file crash_modal.blend
  2. Run the script attached
  3. Click in viewport to add a new object, and then press ESC
  4. Press Ctrl+Z to undo
  5. Crash..
**System Information** Operating system: Win 10 Graphics card: GTX 1070 **Blender Version** Broken: 2.8.3, 2.9 aed11c673efe Worked: 2.8.0 Undoing after object linking to the scene crashes blender. I used the latest daily build **Exact steps for others to reproduce the error** 1. Load the crash blend file [crash_modal.blend](https://archive.blender.org/developer/F8588514/crash_modal.blend) 2. Run the script attached 3. Click in viewport to add a new object, and then press ESC 4. Press Ctrl+Z to undo 5. Crash..
Author

Added subscriber: @rpopovici

Added subscriber: @rpopovici

#92094 was marked as duplicate of this issue

#92094 was marked as duplicate of this issue

#82379 was marked as duplicate of this issue

#82379 was marked as duplicate of this issue

#81883 was marked as duplicate of this issue

#81883 was marked as duplicate of this issue

#81346 was marked as duplicate of this issue

#81346 was marked as duplicate of this issue

#65681 was marked as duplicate of this issue

#65681 was marked as duplicate of this issue

#80759 was marked as duplicate of this issue

#80759 was marked as duplicate of this issue

#79808 was marked as duplicate of this issue

#79808 was marked as duplicate of this issue

#77595 was marked as duplicate of this issue

#77595 was marked as duplicate of this issue
Author

Stack trace:
blender.exe :0x00007FF6A2A2C260 BKE_scene_set_background
blender.exe :0x00007FF6A555F220 setup_app_data
blender.exe :0x00007FF6A555E600 BKE_blendfile_read_from_memfile
blender.exe :0x00007FF6A557B740 BKE_memfile_undo_decode
blender.exe :0x00007FF6A31362B0 memfile_undosys_step_decode
blender.exe :0x00007FF6A555E2F0 undosys_step_decode
blender.exe :0x00007FF6A555DEF0 BKE_undosys_step_undo_with_data_ex
blender.exe :0x00007FF6A3137540 ed_undo_step_impl
blender.exe :0x00007FF6A3137300 ed_undo_exec
blender.exe :0x00007FF6A2BC77A0 wm_operator_invoke
blender.exe :0x00007FF6A2BC5630 wm_handler_operator_call
blender.exe :0x00007FF6A2BC6CE0 wm_handlers_do_keymap_with_keymap_handler
blender.exe :0x00007FF6A2BC65B0 wm_handlers_do_intern
blender.exe :0x00007FF6A2BC5BE0 wm_handlers_do
blender.exe :0x00007FF6A2BC3260 wm_event_do_handlers
blender.exe :0x00007FF6A2BAEDC0 WM_main
blender.exe :0x00007FF6A293A530 main

Stack trace: blender.exe :0x00007FF6A2A2C260 BKE_scene_set_background blender.exe :0x00007FF6A555F220 setup_app_data blender.exe :0x00007FF6A555E600 BKE_blendfile_read_from_memfile blender.exe :0x00007FF6A557B740 BKE_memfile_undo_decode blender.exe :0x00007FF6A31362B0 memfile_undosys_step_decode blender.exe :0x00007FF6A555E2F0 undosys_step_decode blender.exe :0x00007FF6A555DEF0 BKE_undosys_step_undo_with_data_ex blender.exe :0x00007FF6A3137540 ed_undo_step_impl blender.exe :0x00007FF6A3137300 ed_undo_exec blender.exe :0x00007FF6A2BC77A0 wm_operator_invoke blender.exe :0x00007FF6A2BC5630 wm_handler_operator_call blender.exe :0x00007FF6A2BC6CE0 wm_handlers_do_keymap_with_keymap_handler blender.exe :0x00007FF6A2BC65B0 wm_handlers_do_intern blender.exe :0x00007FF6A2BC5BE0 wm_handlers_do blender.exe :0x00007FF6A2BC3260 wm_event_do_handlers blender.exe :0x00007FF6A2BAEDC0 WM_main blender.exe :0x00007FF6A293A530 main
Author

Migth be related to this #77595

Migth be related to this #77595

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

This script has two errors, it's returning CANCELLED instead of FINISHED even though it did change data.

And it does not specify UNDO to its bl_options flags.

@ideasman42 looks like py-defined operators have no UNDO flag by default, think this is no longer acceptable? Any change to data without a proper undo step was (very) bad before, but now it is a guaranteed crash.

This script has two errors, it's returning `CANCELLED` instead of `FINISHED` even though it did change data. And it does not specify `UNDO` to its `bl_options` flags. @ideasman42 looks like py-defined operators have no `UNDO` flag by default, think this is no longer acceptable? Any change to data without a proper undo step was (very) bad before, but now it is a guaranteed crash.

Added subscriber: @Zuorion-4

Added subscriber: @Zuorion-4
Author

@mont29 I am doing something very similar to this script in a different project, unrelated to modal operators, and it's crashing when linking to the scene. I can't share that code.. but what I can say is that I am using depsgraph_update_handler_post() to create and link the objects to the scene.
Maybe introducing new API calls similar to bpy.ops.ed.undo_push() in order to cut new UNDO checkpoints can be a solution for user defined scripts.. Then, they can decide when to cut an check point and when to ignore it.
Anyway, I think is very unusual to crash blender only because you missed an UNDO checkpoint

@mont29 I am doing something very similar to this script in a different project, unrelated to modal operators, and it's crashing when linking to the scene. I can't share that code.. but what I can say is that I am using depsgraph_update_handler_post() to create and link the objects to the scene. Maybe introducing new API calls similar to bpy.ops.ed.undo_push() in order to cut new UNDO checkpoints can be a solution for user defined scripts.. Then, they can decide when to cut an check point and when to ignore it. Anyway, I think is very unusual to crash blender only because you missed an UNDO checkpoint

Closed as duplicate of #77460

Closed as duplicate of #77460

Changed status from 'Duplicate' to: 'Confirmed'

Changed status from 'Duplicate' to: 'Confirmed'

This is cost of new speedy undo code, it expect current datablocks to match those of current undo step, otherwise... bad things will happen

This is cost of new speedy undo code, it expect current datablocks to match those of current undo step, otherwise... bad things will happen
Author

I agree that performance is important, but this cannot stay the way it is now. 'Fast crashing' blender is not a solution for anyone :) I am pretty sure there are many important third party plugins rendered unusable by this change. What can be done to fix it?

I agree that performance is important, but this cannot stay the way it is now. 'Fast crashing' blender is not a solution for anyone :) I am pretty sure there are many important third party plugins rendered unusable by this change. What can be done to fix it?
Author

Calling the esoteric bpy.ops.ed.undo_push() after object link to the scene, appears to be working in this particular case..

Calling the esoteric bpy.ops.ed.undo_push() after object link to the scene, appears to be working in this particular case..

@rpopovici I already told you above what you need to do to fix that operator:

In #77557#948582, @mont29 wrote:
This script has two errors, it's returning CANCELLED instead of FINISHED even though it did change data.

And it does not specify UNDO to its bl_options flags.

Only thing at stake here, is whether we should enable undo by default for py operators (I think we should, but would like to have @ideasman42 advice on this as well).

@rpopovici I already told you above what you need to do to fix that operator: > In #77557#948582, @mont29 wrote: > This script has two errors, it's returning `CANCELLED` instead of `FINISHED` even though it did change data. > > And it does not specify `UNDO` to its `bl_options` flags. Only thing at stake here, is whether we should enable undo by default for py operators (I think we should, but would like to have @ideasman42 advice on this as well).
Author

@mont29 yes that works if you use operators to create stuff, but if you don't add new objects with a custom operator then adding the UNDO flag + FINISHED is not an option. Adding new objects can happen on any callback handler: timer handler, deps graph update handler, nodes updates, even the undo handler

@mont29 yes that works if you use operators to create stuff, but if you don't add new objects with a custom operator then adding the UNDO flag + FINISHED is not an option. Adding new objects can happen on any callback handler: timer handler, deps graph update handler, nodes updates, even the undo handler

@rpopovici adding or removing objects (or any other datablock) in handlers is not a good idea nor practice, not at all. It might work in some cases, but this is typically things that are very likely to cause problems one way or the other.

Our python API is very permissive, which makes it powerful and quiet efficient, but also very easy to shoot yourself in the foot and crash blender in many imaginative ways...

@rpopovici adding or removing objects (or any other datablock) in handlers is not a good idea nor practice, not at all. It might work in some cases, but this is typically things that are very likely to cause problems one way or the other. Our python API is very permissive, which makes it powerful and quiet efficient, but also very easy to shoot yourself in the foot and crash blender in many imaginative ways...
Author

OK then, what exactly can I do from a handler? Is it safe at least to call custom operators from handlers? And then I can create objects from inside the execute method of these ops..

OK then, what exactly can I do from a handler? Is it safe at least to call custom operators from handlers? And then I can create objects from inside the execute method of these ops..
Author

Using application timers, execution queues and custom ops to react to events on main thread appears to be the acceptable solution to my problem, according to the docs https://docs.blender.org/api/current/bpy.app.timers.html

Using application timers, execution queues and custom ops to react to events on main thread appears to be the acceptable solution to my problem, according to the docs https://docs.blender.org/api/current/bpy.app.timers.html

@mont29, there was a similar report I replied to recently but I cant find it now.

Operators that don't set the undo flag but add/remove library data are going to crash.

A fix for this would be to detect the Main data-base as being modified, then add an undo step even if the operator isn't marked to undo.

@mont29, there was a similar report I replied to recently but I cant find it now. Operators that don't set the undo flag but add/remove library data are going to crash. A fix for this would be to detect the Main data-base as being modified, then add an undo step even if the operator isn't marked to undo.

Removed subscriber: @Zuorion-4

Removed subscriber: @Zuorion-4
Author

@ideasman42 batching the unexpected undo step into the previous one would feel more natural than if a new step is created. If you create a new undesired step, that could be cause for confusion
when ctrl+z is pressed and you have to press it multiple times to undo the unexpected steps.
And also this would be more consistent with the current definition of what UNDO flag means. basicaly, you are asking for a new undo step to be created whan an UNDO flag is present, and if none is pressent you want the last changes to be merged into the last existing step.
I think that this approach could address many of the UNDO related issues in blender and besided that it could be beneficial when post event operations are performed to be linked to the last undo step..

@ideasman42 batching the unexpected undo step into the previous one would feel more natural than if a new step is created. If you create a new undesired step, that could be cause for confusion when ctrl+z is pressed and you have to press it multiple times to undo the unexpected steps. And also this would be more consistent with the current definition of what UNDO flag means. basicaly, you are asking for a new undo step to be created whan an UNDO flag is present, and if none is pressent you want the last changes to be merged into the last existing step. I think that this approach could address many of the UNDO related issues in blender and besided that it could be beneficial when post event operations are performed to be linked to the last undo step..

@rpopovici the undo step can be added without it showing up to the user, that's supported already.

@rpopovici the undo step can be added without it showing up to the user, that's supported already.
Author

@ideasman42 how? Can I access this feature from python? is it possible to add an undo step which will be jumped(undo multiple steps in one) when Ctrl+z is pressed?

@ideasman42 how? Can I access this feature from python? is it possible to add an undo step which will be jumped(undo multiple steps in one) when Ctrl+z is pressed?
Author

As a side node here and potential workaround for anyone facing a similar problem: you can avoid this crash by unlinking post-event generated objects in undo_pre handler, and then link it back in undo_post

As a side node here and potential workaround for anyone facing a similar problem: you can avoid this crash by unlinking post-event generated objects in undo_pre handler, and then link it back in undo_post

Added subscriber: @oweissbarth

Added subscriber: @oweissbarth

Added subscribers: @Zweistein, @lichtwerk

Added subscribers: @Zweistein, @lichtwerk
Campbell Barton changed title from UNDO crash in bledner 2.9(daily) while linking new object to the scene to Python Operators that add/remove ID data without an undo step crash Blender 2020-09-11 06:16:19 +02:00

Added subscriber: @jeacom

Added subscriber: @jeacom

Added subscribers: @kursadk, @JosephEagar

Added subscribers: @kursadk, @JosephEagar
Member

Added subscriber: @wbmoss_dev

Added subscriber: @wbmoss_dev

Added subscriber: @pauanyu_blender

Added subscriber: @pauanyu_blender
Member

Added subscriber: @Macroni

Added subscriber: @Macroni

Added subscriber: @MACHIN3

Added subscriber: @MACHIN3

Added subscriber: @ckohl_art

Added subscriber: @ckohl_art

Added subscriber: @Eric-Womer

Added subscriber: @Eric-Womer
I can confirm this is still an issue in 2.83.8 and 2.91.0. 2.83.8: https://gist.github.com/ericwomer/4dc07499dc2490ac0993767579428159 2.91.0: https://gist.github.com/ericwomer/26ad866eda59c4c3ae831c382ef449a8

Added subscriber: @rjg

Added subscriber: @rjg

Just a heads up there are several add-ons shipped with Blender that have operators without "UNDO" in their bl_options = {"REGISTER"}. Not every one of them creates geometry, but they should all be checked and the option included where needed. For example the Discombombulator add-on, definitely has this bug (see blender/blender-addons#82763).

Just a heads up there are several add-ons shipped with Blender that have operators without `"UNDO"` in their `bl_options = {"REGISTER"}`. Not every one of them creates geometry, but they should all be checked and the option included where needed. For example the Discombombulator add-on, definitely has this bug (see blender/blender-addons#82763).

@rjg Just wanted to comment as well, in case this is in response to Eric, whom I've sent here. His crashes are related to adding/removing ID data using layout.template_icon_view and so changing ID data from enum update callbacks. This is still unresolved, yet my reports about it were all closed.
I was told conflicting information: 1. Don't change ID data from update callbacks and don't run operators from them, 2. If you change ID data use operators. Following this advice means the template_icon_view layout element has become useless for its most popular use case: adding external assets. Since Blender is crashing when undoing after such an operation, it also risks data loss for a huge amount of Blender users.

@rjg Just wanted to comment as well, in case this is in response to Eric, whom I've sent here. His crashes are related to adding/removing ID data using `layout.template_icon_view` and so changing ID data from enum update callbacks. This is still unresolved, yet my reports about it were all closed. I was told conflicting information: 1. Don't change ID data from update callbacks and don't run operators from them, 2. If you change ID data use operators. Following this advice means the `template_icon_view` layout element has become useless for its most popular use case: adding external assets. Since Blender is crashing when undoing after such an operation, it also risks data loss for a huge amount of Blender users.
Member

Added subscriber: @Imaginer

Added subscriber: @Imaginer

@MACHIN3 No, this wasn't a reply to Eric. I've just stumbled upon the case where the bl_options weren't properly set in an add-on. If I have time I'll take a look at your ticket later this week.

@MACHIN3 No, this wasn't a reply to Eric. I've just stumbled upon the case where the `bl_options` weren't properly set in an add-on. If I have time I'll take a look at your ticket later this week.

They could tag this as a known issue already.

They could tag this as a known issue already.

Added subscriber: @PhilippeCrassous

Added subscriber: @PhilippeCrassous
Member

Added subscriber: @1029910278

Added subscriber: @1029910278
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:42 +01: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
16 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#77557
No description provided.