Skip to content

Commit bc35c91

Browse files
authored
Fix race conditions in MetalRedrawer (#2672)
Await when all scheduled tasks in async rendering queue are completed before clearing resources. Move surface and renderTarget closing to the critical section. Fixes: https://youtrack.jetbrains.com/issue/CMP-8144/Crash-when-enabling-parallel-rendering-in-MetalRedrawer-for-iOS Fixes: https://youtrack.jetbrains.com/issue/CMP-7598/Skia-crash-on-iOS-GrResourceCachenotifyARefCntReachedZero ## Release Notes ### Fixes - iOS - Fix the crash that may occur when the Compose container disposes.
1 parent 318724b commit bc35c91

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/window/MetalRedrawer.ios.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ internal class MetalRedrawer(
278278
caDisplayLink?.invalidate()
279279
caDisplayLink = null
280280

281+
// Wait until all scheduled rendering tasks are completed to eliminate race conditions
282+
// when clearing the resources
283+
if (useSeparateRenderThreadWhenPossible) {
284+
trace("MetalRedrawer:dispose:waitForAsyncRenderingTasks") {
285+
dispatch_sync(renderingDispatchQueue) {}
286+
}
287+
}
288+
281289
pictureRecorder.close()
282290
context.close()
283291
}
@@ -431,6 +439,9 @@ internal class MetalRedrawer(
431439
picture.close()
432440
surface.flushAndSubmit()
433441

442+
surface.close()
443+
renderTarget.close()
444+
434445
if (useSeparateRenderThreadWhenPossible) {
435446
dispatch_semaphore_signal(drawCanvasSemaphore)
436447
}
@@ -470,9 +481,6 @@ internal class MetalRedrawer(
470481
}
471482
}
472483

473-
surface.close()
474-
renderTarget.close()
475-
476484
// Track current inflight command buffers to synchronously wait for their schedule in case app goes background
477485
inflightCommandBuffers.add(commandBuffer)
478486

0 commit comments

Comments
 (0)