Skip to content

fix(🍏): run CAMetalLayer operation on main thread#280

Draft
wcandillon wants to merge 1 commit intomainfrom
metal-ui-thread
Draft

fix(🍏): run CAMetalLayer operation on main thread#280
wcandillon wants to merge 1 commit intomainfrom
metal-ui-thread

Conversation

@wcandillon
Copy link
Owner

fixes #244

@wcandillon wcandillon marked this pull request as draft October 30, 2025 13:08
@jcarioti
Copy link

These changes are still pending release, huh?

@wcandillon
Copy link
Owner Author

yes we are not 100% of the approach we want to take here yet.

void _configure() {
void _invokeOnMainThread(const std::function<void()> &task) {
#ifdef __APPLE__
RunOnMainThreadSync(task);
Copy link
Contributor

@bhamiltoncx bhamiltoncx Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not entirely safe. The caller holds _mutex, but the main thread might itself be blocked waiting for _mutex.

Example, just after a background thread takes _mutex and then wants to dispatch to the main thread:

  1. RN updates view props
  2. RN calls (on the UI thread) -[WebGPUView updateProps:oldProps:]
  3. WebGPUView calls -[MetalView configure]
  4. -[MetalView configure] calls SurfaceRegistry.switchToOnscreen()
  5. SurfaceRegistry.switchToOnscreen() tries to take _mutex and blocks

In this scenario, the dispatch_sync_f() to the main thread will deadlock.

I think this needs to move to a queue-based system where changes to the surface and/or configuration need to be queued up and then applied in order (which must happen on the main thread on Apple platforms).

@bhamiltoncx
Copy link
Contributor

Left you a comment — I think this approach will deadlock if RN reconfigures view props on the main thread at the same time as the synchronous dispatch.

A queue-based system might be smarter to apply all surface/configuration changes in order on the main thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS Layer Modifications Off Main Thread

3 participants