Metal: Fix race condition in ghost context destruction routines

Found when running with ASAN on through Xcode. Crashed on exit.

Multiple threads can release their own context at the same time
and thus release the `metal_layer_` multiple times, resulting
in use after free.

Pull Request: https://projects.blender.org/blender/blender/pulls/147972
This commit is contained in:
Clément Foucault
2025-10-13 14:33:53 +02:00
committed by Clément Foucault
parent 633102c219
commit 909b682547

View File

@@ -21,6 +21,7 @@
#import <QuartzCore/QuartzCore.h>
#include <cassert>
#include <mutex>
#include <vector>
static const MTLPixelFormat METAL_FRAMEBUFFERPIXEL_FORMAT_EDR = MTLPixelFormatRGBA16Float;
@@ -121,6 +122,10 @@ GHOST_ContextMTL::GHOST_ContextMTL(const GHOST_ContextParams &context_params,
GHOST_ContextMTL::~GHOST_ContextMTL()
{
/* Multiple threads can release their own context at the same time. */
static std::mutex mutex;
std::scoped_lock lock(mutex);
metalFree();
if (owns_metal_device_) {