Thread race condition with lock interface enabled #90043

Open
opened 2021-07-22 12:28:51 +02:00 by michael campbell · 12 comments

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 461.92

Blender Version
Broken: version: 2.93.2 Release Candidate, branch: master, commit date: 2021-07-07 05:52, hash: 9e64fd461a
Worked: (newest version of Blender that worked as expected)

Description
UI redraw is still taking place in the render_init and render_complete handlers when use_lock_interface = True. This lock was only implemented for 3D viewport, but this is not mentioned in any of manuals.

Adding lock to node editor seems to resolve the issue:

diff --git a/source/blender/editors/space_node/space_node.c b/source/blender/editors/space_node/space_node.c
index ff848a7bb95..9813b8aaa86 100644
--- a/source/blender/editors/space_node/space_node.c
+++ b/source/blender/editors/space_node/space_node.c
@@ -1073,6 +1073,7 @@ void ED_spacetype_node(void)
   art->cursor = node_cursor;
   art->event_cursor = true;
   art->clip_gizmo_events_by_ui = true;
+  art->lock = 1;

   BLI_addhead(&st->regiontypes, art);

Trace:

>	blender.exe!UI_block_end_ex(const bContext * C, uiBlock * block, const int * xy, int * r_xy) Line 1873	C

 	blender.exe!node_draw_basis(const bContext * C, const View2D * v2d, const SpaceNode * snode, bNodeTree * ntree, bNode * node, bNodeInstanceKey key) Line 1733	C++
 	blender.exe!node_draw_default(const bContext * C, ARegion * region, SpaceNode * snode, bNodeTree * ntree, bNode * node, bNodeInstanceKey key) Line 1943	C++
 	[Inline Frame] blender.exe!node_draw(const bContext *) Line 2002	C++
 	[Inline Frame] blender.exe!node_draw_nodetree(const bContext *) Line 2059	C++
 	blender.exe!draw_nodetree(const bContext * C, ARegion * region, bNodeTree * ntree, bNodeInstanceKey parent_key) Line 2097	C++
 	blender.exe!node_draw_space(const bContext * C, ARegion * region) Line 2235	C++
 	blender.exe!ED_region_do_draw(bContext * C, ARegion * region) Line 569	C
 	blender.exe!wm_draw_window_offscreen(bContext * C, wmWindow * win, bool stereo) Line 726	C
 	blender.exe!wm_draw_window(bContext * C, wmWindow * win) Line 868	C
 	blender.exe!wm_draw_update(bContext * C) Line 1067	C
 	blender.exe!WM_main(bContext * C) Line 654	C
 	blender.exe!main(int argc, const unsigned char * * UNUSED_argv_c) Line 563	C

Exact steps for others to reproduce the error
Open below file, run the script and then press f12.

ui lock ignored on some handlers which are not on the main thread.blend

**System Information** Operating system: Windows-10-10.0.19041-SP0 64 Bits Graphics card: GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 461.92 **Blender Version** Broken: version: 2.93.2 Release Candidate, branch: master, commit date: 2021-07-07 05:52, hash: `9e64fd461a` Worked: (newest version of Blender that worked as expected) **Description** UI redraw is still taking place in the render_init and render_complete handlers when use_lock_interface = True. This lock was only implemented for 3D viewport, but this is not mentioned in any of manuals. Adding lock to node editor seems to resolve the issue: ``` diff --git a/source/blender/editors/space_node/space_node.c b/source/blender/editors/space_node/space_node.c index ff848a7bb95..9813b8aaa86 100644 --- a/source/blender/editors/space_node/space_node.c +++ b/source/blender/editors/space_node/space_node.c @@ -1073,6 +1073,7 @@ void ED_spacetype_node(void) art->cursor = node_cursor; art->event_cursor = true; art->clip_gizmo_events_by_ui = true; + art->lock = 1; BLI_addhead(&st->regiontypes, art); ``` Trace: ``` > blender.exe!UI_block_end_ex(const bContext * C, uiBlock * block, const int * xy, int * r_xy) Line 1873 C blender.exe!node_draw_basis(const bContext * C, const View2D * v2d, const SpaceNode * snode, bNodeTree * ntree, bNode * node, bNodeInstanceKey key) Line 1733 C++ blender.exe!node_draw_default(const bContext * C, ARegion * region, SpaceNode * snode, bNodeTree * ntree, bNode * node, bNodeInstanceKey key) Line 1943 C++ [Inline Frame] blender.exe!node_draw(const bContext *) Line 2002 C++ [Inline Frame] blender.exe!node_draw_nodetree(const bContext *) Line 2059 C++ blender.exe!draw_nodetree(const bContext * C, ARegion * region, bNodeTree * ntree, bNodeInstanceKey parent_key) Line 2097 C++ blender.exe!node_draw_space(const bContext * C, ARegion * region) Line 2235 C++ blender.exe!ED_region_do_draw(bContext * C, ARegion * region) Line 569 C blender.exe!wm_draw_window_offscreen(bContext * C, wmWindow * win, bool stereo) Line 726 C blender.exe!wm_draw_window(bContext * C, wmWindow * win) Line 868 C blender.exe!wm_draw_update(bContext * C) Line 1067 C blender.exe!WM_main(bContext * C) Line 654 C blender.exe!main(int argc, const unsigned char * * UNUSED_argv_c) Line 563 C ``` **Exact steps for others to reproduce the error** Open below file, run the script and then press f12. [ui lock ignored on some handlers which are not on the main thread.blend](https://archive.blender.org/developer/F10234879/ui_lock_ignored_on_some_handlers_which_are_not_on_the_main_thread.blend)

Added subscriber: @3di

Added subscriber: @3di

#90079 was marked as duplicate of this issue

#90079 was marked as duplicate of this issue

Added subscriber: @iss

Added subscriber: @iss

Seems to work fine in 3.0. Not sure if this would be possible to fix in 2.93 would have to check which commit fixed this

Seems to work fine in 3.0. Not sure if this would be possible to fix in 2.93 would have to check which commit fixed this

Morning @iss I've just tested with today's 3.0 build, and it's still crashing Blender with ACCESS VIOLATION. Render a few times and it should crash (it just depends on what the main thread is doing with memory at the time I believe). One thing I've just noticed is that it only crashes if you have the compositor visible, even if lock interface is set to true.

My guess is that lock interface is ignored during the render_init and render_complete handlers, which causes the crash as they're still not on the main thread. This would explain why it always happens during UI drawing:

ExceptionCode : EXCEPTION_ACCESS_VIOLATION
Exception Address : 0x00007FF742D015C3
Exception Module : blender.exe
Exception Flags : 0x00000000
Exception Parameters : 0x2
Parameters- [x] : 0x0000000000000000
Parameters- [x] : 0x0000000000000010

Stack trace:
blender.exe :0x00007FF742D01560 UI_block_end_ex
blender.exe :0x00007FF742E038A0 node_draw_basis
blender.exe :0x00007FF742E06630 node_draw_default
blender.exe :0x00007FF742E03450 draw_nodetree
blender.exe :0x00007FF742E06D70 node_draw_space
blender.exe :0x00007FF7429F5B20 ED_region_do_draw
blender.exe :0x00007FF742513680 wm_draw_window_offscreen
blender.exe :0x00007FF7425134D0 wm_draw_window
blender.exe :0x00007FF742512FD0 wm_draw_update
blender.exe :0x00007FF7424EEC00 WM_main
blender.exe :0x00007FF742182EA0 main
blender.exe :0x00007FF747025F08 __scrt_common_main_seh
KERNEL32.DLL :0x00007FFBC0537020 BaseThreadInitThunk
ntdll.dll :0x00007FFBC2502630 RtlUserThreadStart

Morning @iss I've just tested with today's 3.0 build, and it's still crashing Blender with ACCESS VIOLATION. Render a few times and it should crash (it just depends on what the main thread is doing with memory at the time I believe). One thing I've just noticed is that it only crashes if you have the compositor visible, even if lock interface is set to true. My guess is that lock interface is ignored during the render_init and render_complete handlers, which causes the crash as they're still not on the main thread. This would explain why it always happens during UI drawing: ExceptionCode : EXCEPTION_ACCESS_VIOLATION Exception Address : 0x00007FF742D015C3 Exception Module : blender.exe Exception Flags : 0x00000000 Exception Parameters : 0x2 Parameters- [x] : 0x0000000000000000 Parameters- [x] : 0x0000000000000010 Stack trace: blender.exe :0x00007FF742D01560 UI_block_end_ex blender.exe :0x00007FF742E038A0 node_draw_basis blender.exe :0x00007FF742E06630 node_draw_default blender.exe :0x00007FF742E03450 draw_nodetree blender.exe :0x00007FF742E06D70 node_draw_space blender.exe :0x00007FF7429F5B20 ED_region_do_draw blender.exe :0x00007FF742513680 wm_draw_window_offscreen blender.exe :0x00007FF7425134D0 wm_draw_window blender.exe :0x00007FF742512FD0 wm_draw_update blender.exe :0x00007FF7424EEC00 WM_main blender.exe :0x00007FF742182EA0 main blender.exe :0x00007FF747025F08 __scrt_common_main_seh KERNEL32.DLL :0x00007FFBC0537020 BaseThreadInitThunk ntdll.dll :0x00007FFBC2502630 RtlUserThreadStart

So perhaps an easier solution would be to have render_complete respect the lock interface setting? Although that would still leave render_init open to problems if it's also not on the main python thread?

So perhaps an easier solution would be to have render_complete respect the lock interface setting? Although that would still leave render_init open to problems if it's also not on the main python thread?
michael campbell changed title from Access Violation due to some handlers not running on the main thread (Speak to Pedro, he mentioned he'd moved all python code to run on the main thread, maybe some slipped through the net?) to Access Violation due to some handlers not running on the main thread and still updating the UI even when lock UI is set to true. 2021-07-24 11:53:36 +02:00

Here's a simple example file showing how to force all render handlers onto the main thread:

force render handlers onto main thread.blend

INSTRUCTIONS:

  
  With a console window open, run this script.  It'll do two things, register the render handlers, and also
  render a still image by calling render.render directly from script.
  
  You'll notice that the handlers report that they're running on the main thread when render.render is called
  from script.
  
  Now press f12, you'll notice that the render handlers no longer run on the main thread.  This is an issue
  because render_init and render_complete are used to modify the scene before and after rendering, but 
  because of they're not on the main thread, ACCESS VIOLATION will occur due to the UI being redrawn.
  
  Lock UI doesn't seem to prevent UI redraw from render_init and render_complete either.
Here's a simple example file showing how to force all render handlers onto the main thread: [force render handlers onto main thread.blend](https://archive.blender.org/developer/F10235899/force_render_handlers_onto_main_thread.blend) INSTRUCTIONS: ``` With a console window open, run this script. It'll do two things, register the render handlers, and also render a still image by calling render.render directly from script. You'll notice that the handlers report that they're running on the main thread when render.render is called from script. Now press f12, you'll notice that the render handlers no longer run on the main thread. This is an issue because render_init and render_complete are used to modify the scene before and after rendering, but because of they're not on the main thread, ACCESS VIOLATION will occur due to the UI being redrawn. Lock UI doesn't seem to prevent UI redraw from render_init and render_complete either.
Richard Antalik changed title from Access Violation due to some handlers not running on the main thread and still updating the UI even when lock UI is set to true. to Thread race condition with lock interface enabled 2021-07-26 06:46:38 +02:00

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

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

@3di can reproduce in 3.0 too, I think I was lucky on first try. Issue here seems to be, that use_lock_interface property is used only by 3D viewport. I don't know why more editors don't use this property though.

I think It's best to add this to description too.

@3di can reproduce in 3.0 too, I think I was lucky on first try. Issue here seems to be, that `use_lock_interface` property is used only by 3D viewport. I don't know why more editors don't use this property though. I think It's best to add this to description too.

@iss You're a legend! Could you tell me when you're fix will be applied to master, and which versions it'll apply to please? Does it stop the image editor opening during render, and can the user still press escape to cancel a render?

I did find a workaround of:

overwrite default keymap on addon initialisation so that render.render would be called from an operator in my addon (so that I could handle render_init code before the render.render call, instread of using the render_init handler), and then using a queue to do move the render_complete code to a function run by the main thread once the rendering thread had closed down.

Do you know if it's possible to cancel a render from the render_init handler by the way?

@iss You're a legend! Could you tell me when you're fix will be applied to master, and which versions it'll apply to please? Does it stop the image editor opening during render, and can the user still press escape to cancel a render? I did find a workaround of: overwrite default keymap on addon initialisation so that render.render would be called from an operator in my addon (so that I could handle render_init code before the render.render call, instread of using the render_init handler), and then using a queue to do move the render_complete code to a function run by the main thread once the rendering thread had closed down. Do you know if it's possible to cancel a render from the render_init handler by the way?

I will leave this decision on nodes & physics module owners, because I don't know why this is implemented this way, nor can I say that this would be a good change. Even though it looks safe...

I will leave this decision on nodes & physics module owners, because I don't know why this is implemented this way, nor can I say that this would be a good change. Even though it looks safe...

ah ok, got you. Thanks 👍

ah ok, got you. Thanks 👍
Philipp Oeser removed the
Interest
Nodes & Physics
label 2023-02-10 08:44:37 +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
3 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#90043
No description provided.