Crash (EXC_BAD_ACCESS) in vsg::CompileManager::compile() while running vsgpagedlod
Describe the bug Sometimes VulkanSceneGraph crashes in vsg::CompileManager::compile() while running "vsgpagedlod --osm". Possibly, related to some race condition.
To Reproduce Steps to reproduce the behavior:
- Compile and install VulkanSceneGraph, vsgXchange, vsgImGui with standard flags.
- Compile vsgExamples with standard flags.
- Run "vsgpagedlod --osm". I don't sure that flag --osm is required, but I reproduced the bug with it several times.
- Press "1", "2", "3" ... "9" while crash is reproduced.
- If crash is not reproduced, restart the app and try again.
Expected behavior No crashing
Screenshots

Desktop (please complete the following information):
- OS: Mac OS Ventura
- HW: Apple Silicon (M1)
- Compiler: Apple clang version 14.0.0 (clang-1400.0.29.202)
- Target: arm64-apple-darwin22.1.0
- MoltenVK Vulkan SDK 1.3.231
- VulkanSceneGraph master
- vsgExamples master
Additional context Core dumps are attached below. corefilemodified_memory.zip corefilestack.zip
Hi Artemkulyk & Robert,
I get a similar occasional crash in my own application that pages terrain tiles using DatabasePager. Refer to this report: https://github.com/vsg-dev/VulkanSceneGraph/issues/490
Regards, Roland
On Wed, 7 Dec 2022 at 17:58, artemkulyk @.***> wrote:
Describe the bug Sometimes VulkanSceneGraph crashes in vsg::CompileManager::compile() while running "vsgpagedlod --osm". Possibly, related to some race condition.
To Reproduce Steps to reproduce the behavior:
- Compile and install VulkanSceneGraph, vsgXchange, vsgImGui with standard flags.
- Compile vsgExamples with standard flags.
- Run "vsgpagedlod --osm". I don't sure that flag --osm is required, but I reproduced the bug with it several times.
- Press "1", "2", "3" ... "9" while crash is reproduced.
- If crash is not reproduced, restart the app and try again.
Expected behavior No crashing
Screenshots [image: Screenshot 2022-12-07 at 07 54 18] https://user-images.githubusercontent.com/88086187/206109711-41337e0f-9c99-4c67-b361-8b99f6a4f6a3.png
Desktop (please complete the following information):
- OS: Mac OS Ventura
- HW: Apple Silicon (M1)
- Compiler: Apple clang version 14.0.0 (clang-1400.0.29.202)
- Target: arm64-apple-darwin22.1.0
- MoltenVK Vulkan SDK 1.3.231
- VulkanSceneGraph master
- vsgExamples master
Additional context Core dumps are attached below. corefilemodified_memory.zip https://github.com/vsg-dev/VulkanSceneGraph/files/10173497/corefilemodified_memory.zip corefilestack.zip https://github.com/vsg-dev/VulkanSceneGraph/files/10173498/corefilestack.zip
— Reply to this email directly, view it on GitHub https://github.com/vsg-dev/VulkanSceneGraph/issues/632, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPOEQ3IMSG4LU2X5B5WTXDWMAYRZANCNFSM6AAAAAASWNVBBY . You are receiving this because you are subscribed to this thread.Message ID: @.***>
Hi @rolandhill,
Yes, it seems the same issue.
Best regards, Artem Kulyk
Hi Artem,
I just posted draft PR #633 https://github.com/vsg-dev/VulkanSceneGraph/pull/633.
I've had this for a while without being able to reliably recreate the crash. Maybe give that PR a try and see if it helps. There's more of an explanation at that link.
Regards, Roland
Hi @rolandhill,
The crash was not stable reproducible on my machine, but I can confirm that I was able to reproduce it four times per about 20 runs without your changes. I tried to reproduce it with #633, but it's not reproducible after 10 runs.
If you need any additional information before merging, you can prepare a changelist with additional logging and I can try to reproduce the crash again on engineering binaries with additional logging.
Unfortunately, I am not familiar enough with VSG or OSG code yet to inspect and debug changes. I started learning it just yesterday.
Best regards, Artem Kulyk
It is still reproducible with #633. Core dumps attached below. corefilemodified_memory_633.zip corefilestack_633.zip

You can see that CompileTraversal->contexts[0] is a valid pointer, but context (which should just be a reference to CompileTraversal->contexts[0]) is different and likely invalid. Same thing I was seeing.
Robert has a much better understanding of how all this works and said he will have a look at it when his client work is finished. At least we have given him some leads and potentially a way to replicate the crash.
Regards, Roland
On Wed, 7 Dec 2022 at 22:36, artemkulyk @.***> wrote:
It is still reproducible with #633 https://github.com/vsg-dev/VulkanSceneGraph/pull/633. Core dumps attached below. corefilemodified_memory_633.zip https://github.com/vsg-dev/VulkanSceneGraph/files/10175585/corefilemodified_memory_633.zip corefilestack_633.zip https://github.com/vsg-dev/VulkanSceneGraph/files/10175586/corefilestack_633.zip
[image: image] https://user-images.githubusercontent.com/88086187/206169180-8f5a5064-fffe-4f21-aa3a-655a84086eea.png
— Reply to this email directly, view it on GitHub https://github.com/vsg-dev/VulkanSceneGraph/issues/632#issuecomment-1340840498, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPOEQ4BDIPLY34UMVDAWHTWMBZDPANCNFSM6AAAAAASWNVBBY . You are receiving this because you were mentioned.Message ID: @.***>
Consider avoiding of calling std::list::swap with itself in lines https://github.com/vsg-dev/VulkanSceneGraph/blob/master/src/vsg/app/CompileManager.cpp#L142 and https://github.com/vsg-dev/VulkanSceneGraph/blob/master/src/vsg/app/CompileManager.cpp#L169
Currently it works like this:
std::list<ref_ptr<Context>> contexts;
contexts = compileTraversal->contexts;
compileTraversal->contexts.swap(contexts);
// iterate on compileTraversal->contexts
compileTraversal->contexts.swap(contexts);
if contextSelection parameter is not provided.
BR, AK
I've moved the first swap into the condition so it only executes if contextSelection is valid. The second swap doesn't look to achieve much so I commented it out. I've left my original lock changes in.
Testing my application and it all works and I couldn't get a crash. Testing vsgpagedlod --osm and pressing all the numbers worked and didn't crash either in 8 attempts.
I'm not sure what the contextSelection does to be honest. Let's see what Robert thinks.
Cheers, Roland
On Thu, 8 Dec 2022 at 10:04, artemkulyk @.***> wrote:
Consider avoiding of calling std::list::swap with itself in lines
https://github.com/vsg-dev/VulkanSceneGraph/blob/master/src/vsg/app/CompileManager.cpp#L142 and
https://github.com/vsg-dev/VulkanSceneGraph/blob/master/src/vsg/app/CompileManager.cpp#L169
Currently it works like this:
std::list<ref_ptr
> contexts; contexts = compileTraversal->contexts; compileTraversal->contexts.swap(contexts); // iterate on compileTraversal->contexts compileTraversal->contexts.swap(contexts);
BR, AK
— Reply to this email directly, view it on GitHub https://github.com/vsg-dev/VulkanSceneGraph/issues/632#issuecomment-1341715451, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPOEQYP3CWNE36VAZAIQBLWMEJYXANCNFSM6AAAAAASWNVBBY . You are receiving this because you were mentioned.Message ID: @.***>
It took me a while to provoke a crash, this is what I see on my Kubuntu 22.04 / AMD5700G system, I presume this is the same issue:
Thread 4 "vsgpagedlod" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffdd860640 (LWP 16650)]
0x00005555556a947d in vsg::BindDescriptorSets::compile (this=0x7fffec4d1900, context=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/state/BindDescriptorSet.cpp:88
88 auto& vkd = _vulkanData[context.deviceID];
(gdb) where
#0 0x00005555556a947d in vsg::BindDescriptorSets::compile (this=0x7fffec4d1900, context=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/state/BindDescriptorSet.cpp:88
#1 0x000055555569b9fd in vsg::StateGroup::compile (this=0x7fffec4d1980, context=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/nodes/StateGroup.cpp:59
#2 0x000055555576a43d in vsg::CompileTraversal::apply (this=0x7fffec5a4f68, stateGroup=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/app/CompileTraversal.cpp:211
#3 0x000055555569ce92 in vsg::Inherit<vsg::Group, vsg::StateGroup>::accept (this=0x7fffec4d1980, visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/core/Inherit.h:70
#4 0x000055555569745d in vsg::PagedLOD::t_traverse<vsg::PagedLOD, vsg::Visitor> (node=..., visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/nodes/PagedLOD.h:45
#5 0x0000555555697143 in vsg::PagedLOD::traverse (this=0x7fffec4d1b90, visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/nodes/PagedLOD.h:49
#6 0x000055555576a21a in vsg::CompileTraversal::apply (this=0x7fffec5a4f68, object=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/app/CompileTraversal.cpp:188
#7 0x0000555555685d1a in vsg::Visitor::apply (this=0x7fffec5a4f68, value=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/core/Visitor.cpp:422
#8 0x0000555555685e32 in vsg::Visitor::apply (this=0x7fffec5a4f68, value=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/core/Visitor.cpp:442
#9 0x000055555569a426 in vsg::Inherit<vsg::Node, vsg::PagedLOD>::accept (this=0x7fffec4d1b90, visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/core/Inherit.h:70
#10 0x0000555555693ee9 in vsg::Group::t_traverse<vsg::Group, vsg::Visitor> (node=..., visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/nodes/Group.h:37
#11 0x0000555555693da1 in vsg::Group::traverse (this=0x7fffec4d0770, visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/nodes/Group.h:40
#12 0x000055555576a21a in vsg::CompileTraversal::apply (this=0x7fffec5a4f68, object=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/app/CompileTraversal.cpp:188
#13 0x0000555555685d1a in vsg::Visitor::apply (this=0x7fffec5a4f68, value=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/core/Visitor.cpp:422
#14 0x0000555555685d8a in vsg::Visitor::apply (this=0x7fffec5a4f68, value=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/core/Visitor.cpp:430
#15 0x00005555556950ec in vsg::Inherit<vsg::Node, vsg::Group>::accept (this=0x7fffec4d0770, visitor=...) at /home/robert/Dev/VulkanSceneGraph/include/vsg/core/Inherit.h:70
#16 0x00005555557f9cd2 in vsg::CompileManager::compile(vsg::ref_ptr<vsg::Object>, std::function<bool (vsg::Context&)>) (this=0x7fffec5a4eb0, object=..., contextSelection=...)
at /home/robert/Dev/VulkanSceneGraph/src/vsg/app/CompileManager.cpp:158
#17 0x00005555557ebe98 in operator() (__closure=0x5555567495a8, requestQueue=..., status=..., databasePager=...) at /home/robert/Dev/VulkanSceneGraph/src/vsg/io/DatabasePager.cpp:197
#18 0x00005555557ed916 in std::__invoke_impl<void, vsg::DatabasePager::start()::<lambda(vsg::ref_ptr<vsg::DatabaseQueue>, vsg::ref_ptr<vsg::ActivityStatus>, vsg::DatabasePager&)>, std::reference_wrapper<vsg::ref_ptr<vsg::DatabaseQueue> >, std::reference_wrapper<vsg::ref_ptr<vsg::ActivityStatus> >, std::reference_wrapper<vsg::DatabasePager> >(std::__invoke_other, struct {...} &&) (__f=...)
When doing a code review the the CompileManager::compile(..) method I spotted that the final swap of the CompileTraversal::context list was happening after the CompileTraversal is added back into the thread safe queue to be reused, this gives the opportunity for a another thread to grab the Compiletraversal object when the original thread still hasn't finished modifying it.
The simplest fix for this race condition would be to move the swap to before the add, but in hindsight having a copy and swap happening even when you don't need it i.e. when contextSelection is not used, is inefficient, better to just do this extra work in the rate usage case where it's really needed. So I've refactored the code to use a local lamda function to make it easy to specialize the behavior when using contextSelection.
I have checked in my fix to CompileManagerThreadingFix branch:
https://github.com/vsg-dev/VulkanSceneGraph/tree/CompileManagerThreadingFix
With this fix I have been able to cause a crash. I can't claim this is a proof that I've fixed all the possible race conditions that might be lurking, but I think I have fixed the one that was causing the crash that I provoked earlier.
@artemkulyk and @rolandhill could you test out this branch and see if that fixes the crash you're seeing. If things look solid I'll go ahead and merge the VSG master, the 1.0 branch and start thinking about making a 1.0.2 release...
Hi @robertosfield,
I can't reproduce the crash on the branch CompileManagerThreadingFix. 👍
I'll add a comment here if it will be reproducible again.
BR, AK
Thanks for the testing @artemkulyk.
@rolandhill could you you let me know if my changes work fine, if they do I'll merge with master and the 1.0 branch and close your draft PR which I'm hoping won't be required now.
I wanted to wrap up this work for the day so I've merged the branch with VSG master #634.
I will next bump the version to 1.0.2 and merge with the 1.0 branch. In prep for a 1.0.2 release.
Hi Robert,
Time-zone issue, just seeing this this morning.
I've built and run the new master merged with your fix. It's all running and I haven't experienced any crashes yet. I'll let you know if I do.
Thanks Robert and Artem for your work on this!
Regards, Roland
On Fri, 9 Dec 2022 at 04:08, Robert Osfield @.***> wrote:
Thanks for the testing @artemkulyk https://github.com/artemkulyk.
@rolandhill https://github.com/rolandhill could you you let me know if my changes work fine, if they do I'll merge with master and the 1.0 branch and close your draft PR which I'm hoping won't be required now.
— Reply to this email directly, view it on GitHub https://github.com/vsg-dev/VulkanSceneGraph/issues/632#issuecomment-1343032351, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPOEQZQKUCY5EE32LHL6L3WMIIZHANCNFSM6AAAAAASWNVBBY . You are receiving this because you were mentioned.Message ID: @.***>