Repository: godot
Issue: Potential race in VulkanContext (TSAN finding)
Labels: bug,topic:rendering

### Godot version

4.0.2

### System information

Linux

### Issue description

@clayjohn [suggested](https://github.com/godotengine/godot/issues/71929#issuecomment-1528075917) that https://github.com/godotengine/godot/issues/71929#issuecomment-1528075917 may be caused by a synchronization issue, so I tried running my example project under TSAN.

I ran into an issue running TSAN with nvidia drivers (https://github.com/google/sanitizers/issues/1647), so I ended up using [lavapipe](https://wiki.archlinux.org/title/Vulkan#Software_Vulkan:_lavapipe) instead, as suggested by @Calinou.

I did get a TSAN finding in `VulkanContext::_initialize_queues`. I'm not quite following the report. It seems that `initialize_queues` somehow creates two threads, though I'm not sure how, possibly because I'm missing symbols for `libvulkan_lvp.so`? 
Feel free to close this if it is noise, but I figured it could be relevant since some error messages in https://github.com/godotengine/godot/issues/71929#issuecomment-1528075917 mention the graphics queue.

<details>
<summary>One example</summary>

WARNING: ThreadSanitizer: data race (pid=22868)
  Write of size 1 at 0x7b1c000637a8 by thread T15 (mutexes: write M0):
    #0 pthread_mutex_destroy <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x561e81) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0x331972) (BuildId: cf1adadfe59188457be25f35e22287583767a044)

  Previous atomic read of size 1 at 0x7b1c000637a8 by thread T22:
    #0 pthread_mutex_lock <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x57d641) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0x235f76) (BuildId: cf1adadfe59188457be25f35e22287583767a044)

  Location is heap block of size 112 at 0x7b1c000637a0 allocated by thread T15:
    #0 calloc <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x55debb) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0x234f3b) (BuildId: cf1adadfe59188457be25f35e22287583767a044)

  Mutex M0 (0x7b6000002258) created at:
    #0 pthread_mutex_init <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x5754b8) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0x5b510) (BuildId: cf1adadfe59188457be25f35e22287583767a044)
    #2 VulkanContext::_window_create(int, DisplayServer::VSyncMode, VkSurfaceKHR_T*, int, int) /home/rcorre/src/godot/godot/drivers/vulkan/vulkan_context.cpp:1680:15 (godot.linuxbsd.editor.x86_64.llvm.san+0x26ed88b) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #3 VulkanContextX11::window_create(int, DisplayServer::VSyncMode, unsigned long, _XDisplay*, int, int) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/vulkan_context_x11.cpp:56:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x651922) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #4 DisplayServerX11::_create_window(DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Rect2i const&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5144:32 (godot.linuxbsd.editor.x86_64.llvm.san+0x5fe032) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #5 DisplayServerX11::DisplayServerX11(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5551:25 (godot.linuxbsd.editor.x86_64.llvm.san+0x6175bb) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #6 DisplayServerX11::create_func(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:4845:22 (godot.linuxbsd.editor.x86_64.llvm.san+0x615921) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #7 DisplayServer::create(int, String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/servers/display_server.cpp:904:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x5c384f4) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #8 Main::setup2(unsigned long) /home/rcorre/src/godot/godot/main/main.cpp:2001:20 (godot.linuxbsd.editor.x86_64.llvm.san+0x689068) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #9 Main::setup(char const*, int, char**, bool) /home/rcorre/src/godot/godot/main/main.cpp:1879:10 (godot.linuxbsd.editor.x86_64.llvm.san+0x681b60) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #10 main /home/rcorre/src/godot/godot/platform/linuxbsd/godot_linuxbsd.cpp:61:14 (godot.linuxbsd.editor.x86_64.llvm.san+0x5d2bef) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)

  Thread T15 (tid=22927, running) created by main thread at:
    #0 pthread_create <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x550566) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0xcaab5) (BuildId: cf1adadfe59188457be25f35e22287583767a044)
    #2 VulkanContext::_initialize_queues(VkSurfaceKHR_T*) /home/rcorre/src/godot/godot/drivers/vulkan/vulkan_context.cpp:1528:2 (godot.linuxbsd.editor.x86_64.llvm.san+0x26ec928) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #3 VulkanContext::_window_create(int, DisplayServer::VSyncMode, VkSurfaceKHR_T*, int, int) /home/rcorre/src/godot/godot/drivers/vulkan/vulkan_context.cpp:1688:15 (godot.linuxbsd.editor.x86_64.llvm.san+0x26ed8b3) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #4 VulkanContextX11::window_create(int, DisplayServer::VSyncMode, unsigned long, _XDisplay*, int, int) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/vulkan_context_x11.cpp:56:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x651922) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #5 DisplayServerX11::_create_window(DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Rect2i const&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5144:32 (godot.linuxbsd.editor.x86_64.llvm.san+0x5fe032) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #6 DisplayServerX11::DisplayServerX11(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5551:25 (godot.linuxbsd.editor.x86_64.llvm.san+0x6175bb) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #7 DisplayServerX11::create_func(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:4845:22 (godot.linuxbsd.editor.x86_64.llvm.san+0x615921) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #8 DisplayServer::create(int, String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/servers/display_server.cpp:904:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x5c384f4) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #9 Main::setup2(unsigned long) /home/rcorre/src/godot/godot/main/main.cpp:2001:20 (godot.linuxbsd.editor.x86_64.llvm.san+0x689068) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #10 Main::setup(char const*, int, char**, bool) /home/rcorre/src/godot/godot/main/main.cpp:1879:10 (godot.linuxbsd.editor.x86_64.llvm.san+0x681b60) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #11 main /home/rcorre/src/godot/godot/platform/linuxbsd/godot_linuxbsd.cpp:61:14 (godot.linuxbsd.editor.x86_64.llvm.san+0x5d2bef) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)

  Thread T22 'llvmpipe-6' (tid=22934, running) created by main thread at:
    #0 pthread_create <null> (godot.linuxbsd.editor.x86_64.llvm.san+0x550566) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #1 <null> <null> (libvulkan_lvp.so+0x7042b) (BuildId: cf1adadfe59188457be25f35e22287583767a044)
    #2 VulkanContext::_initialize_queues(VkSurfaceKHR_T*) /home/rcorre/src/godot/godot/drivers/vulkan/vulkan_context.cpp:1528:2 (godot.linuxbsd.editor.x86_64.llvm.san+0x26ec928) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #3 VulkanContext::_window_create(int, DisplayServer::VSyncMode, VkSurfaceKHR_T*, int, int) /home/rcorre/src/godot/godot/drivers/vulkan/vulkan_context.cpp:1688:15 (godot.linuxbsd.editor.x86_64.llvm.san+0x26ed8b3) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #4 VulkanContextX11::window_create(int, DisplayServer::VSyncMode, unsigned long, _XDisplay*, int, int) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/vulkan_context_x11.cpp:56:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x651922) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #5 DisplayServerX11::_create_window(DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Rect2i const&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5144:32 (godot.linuxbsd.editor.x86_64.llvm.san+0x5fe032) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #6 DisplayServerX11::DisplayServerX11(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:5551:25 (godot.linuxbsd.editor.x86_64.llvm.san+0x6175bb) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #7 DisplayServerX11::create_func(String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/platform/linuxbsd/x11/display_server_x11.cpp:4845:22 (godot.linuxbsd.editor.x86_64.llvm.san+0x615921) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #8 DisplayServer::create(int, String const&, DisplayServer::WindowMode, DisplayServer::VSyncMode, unsigned int, Vector2i const*, Vector2i const&, int, Error&) /home/rcorre/src/godot/godot/servers/display_server.cpp:904:9 (godot.linuxbsd.editor.x86_64.llvm.san+0x5c384f4) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #9 Main::setup2(unsigned long) /home/rcorre/src/godot/godot/main/main.cpp:2001:20 (godot.linuxbsd.editor.x86_64.llvm.san+0x689068) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #10 Main::setup(char const*, int, char**, bool) /home/rcorre/src/godot/godot/main/main.cpp:1879:10 (godot.linuxbsd.editor.x86_64.llvm.san+0x681b60) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)
    #11 main /home/rcorre/src/godot/godot/platform/linuxbsd/godot_linuxbsd.cpp:61:14 (godot.linuxbsd.editor.x86_64.llvm.san+0x5d2bef) (BuildId: 4d838840588e872acec3016b35eaee94aed07d07)

</details>

[full report](https://github.com/godotengine/godot/files/11542982/tsan.txt)


### Steps to reproduce

1. Build godot with TSAN (`scons debug_symbols=yes use_llvm=yes use_static_cpp=no target=editor use_tsan=yes`
2. Navigate to the example project
3. Run `LIBGL_ALWAYS_SOFTWARE=1 __GLX_VENDOR_LIBRARY_NAME=mesa VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.x86_64.json:/usr/share/vulkan/icd.d/lvp_icd.x86_64.json godot.linuxbsd.editor.x86_64.llvm.san Halls.tscn 2>&1 | tee /tmp/tsan.txt`

### Minimal reproduction project

https://github.com/godotengine/godot/files/11348650/example.zip