Skip to content

Mitigate device info main queue modules crash#51662

Closed
RSNara wants to merge 1 commit into
facebook:mainfrom
RSNara:export-D75537894
Closed

Mitigate device info main queue modules crash#51662
RSNara wants to merge 1 commit into
facebook:mainfrom
RSNara:export-D75537894

Conversation

@RSNara
Copy link
Copy Markdown
Contributor

@RSNara RSNara commented May 28, 2025

Summary:

Changes

DeviceInfo: Deafult fontScale to 1 when accessibility manager isn't available

Analysis

The problem: For reasons that we don't understand, accessibility manager is sometimes nil, when deviceinfo needs it. This is a long-standing issue in react native.

  _constants = @{
    @"Dimensions" : [self _exportedDimensions],
    // Note:
    // This prop is deprecated and will be removed in a future release.
    // Please use this only for a quick and temporary solution.
    // Use <SafeAreaView> instead.
    @"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()),
  };
- (NSDictionary *)_exportedDimensions
{
  RCTAssert(!_invalidated, @"Failed to get exported dimensions: RCTDeviceInfo has been invalidated");
  RCTAssert(_moduleRegistry, @"Failed to get exported dimensions: RCTModuleRegistry is nil");
  RCTAccessibilityManager *accessibilityManager =
      (RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"];
  if (!accessibilityManager) {
    return nil;
  }
  CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
  return RCTExportedDimensions(fontScale);
}

The crash: When accessibility manager is nil, device info tries to insert nil into an NSDictionary, and crashes.

We found a possible repro of this issue: launch facebook, log out, and log back in.

Why this surged now: Recently, we started initializing device info during react native init. That means this code-path just runs much more often.

Mitigation

Remove the return nil in [self _exportedDimensions]. Instead, just default the fontScale to 1 when the accessibility manager isn't available.

This should be safe: If we assume that accessibility manager eventually becomes available, the fontScale will eventually become correct. Device info will send the updated fontScale to js when it becomes available: native, js.

Long-term fix

Understand why accessibility manager is nil.

Changelog: [Internal]

Differential Revision: D75537894

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Facebook Partner: Facebook Partner labels May 28, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D75537894

Summary:
## Changes
DeviceInfo: Deafult fontScale to 1 when accessibility manager isn't available

# Analysis
**The problem:** For reasons that we don't understand, accessibility manager is sometimes nil, when deviceinfo needs it. This is a long-standing issue in react native.

```
  _constants = @{
    @"Dimensions" : [self _exportedDimensions],
    // Note:
    // This prop is deprecated and will be removed in a future release.
    // Please use this only for a quick and temporary solution.
    // Use <SafeAreaView> instead.
    @"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()),
  };
```

```
- (NSDictionary *)_exportedDimensions
{
  RCTAssert(!_invalidated, @"Failed to get exported dimensions: RCTDeviceInfo has been invalidated");
  RCTAssert(_moduleRegistry, @"Failed to get exported dimensions: RCTModuleRegistry is nil");
  RCTAccessibilityManager *accessibilityManager =
      (RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"];
  if (!accessibilityManager) {
    return nil;
  }
  CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
  return RCTExportedDimensions(fontScale);
}

```

**The crash:** When accessibility manager is nil, device info tries to insert nil into an NSDictionary, and crashes.

We found a possible repro of this issue: [launch facebook, log out, and log back in](https://www.internalfb.com/diff/D72020801?transaction_fbid=982516293994114).

**Why this surged now:** Recently, we started initializing device info during react native init. That means this code-path just runs much more often.

# Mitigation
Remove the return nil in `[self _exportedDimensions]`. Instead, just default the fontScale to 1 when the accessibility manager isn't available.

**This should be safe:** If we assume that accessibility manager eventually becomes available, the fontScale will eventually become correct. Device info will send the updated fontScale to js when it becomes available: [native](https://www.internalfb.com/code/fbsource/[ec6fd664a9cd]/xplat/js/react-native-github/packages/react-native/React/CoreModules/RCTDeviceInfo.mm?lines=231-232), [js](https://www.internalfb.com/code/fbsource/[ec6fd664a9cd]/xplat/js/react-native-github/packages/react-native/Libraries/Utilities/Dimensions.js?lines=120-125).

# Long-term fix
Understand why accessibility manager is nil.

Changelog: [Internal]

Differential Revision: D75537894
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D75537894

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @RSNara in 0b8db7e

When will my fix make it into a release? | How to file a pick request?

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 0b8db7e.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants