Skip to content

[iOS] Remove dropped native handlers - #3819

Merged
m-bert merged 6 commits into
nextfrom
@mbert/fix-native-attach
Nov 17, 2025
Merged

m-bert merged 6 commits into
nextfrom
@mbert/fix-native-attach

Conversation

@m-bert

@m-bert m-bert commented Nov 13, 2025 •

Copy link
Copy Markdown
Collaborator

What is happening

While working on #3800 I've noticed frequent crashes on iOS. I'll explain this using the demo from the test code, basically two screens, second screen has a RectButton.

  1. Navigate to second screen
  2. Go back
  3. Navigate to the second screen again
  4. App crashes

Sometime it crashes immediately on startup.

Why is it happening?

Native handlers are handled differently compared to others. The main problem was that we tried to attach handler that was previously dropped, but NativeDetector has no information that this happened. The flow here looks as follows:

  1. We navigate to screen with button
  2. addSubview is called - at this point it won't attach anything as _nativeHandlers is empty
  3. updateProps is called - handlers are attached
  4. We navigate back to first screen
  5. Handler is dropped
  6. We navigate again to second screen
  7. addSubview is called - it tries to attach handler that was dropped, therefore app crashes

Solution

To fix this issue, we clear _nativeHandlers in prepareForRecycle method.

Test plan

I've tested it on my branch with components re-written to new API, using the following code in EmptyExample:
import React from 'react';
import { StyleSheet, View } from 'react-native';
import { RectButton } from 'react-native-gesture-handler';

export default function EmptyExample() {
  return (
    <View style={styles.container}>
      <RectButton
        style={styles.button}
        onPress={() => console.log('Hello World!')}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  button: {
    width: 100,
    height: 30,
    borderRadius: 10,
    backgroundColor: 'pink',
  },
});

@j-piasecki

j-piasecki commented Nov 14, 2025 •

Copy link
Copy Markdown
Member

Hmm, I see that we already disabled recycling only on macOS. Disabling that would be ok, but a better, and correct, thing to do would be to reset all local fields inside prepareForRecycle. It can stay disabled on macOS, I'm sure there was a reason why our components do that but don't recall it.

@m-bert

m-bert commented Nov 14, 2025

Copy link
Copy Markdown
Collaborator Author

Disabling that would be ok, but a better, and correct, thing to do would be to reset all local fields inside prepareForRecycle.

For now I've cleared only _nativeHandlers, as other sets seem to be correctly cleared at this point. WDYT?

@j-piasecki

Copy link
Copy Markdown
Member

For now I've cleared only _nativeHandlers, as other sets seem to be correctly cleared at this point. WDYT?

Can everything be reset? I shouldn't impact performance in a meaningful way and will likely prevent some nasty bugs in the future, where that assumption may no longer hold.

@m-bert
m-bert merged commit 2a90922 into next Nov 17, 2025
3 checks passed
@m-bert
m-bert deleted the @mbert/fix-native-attach branch November 17, 2025 10:13
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.

2 participants