From 909b6825479fc144fb09ca9406f00db6daab92cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Mon, 13 Oct 2025 14:33:53 +0200 Subject: [PATCH] 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 --- intern/ghost/intern/GHOST_ContextMTL.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/intern/ghost/intern/GHOST_ContextMTL.mm b/intern/ghost/intern/GHOST_ContextMTL.mm index 66da5873514..cb34a8ebc45 100644 --- a/intern/ghost/intern/GHOST_ContextMTL.mm +++ b/intern/ghost/intern/GHOST_ContextMTL.mm @@ -21,6 +21,7 @@ #import #include +#include #include 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_) {