save_as_mainfile will crash if context has no screen #93949

Closed
opened 2021-12-10 21:52:08 +01:00 by Bay Raitt · 19 comments

System Information
Windows 10
GTX 970

Blender Version
Broken: 3.0
Worked: 2.93

Short description of error
using python, loading a template.blend file and saving as a new.blend file will crash blender if a camera is present in the template.blend file.

Exact steps for others to reproduce the error
In script editor execute:

import bpy
bpy.ops.wm.read_homefile()
bpy.ops.wm.save_as_mainfile(filepath='/tmp/test.blend')

With factory startup file, this will cause crash - NULL dereference in BKE_screen_find_big_area(), screen was NULL

**System Information** Windows 10 GTX 970 **Blender Version** Broken: 3.0 Worked: 2.93 **Short description of error** using python, loading a template.blend file and saving as a new.blend file will crash blender if a camera is present in the template.blend file. **Exact steps for others to reproduce the error** In script editor execute: ``` import bpy bpy.ops.wm.read_homefile() bpy.ops.wm.save_as_mainfile(filepath='/tmp/test.blend') ``` With factory startup file, this will cause crash - NULL dereference in `BKE_screen_find_big_area()`, `screen` was `NULL`
Author

Added subscriber: @spiraloid-3

Added subscriber: @spiraloid-3

#95188 was marked as duplicate of this issue

#95188 was marked as duplicate of this issue

#93950 was marked as duplicate of this issue

#93950 was marked as duplicate of this issue

Added subscriber: @iss

Added subscriber: @iss

Not sure if this is different report than #93950, but if there are different steps, please provide them.

Not sure if this is different report than #93950, but if there are different steps, please provide them.
Author

Different issue then #93950.

(see last comment)

Steps to reproduce, Install the addon attached to my previous post and click “crash me” in the file menu.

  • or

write a python script to load a blend file containing a camera and then save out a new blend file.

this is essentially all my addon does.

if you look at my python, I commented the source of the crash. (it has something to do with loading a .blend file containing a camera and saving again)

I spent quite a bit of time finding the exact steps to reproduce this crash.

Different issue then #93950. (see last comment) Steps to reproduce, Install the addon attached to my previous post and click “crash me” in the file menu. - or write a python script to load a blend file containing a camera and then save out a new blend file. this is essentially all my addon does. if you look at my python, I commented the source of the crash. (it has something to do with loading a .blend file containing a camera and saving again) I spent quite a bit of time finding the exact steps to reproduce this crash.

Added subscriber: @michael64

Added subscriber: @michael64

Thanks @spiraloid-3 for the detailed description, I could reproduce the crash, find a fix for future Blender versions and a workaround for you to get your add-on working with Blender 3.0.

For your workaround you have to circumvent the automatic thumbnail generation that was introduced in 4fa0bbb5.
This can be done for example like this

        # :::::::::::::::::::::::::::::::::::::::::::::::::::Avoid Trigger Crash here 
        if bpy.context.screen is not None:
            bpy.ops.wm.save_as_mainfile(filepath=clean_target_filename)
        else:
            - Prevent crash when the context has no screen
            - by temporarily disabling automatic thumbnail generation.
            old_preview_type = bpy.context.preferences.filepaths.file_preview_type
            bpy.context.preferences.filepaths.file_preview_type = 'NONE'
            try:
                bpy.ops.wm.save_as_mainfile(filepath=clean_target_filename)
            finally:
                # Restore file_preview_type
                bpy.context.preferences.filepaths.file_preview_type = old_preview_type
        # :::::::::::::::::::::::::::::::::::::::::::::::::::Avoid Trigger Crash here 

Because thumbnails are only automatically generated from the main thread, calling bpy.ops.wm.save_as_mainfile from another thread should also work but the previous workaround seems simpler.
Also if the scene already had a thumbnail it would not be regenerated in save_as_mainfile.

Now for the cause of the crash for the Blender developers.
The automatic thumbnail generation calls BKE_screen_find_big_area in wm_files.c: (BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0) != NULL)
This function in screen.c references screen->areabase before checking if screen is NULL which can be the case when called from a Python add-on.
This dereferencing screen->areabase crashes the Blender process.

I would propose a three-line addition that returns NULL if a NULL screen was passed in as the logic that NULL can be returned seems to be already expected by the callers:

ScrArea *BKE_screen_find_big_area(bScreen *screen, const int spacetype, const short min)
{
  ScrArea *big = NULL;
  int maxsize = 0;
  if (screen == NULL) {
    return NULL;
  }

I have not looked at the difference to #93950 but maybe the same error could be triggered in two different ways.

        bool do_render = (scene != NULL && scene->camera != NULL &&
                          (BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0) != NULL));

That scene->camera != NULL short circuits the call to BKE_screen_find_big_area in case of a missing camera also explains why crashmenot.blend does not crash.

Thanks @spiraloid-3 for the detailed description, I could reproduce the crash, find a fix for future Blender versions and a workaround for you to get your add-on working with Blender 3.0. For your workaround you have to circumvent the automatic thumbnail generation that was introduced in 4fa0bbb5. This can be done for example like this ``` # :::::::::::::::::::::::::::::::::::::::::::::::::::Avoid Trigger Crash here if bpy.context.screen is not None: bpy.ops.wm.save_as_mainfile(filepath=clean_target_filename) else: - Prevent crash when the context has no screen - by temporarily disabling automatic thumbnail generation. old_preview_type = bpy.context.preferences.filepaths.file_preview_type bpy.context.preferences.filepaths.file_preview_type = 'NONE' try: bpy.ops.wm.save_as_mainfile(filepath=clean_target_filename) finally: # Restore file_preview_type bpy.context.preferences.filepaths.file_preview_type = old_preview_type # :::::::::::::::::::::::::::::::::::::::::::::::::::Avoid Trigger Crash here ``` Because thumbnails are only automatically generated from the main thread, calling bpy.ops.wm.save_as_mainfile from another thread should also work but the previous workaround seems simpler. Also if the scene already had a thumbnail it would not be regenerated in save_as_mainfile. Now for the cause of the crash for the Blender developers. The automatic thumbnail generation calls BKE_screen_find_big_area in wm_files.c: (BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0) != NULL) This function in screen.c references screen->areabase before checking if screen is NULL which can be the case when called from a Python add-on. This dereferencing screen->areabase crashes the Blender process. I would propose a three-line addition that returns NULL if a NULL screen was passed in as the logic that NULL can be returned seems to be already expected by the callers: ``` ScrArea *BKE_screen_find_big_area(bScreen *screen, const int spacetype, const short min) { ScrArea *big = NULL; int maxsize = 0; if (screen == NULL) { return NULL; } ``` I have not looked at the difference to #93950 but maybe the same error could be triggered in two different ways. ``` bool do_render = (scene != NULL && scene->camera != NULL && (BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0) != NULL)); ``` That scene->camera != NULL short circuits the call to BKE_screen_find_big_area in case of a missing camera also explains why crashmenot.blend does not crash.
Author

Thanks so much @michael64, Much appreciated.

Thanks so much @michael64, Much appreciated.

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

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

Added subscriber: @Harley

Added subscriber: @Harley

CC @Harley

CC @Harley

Added subscribers: @chemicalcrux, @Raimund58

Added subscribers: @chemicalcrux, @Raimund58
Richard Antalik changed title from Blener crash w python save as". to save_as_mainfile will crash if context has no screen 2021-12-13 02:05:56 +01:00
Harley Acheson self-assigned this 2021-12-13 04:00:19 +01:00

This issue was referenced by e53f3954a4

This issue was referenced by e53f3954a44b3d527cf2ef3ad8bd9c23ca810817

This issue was referenced by 644eb68524

This issue was referenced by 644eb68524b9fc709891860e2c0c40782325a62b
Member

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'

Added subscriber: @Lendo

Added subscriber: @Lendo
Author

FYI, I'm getting a similar preview error if the asset browser is visible.

I've resorted to

bpy.ops.screen.screen_full_area()

in the interrum.

FYI, I'm getting a similar preview error if the asset browser is visible. I've resorted to ``` bpy.ops.screen.screen_full_area() ``` in the interrum.

@spiraloid-3 Please open new report and provide information as described in template.

@spiraloid-3 Please open new report and provide information as described in template.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
FBX
Interest
Freestyle
Interest
Geometry Nodes
Interest
glTF
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 & 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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
6 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#93949
No description provided.