VSE text strips can attempt to load fonts from threaded context, which is not allowed. #114592

Open
opened 2023-11-07 18:37:50 +01:00 by Bastien Montagne · 9 comments

System Information
Operating system: N/A
Graphics card: N/A

Blender Version
Broken: current main
Worked: Never ?

Short description of error

The VSE text strip tries to load VFont data in several places, including from code called from non-main threads.

E.g. copy_text_effect callback is called as part of Scene 'CoW' copying during depsgraph evaluation, which can be done from a worker thread.

BLF loading code is not thread safe in any way.

Commit 2f8499415b is a quick band-aid fix to address this issue, but the actual problem is that a strip can try to load a missing font from depsgraph evaluation code. Proper solution is likely rather to store the fact that a font could not be loaded somehow, and avoid trying to reload it over and over.

Exact steps for others to reproduce the error

Make a debug build of current main with 2f8499415b reverted (or neutralized with snippet patch below).

--- a/source/blender/sequencer/intern/effects.cc
+++ b/source/blender/sequencer/intern/effects.cc
@@ -3251,7 +3251,8 @@ void SEQ_effect_text_font_load(TextVars *data, const bool do_id_user)
   else {
     char filepath[FILE_MAX];
     STRNCPY(filepath, vfont->filepath);
-    if (BLI_thread_is_main()) {
+    if (BLI_thread_is_main() || true) {
+      BLI_assert(BLI_thread_is_main());
       /* FIXME: This is a band-aid fix. A proper solution has to be worked on by the VSE team.
        *
        * This code can be called from non-main thread, e.g. when copying sequences as part of
  • Open vse_crash_on_missing_fonts.blend.
  • In the VSE of scene Scene.001, attempt to add a Scene strip of Scene.
  • In debug build Blender will crash immediately on the assert about being in main thread.
**System Information** Operating system: N/A Graphics card: N/A **Blender Version** Broken: current main Worked: Never ? **Short description of error** The VSE text strip tries to load VFont data in several places, including from code called from non-main threads. E.g. `copy_text_effect` callback is called as part of Scene 'CoW' copying during depsgraph evaluation, which can be done from a worker thread. BLF loading code is not thread safe in any way. Commit 2f8499415b is a quick band-aid fix to address this issue, but the actual problem is that a strip can try to load a missing font from depsgraph evaluation code. Proper solution is likely rather to store the fact that a font could not be loaded somehow, and avoid trying to reload it over and over. **Exact steps for others to reproduce the error** Make a debug build of current main with 2f8499415b reverted (or neutralized with snippet patch below). ``` --- a/source/blender/sequencer/intern/effects.cc +++ b/source/blender/sequencer/intern/effects.cc @@ -3251,7 +3251,8 @@ void SEQ_effect_text_font_load(TextVars *data, const bool do_id_user) else { char filepath[FILE_MAX]; STRNCPY(filepath, vfont->filepath); - if (BLI_thread_is_main()) { + if (BLI_thread_is_main() || true) { + BLI_assert(BLI_thread_is_main()); /* FIXME: This is a band-aid fix. A proper solution has to be worked on by the VSE team. * * This code can be called from non-main thread, e.g. when copying sequences as part of ``` * Open [vse_crash_on_missing_fonts.blend](/attachments/35fb982b-609f-4c72-a35c-cba8b08c1763). * In the VSE of scene `Scene.001`, attempt to add a `Scene` strip of `Scene`. * In debug build Blender will crash immediately on the assert about being in main thread.

String to Curve node works in geometry node (fully multithreaded). After short research, vfont.cc have singleton mutex for such things.

`String to Curve` node works in geometry node (fully multithreaded). After short research, `vfont.cc` have singleton mutex for such things.
Author
Owner

@mod_moder That may be so, but issue here is that this code directly uses BLF, fully by-passing BKE VFont code...

@mod_moder That may be so, but issue here is that this code directly uses BLF, fully by-passing BKE VFont code...
Author
Owner

@ZedDB @iss FYI.

@ZedDB @iss FYI.

I was thinking about solution quite some time ago, but did not come up with something reasonable. Will check BKE VFont code to see how I can avoid this situation.

I was thinking about solution quite some time ago, but did not come up with something reasonable. Will check BKE VFont code to see how I can avoid this situation.
Author
Owner

@iss any reason not to 'just' use the VFont API directly?

@iss any reason not to 'just' use the VFont API directly?

@mont29 At the time when I implemented patch I wasn't aware of API existing, after that I did not come across it.

@mont29 At the time when I implemented patch I wasn't aware of API existing, after that I did not come across it.
Member

I haven't looked at this deeply, but wouldn't this problem be helped by using BLF_load_unique instead of BLF_load and BLF_load_mem_unique instead of BLF_load_mem?

FreeType and our use of it are threadsafe, but the FontBLF struct has parts that are not - like pen position, aspect, etc. But this doesn't matter if you are the only user of it. We load a unique (2nd) copy of our monospaced font just for render info for this same problem.

EDIT: Oh, I see now there are notes about wanting to ensure that the same font is not loaded multiple times for multiple strips. So my idea wouldn't works here. Oh well...

I haven't looked at this deeply, but wouldn't this problem be helped by using `BLF_load_unique` instead of `BLF_load` and `BLF_load_mem_unique` instead of `BLF_load_mem`? FreeType and our use of it are threadsafe, but the FontBLF struct has parts that are not - like pen position, aspect, etc. But this doesn't matter if you are the only user of it. We load a unique (2nd) copy of our monospaced font just for render info for this same problem. EDIT: Oh, I see now there are notes about wanting to ensure that the same font is not loaded multiple times for multiple strips. So my idea wouldn't works here. Oh well...

So I have checked the VFont API and current VSE code. The reason it uses BLF directly seems to be, that it provides a simple way to draw text to bitmap buffer. A solution could be to make similar function as BKE_vfont_to_curve_ex() for drawing to a ImBuf, that seems to be most logical approach to me. However the renderer would still be BLF. So this doesn't solve the issue of thread safety, since render jobs needs to load and render the font anyway. Technically render job could pre-load all fonts from main thread, but drawing won't be covered. In past I have implemented glyph cache mutex to make rendering of fonts thread safe, but as @Harley mentioned, this is complete solution. But for VSE rendering this was fine, as it normally is only user of the font.

Looking at BLF API, fonts are referenced by int fontid instead of FontBLF pointer. I remember wanting to change this some time ago. That way you can have mutex locked acquire / release functions where you would have exclusive control over font drawing.

@Harley Would you see any issue with making BLF loading and usage thread safe as I suggest?

So I have checked the VFont API and current VSE code. The reason it uses BLF directly seems to be, that it provides a simple way to draw text to bitmap buffer. A solution could be to make similar function as `BKE_vfont_to_curve_ex()` for drawing to a `ImBuf`, that seems to be most logical approach to me. However the renderer would still be BLF. So this doesn't solve the issue of thread safety, since render jobs needs to load and render the font anyway. Technically render job could pre-load all fonts from main thread, but drawing won't be covered. In past I have implemented glyph cache mutex to make rendering of fonts thread safe, but as @Harley mentioned, this is complete solution. But for VSE rendering this was fine, as it normally is only user of the font. Looking at BLF API, fonts are referenced by `int fontid` instead of `FontBLF` pointer. I remember wanting to change this some time ago. That way you can have mutex locked acquire / release functions where you would have exclusive control over font drawing. @Harley Would you see any issue with making BLF loading and usage thread safe as I suggest?
Member

@iss - fonts are referenced by int fontid instead of FontBLF pointer. I remember wanting to change this some time ago. That way you can have mutex locked acquire / release functions where you would have exclusive control over font drawing.

Interesting...

All of FreeType and our calls to it are now threadsafe. And with the addition of FreeType caching we no longer have a one-to-one relationship between a FontBLF and a freetype face. FreeType will now load and unload fonts whenever it wants, and we no longer require a face to be loaded unless you need new glyphs rendered and it will likely be unloaded shortly after. In fact we have been running for a few years now where FreeType can not have more than four faces open at a time, so it is quite often bringing them in and out without us even noticing.

But there are some things in FontBLF itself that is not threadsafe, like the current pen position for example. As you mention we are currently using fontid in the public API, which means we are generally reusing those (unless we call BLF_load_unique instead) for no real reason. I think you are right in that we could always just create a new FontBLF when asked and use a pointer to it. Fairly certain we almost everything in c++ now so we could probably use that directly. The underlying code might have two different FontBLF use the same face, but that would great and is what happens now anyway.

It would be nice time to get rid of BLF_MAX_FONT, now 64, and the global_font array. Use some better limitless list instead. We actually have no need to limit the number of FontBLF at all.

This idea would probably break something in the python api. Although they could use the returned pointer as an integer as they do now, I think they would be treating 0 and/or -1 or something as special. Might not be a biggie, not sure. We need to keep track of the regular and mono UI fonts anyway.

Sounds interesting.

The reason it uses BLF directly seems to be, that it provides a simple way to draw text to bitmap buffer. A solution could be to make similar function as BKE_vfont_to_curve_ex() for drawing to a ImBuf, that seems to be most logical approach to me.

What exactly do you need? You could provide a pointer to an Imbuf, an output string, a pen position, and details of the font and size, weight, etc. I could write out to that surface and then return a new pen position?

> @iss - fonts are referenced by int fontid instead of FontBLF pointer. I remember wanting to change this some time ago. That way you can have mutex locked acquire / release functions where you would have exclusive control over font drawing. Interesting... All of FreeType and our calls to it are now threadsafe. And with the addition of FreeType caching we no longer have a one-to-one relationship between a FontBLF and a freetype face. FreeType will now load and unload fonts whenever it wants, and we no longer require a face to be loaded unless you need new glyphs rendered and it will likely be unloaded shortly after. In fact we have been running for a few years now where FreeType can not have more than four faces open at a time, so it is quite often bringing them in and out without us even noticing. But there are some things in FontBLF itself that is not threadsafe, like the current pen position for example. As you mention we are currently using fontid in the public API, which means we are generally reusing those (unless we call BLF_load_unique instead) for no real reason. I think you are right in that we could always just create a new FontBLF when asked and use a pointer to it. Fairly certain we almost everything in c++ now so we could probably use that directly. The underlying code might have two different FontBLF use the same face, but that would great and is what happens now anyway. It would be nice time to get rid of BLF_MAX_FONT, now 64, and the `global_font` array. Use some better limitless list instead. We actually have no need to limit the number of FontBLF at all. This idea would probably break something in the python api. Although they could use the returned pointer as an integer as they do now, I think they would be treating 0 and/or -1 or something as special. Might not be a biggie, not sure. We need to keep track of the regular and mono UI fonts anyway. Sounds interesting. > The reason it uses BLF directly seems to be, that it provides a simple way to draw text to bitmap buffer. A solution could be to make similar function as BKE_vfont_to_curve_ex() for drawing to a ImBuf, that seems to be most logical approach to me. What exactly do you need? You could provide a pointer to an Imbuf, an output string, a pen position, and details of the font and size, weight, etc. I could write out to that surface and then return a new pen position?
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
4 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#114592
No description provided.