question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RCTScreenScale may deadlock

See original GitHub issue

RCTScreenScale will deadlock if it called for the first time on a non-main thread and the main thread is waiting synchronously for the thread on which RCTScreenScale is called, because the dispatch_sync(dispatch_get_main_queue(), ...) in RCTUnsafeExecuteOnMainQueueOnceSync will deadlock in that scenario. I don’t know whether this can actually happen in a React Native app, but if not it should probably be commented on in the code (e.g. to prevent people from copying the code without taking this issue into account).

Because of this issue, having a general helper function like RCTUnsafeExecuteOnMainQueueOnceSync is problematic. For use cases like RCTScreenScale it’s usually better to do the initialization work eagerly at app startup or library load time (e.g. in an Objective-C category load method).

Apart from this general issue, RCTUnsafeExecuteOnMainQueueOnceSync also misses a compiler barrier in the second if statement, see the dispatch_once inline function implementation in more recent libdispatch versions, e.g. https://github.com/apple/swift-corelibs-libdispatch/blob/b5ec5d8dfa59426912fbb4884fab642804b99827/dispatch/once.h#L86

Also, if you copy the internals of dispatch_once, I’d use exactly the same predicate test against ~0L instead of 0, just in case that it may matter for some obscure reason.

Finally, I’d like to point out that RCTScreenScale currently doesn’t handle the case where the scale of the mainScreen changes, which I think could happen on the Apple TV.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sophiebitscommented, Feb 26, 2018

@shergin Care to comment? I believe that we never dispatch synchronously from the main thread to this RN thread, precisely to avoid deadlock, but I am not sure.

It’s true that screen size can change and I don’t think our APIs are really set up to accommodate that at the moment. 😦

0reactions
Titozzzcommented, Mar 19, 2019

@shergin Do you think we can close this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

RCTBridge required dispatch_sync to load ... - Stack Overflow
RCTBridge required dispatch_sync to load RNGestureHandlerModule. This may lead to deadlocks · Ask Question. Asked 9 months ago.
Read more >
dispatch_once wait crash
Attempting to synchronously execute a work item on the main queue results in deadlock. Avoiding Excessive Thread Creation. When designing tasks for concurrent ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found