Fix USD Export crash with complex scenes, due to race condition during depsgraph building. #113537

Merged
Bastien Montagne merged 1 commits from mont29/blender:tmp-usd-fix-deg-export-crash into blender-v4.0-release 2023-10-12 19:10:46 +02:00

Building the depsgraph from non-main thread is unsafe currently, as it
may trigger some deferred processing on the Main data itself (here, the
update/resync of viewlayer collections).

So this commit moves the building of the export depsgraph back into the
main thread for the USD exporter, while keeping the depsgraph evaluation
code (which is typically the heavy part with complex scenes) into the
threaded job worker code.

Issue was randomly (but fairly commonly) reproducible when trying to export
most of the Pets project production files (on linux debug build with ASAN).

Root issue has been reported and is being discussed in #112534, ideally a
better solution can be designed at depsgraph level in the future.

NOTE: Should likely also be back-ported to 3.6 and 3.3 LTS.

Building the depsgraph from non-main thread is unsafe currently, as it may trigger some deferred processing on the Main data itself (here, the update/resync of viewlayer collections). So this commit moves the building of the export depsgraph back into the main thread for the USD exporter, while keeping the depsgraph evaluation code (which is typically the heavy part with complex scenes) into the threaded job worker code. Issue was randomly (but fairly commonly) reproducible when trying to export most of the Pets project production files (on linux debug build with ASAN). Root issue has been reported and is being discussed in #112534, ideally a better solution can be designed at depsgraph level in the future. NOTE: Should likely also be back-ported to 3.6 and 3.3 LTS.
Bastien Montagne added the
Interest
USD
Module
Pipeline, Assets & IO
labels 2023-10-11 12:16:42 +02:00
Bastien Montagne added 1 commit 2023-10-11 12:16:54 +02:00
737ac07dc9 Fix USD Export crash with complex scenes, due to race condition during depsgraph building.
Building the depsgraph from non-main thread is unsafe currently, as it
may trigger some deferred processing on the Main data itself (here, the
update/resync of viewlayer collections).

So this commit moves the building of the export depsgraph back into the
main thread for the USD exporter, while keeping the depsgraph evaluation
code (which is typically the heavy part with complex scenes) into the
threaded job worker code.

Issue was randomly (but fairly commonly) reproducible when trying to export
most of the Pets project production files (on linux debug build with ASAN).

Root issue has been reported and is being discussed in #112534, ideally a
better solution can be designed at depsgraph level in the future.

NOTE: Should likely also be back-ported to 3.6 and 3.3 LTS.
Bastien Montagne requested review from Sybren A. Stüvel 2023-10-11 12:17:12 +02:00
Bastien Montagne requested review from Sergey Sharybin 2023-10-11 12:17:12 +02:00
Bastien Montagne requested review from Michael Kowalski 2023-10-11 12:17:12 +02:00
Bastien Montagne added this to the USD project 2023-10-11 12:17:21 +02:00

The first evaluation would need to do the Copy-on-Write step, which involves accessing to the original objects. As long as the interface is locked before the depsgraph is constructed it is fine (so that there is no risk of artist deleting object after the graph is build and worker thread is actually span).

What is unclear to me, is why access to the original objects is fine from the thread for evaluation, but is not for the building. Is there are still some operations which access the view layer are run from the main thread while the export job is running?

The first evaluation would need to do the Copy-on-Write step, which involves accessing to the original objects. As long as the interface is locked before the depsgraph is constructed it is fine (so that there is no risk of artist deleting object after the graph is build and worker thread is actually span). What is unclear to me, is why access to the original objects is fine from the thread for evaluation, but is not for the building. Is there are still some operations which access the view layer are run from the main thread while the export job is running?
Author
Owner

Issue is with scenes' ViewLayers, and their LayerCollections hierarchies. These are now only tagged as dirty when collection (and their objects) hierarchies are modified, and lazily updated on-demand (calls to BKE_main_view_layers_synced_ensure and the like). This tagging and lazy update is not threadsafe at all, so it should only ever be called from main thread.

If that happens as part of depsgraph building (which it can), from some random thread, then main thread (or another thread) may also be calling the same update code (from it's own main loop event depsgraph update e.g), and we have concurrency issues.

Issue is with scenes' ViewLayers, and their LayerCollections hierarchies. These are now only tagged as dirty when collection (and their objects) hierarchies are modified, and lazily updated on-demand (calls to `BKE_main_view_layers_synced_ensure` and the like). This tagging and lazy update is not threadsafe at all, so it should only ever be called from main thread. If that happens as part of depsgraph building (which it can), from some random thread, then main thread (or another thread) may also be calling the same update code (from it's own main loop event depsgraph update e.g), and we have concurrency issues.

Sure, but the question is more about: how is it possible that main thread with locked UI does something with the dirty state of collections?

Sure, but the question is more about: how is it possible that main thread with locked UI does something with the dirty state of collections?
Sergey Sharybin approved these changes 2023-10-11 16:26:12 +02:00
Sergey Sharybin left a comment
Owner

Talked IRL.

So it is just the UI which is locked, and the notifiers etc are still handled, so it is possible that layer collection's dirty state is handled from multiple threads.

Since the UI is locked it is not possible for artists to remove objects, so the rest of the evaluation is fine to happen in the thread.

Talked IRL. So it is just the UI which is locked, and the notifiers etc are still handled, so it is possible that layer collection's dirty state is handled from multiple threads. Since the UI is locked it is not possible for artists to remove objects, so the rest of the evaluation is fine to happen in the thread.

I will do a quick local build and test for due diligence, but the change looks good to me! Thank you for the fix!

I will do a quick local build and test for due diligence, but the change looks good to me! Thank you for the fix!
Michael Kowalski approved these changes 2023-10-11 18:10:59 +02:00
Bastien Montagne merged commit 5cf3654d9d into blender-v4.0-release 2023-10-12 19:10:46 +02:00
Bastien Montagne deleted branch tmp-usd-fix-deg-export-crash 2023-10-12 19:10:48 +02:00
Bastien Montagne removed this from the USD project 2023-11-02 16:13:33 +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#113537
No description provided.