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

Open
opened 3 years ago by rpopovici · 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..
Poster

Added subscriber: @rpopovici

Added subscriber: @rpopovici
Collaborator

#92094 was marked as duplicate of this issue

#92094 was marked as duplicate of this issue
Collaborator

#82379 was marked as duplicate of this issue

#82379 was marked as duplicate of this issue
Collaborator

#81883 was marked as duplicate of this issue

#81883 was marked as duplicate of this issue
Collaborator

#81346 was marked as duplicate of this issue

#81346 was marked as duplicate of this issue
Collaborator

#65681 was marked as duplicate of this issue

#65681 was marked as duplicate of this issue
Collaborator

#80759 was marked as duplicate of this issue

#80759 was marked as duplicate of this issue
Collaborator

#79808 was marked as duplicate of this issue

#79808 was marked as duplicate of this issue
Collaborator

#77595 was marked as duplicate of this issue

#77595 was marked as duplicate of this issue
Poster

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
Poster

Migth be related to this #77595

Migth be related to this #77595
mont29 commented 3 years ago
Owner

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29
mont29 commented 3 years ago
Owner

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

Changed status from 'Needs Triage' to: 'Confirmed'
mont29 commented 3 years ago
Owner

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.
mont29 commented 3 years ago
Owner

Added subscriber: @Zuorion-4

Added subscriber: @Zuorion-4
Poster

@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
mont29 commented 3 years ago
Owner

Closed as duplicate of #77460

Closed as duplicate of #77460
mont29 closed this issue 3 years ago
mont29 commented 3 years ago
Owner

Changed status from 'Duplicate' to: 'Confirmed'

Changed status from 'Duplicate' to: 'Confirmed'
mont29 reopened this issue 3 years ago
mont29 commented 3 years ago
Owner

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
Poster

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?
Poster

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..
mont29 commented 3 years ago
Owner

@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).
Poster

@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
mont29 commented 3 years ago
Owner

@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...
Poster

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..
Poster

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
Owner

@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
Poster

@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..
Owner

@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.
Poster

@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?
Poster

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
Owner

Added subscribers: @Zweistein, @lichtwerk

Added subscribers: @Zweistein, @lichtwerk
ideasman42 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 2 years ago
Owner

Added subscriber: @jeacom

Added subscriber: @jeacom
Owner

Added subscribers: @kursadk, @JosephEagar

Added subscribers: @kursadk, @JosephEagar
Collaborator

Added subscriber: @wbmoss_dev

Added subscriber: @wbmoss_dev

Added subscriber: @pauanyu_blender

Added subscriber: @pauanyu_blender
Collaborator

Added subscriber: @Macroni

Added subscriber: @Macroni
Owner

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
rjg commented 2 years ago
Collaborator

Added subscriber: @rjg

Added subscriber: @rjg
rjg commented 2 years ago
Collaborator

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.

Added subscriber: @Imaginer

Added subscriber: @Imaginer
rjg commented 2 years ago
Collaborator

@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.
jeacom commented 2 years ago

They could tag this as a known issue already.

They could tag this as a known issue already.

Added subscriber: @PhilippeCrassous

Added subscriber: @PhilippeCrassous
EAW commented 1 year ago

Added subscriber: @1029910278

Added subscriber: @1029910278
lichtwerk removed the
legacy module/Python API
label 2 hours ago
lichtwerk removed the
Interest/Python API
label 2 hours ago
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/Collada
Interest/Compositing
Interest/Core
Interest/Cycles
Interest/Dependency Graph
Interest/Development Management
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/Modeling
Interest/Modifiers
Interest/Motion Tracking
Interest/Nodes & Physics
Interest/Overrides
Interest/Performance
Interest/Performance
Interest/Physics
Interest/Pipeline, Assets & I/O
Interest/Platforms, Builds, Tests & Devices
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
legacy module/Animation & Rigging
legacy module/Core
legacy module/Development Management
legacy module/Eevee & Viewport
legacy module/Grease Pencil
legacy module/Modeling
legacy module/Nodes & Physics
legacy module/Pipeline, Assets & IO
legacy module/Platforms, Builds, Tests & Devices
legacy module/Python API
legacy module/Rendering & Cycles
legacy module/Sculpt, Paint & Texture
legacy module/Triaging
legacy module/User Interface
legacy module/VFX & Video
legacy project/1.0.0-beta.2
legacy project/Asset Browser (Archived)
legacy project/BF Blender: 2.8
legacy project/BF Blender: After Release
legacy project/BF Blender: Next
legacy project/BF Blender: Regressions
legacy project/BF Blender: Unconfirmed
legacy project/Blender 2.70
legacy project/Code Quest
legacy project/Datablocks and Libraries
legacy project/Eevee
legacy project/Game Animation
legacy project/Game Audio
legacy project/Game Data Conversion
legacy project/Game Engine
legacy project/Game Logic
legacy project/Game Physics
legacy project/Game Python
legacy project/Game Rendering
legacy project/Game UI
legacy project/GPU / Viewport
legacy project/GSoC
legacy project/Infrastructure: Websites
legacy project/LibOverrides - Usability and UX
legacy project/Milestone 1: Basic, Local Asset Browser
legacy project/Nodes
legacy project/OpenGL Error
legacy project/Papercut
legacy project/Pose Library Basics
legacy project/Retrospective
legacy project/Tracker Curfew
legacy project/Wintab High Frequency
Meta/Good First Issue
Meta/Papercut
migration/requires-manual-verification
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 & Devices
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 Information 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

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#77557
Loading…
There is no content yet.