From 4b2f381d237a4468c4dec71209bb8c68c9399640 Mon Sep 17 00:00:00 2001 From: Andrei Salavei Date: Fri, 9 Jan 2026 11:54:59 +0100 Subject: [PATCH] Fix race conditions in MetalRedrawer --- .../compose/ui/window/MetalRedrawer.ios.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt b/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt index ad73f6ebf05b1..1547468c7e75f 100644 --- a/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt +++ b/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt @@ -278,6 +278,14 @@ internal class MetalRedrawer( caDisplayLink?.invalidate() caDisplayLink = null + // Wait until all scheduled rendering tasks are completed to eliminate race conditions + // when clearing the resources + if (useSeparateRenderThreadWhenPossible) { + trace("MetalRedrawer:dispose:waitForAsyncRenderingTasks") { + dispatch_sync(renderingDispatchQueue) {} + } + } + pictureRecorder.close() context.close() } @@ -431,6 +439,9 @@ internal class MetalRedrawer( picture.close() surface.flushAndSubmit() + surface.close() + renderTarget.close() + if (useSeparateRenderThreadWhenPossible) { dispatch_semaphore_signal(drawCanvasSemaphore) } @@ -470,9 +481,6 @@ internal class MetalRedrawer( } } - surface.close() - renderTarget.close() - // Track current inflight command buffers to synchronously wait for their schedule in case app goes background inflightCommandBuffers.add(commandBuffer)