{"action":"opened","issue":{"url":"https://api.github.com/repos/facebook/react-native/issues/7562","repository_url":"https://api.github.com/repos/facebook/react-native","labels_url":"https://api.github.com/repos/facebook/react-native/issues/7562/labels{/name}","comments_url":"https://api.github.com/repos/facebook/react-native/issues/7562/comments","events_url":"https://api.github.com/repos/facebook/react-native/issues/7562/events","html_url":"https://github.com/facebook/react-native/issues/7562","id":154831506,"number":7562,"title":"[iOS] Race condition causing deadlock during app startup due to module initialization process","user":{"login":"rigdern","id":199935,"avatar_url":"https://avatars.githubusercontent.com/u/199935?v=3","gravatar_id":"","url":"https://api.github.com/users/rigdern","html_url":"https://github.com/rigdern","followers_url":"https://api.github.com/users/rigdern/followers","following_url":"https://api.github.com/users/rigdern/following{/other_user}","gists_url":"https://api.github.com/users/rigdern/gists{/gist_id}","starred_url":"https://api.github.com/users/rigdern/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rigdern/subscriptions","organizations_url":"https://api.github.com/users/rigdern/orgs","repos_url":"https://api.github.com/users/rigdern/repos","events_url":"https://api.github.com/users/rigdern/events{/privacy}","received_events_url":"https://api.github.com/users/rigdern/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-14T03:21:54Z","updated_at":"2016-05-14T03:21:54Z","closed_at":null,"body":"I was running my app with commit 68b2f49a314ae07681353d89b89f2a7f28cef10d and I sometimes hit a deadlock during startup. Here's what I learned during debugging.\r\n\r\nThe race condition is in the initialization of the native modules.\r\n\r\nActivity on the main thread:\r\n- The main thread is [looping through the modules to initialize them](https://github.com/facebook/react-native/blob/68b2f49a314ae07681353d89b89f2a7f28cef10d/React/Base/RCTBatchedBridge.m#L375-L392).\r\n- This leads to this thread wanting to initialize the `UIManager`. However, the background thread (below) is already in the middle of initializing the `UIManager`. Consequently, the lock for the module data for the `UIManager` is locked and the main thread waits for the lock to become unlocked (this will never happen).\r\n\r\nActivity on a background thread:\r\n- The background thread is [looping through the modules to collect their configs](https://github.com/facebook/react-native/blob/68b2f49a314ae07681353d89b89f2a7f28cef10d/React/Base/RCTBatchedBridge.m#L413-L418). This is causing modules to become initialized.\r\n- This leads to this thread starting to initialize the `UIManager` (at this point, this thread holds the lock for the module data for the `UIManager` and it will never release it)\r\n- While initializing the `UIManager`, it runs the `UIManager`'s `setBridge` method. [`setBridge` causes the initialization process for the `AccessibilityManager` to start running](https://github.com/facebook/react-native/blob/68b2f49a314ae07681353d89b89f2a7f28cef10d/React/Modules/RCTUIManager.m#L314).\r\n- The `AccessibilityManager` requires the main thread so this thread attempts to run code on the main thread causing the deadlock: the main thread is waiting for this thread to release the `UIManager`'s lock and this thread is waiting to run code on the main thread.\r\n\r\ncc @nicklockwood "}}