Reloading an app while a native animation is in progress results in a crash
Environment
Run the following in your terminal and copy the results here.
npx react-native --version: 4.13.0npx react-native run-windows --info: System: OS: Windows 10 10.0.19042 CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor Memory: 13.70 GB / 31.91 GB Binaries: Node: 10.21.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.4 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.14.4 - C:\Program Files\nodejs\npm.CMD npmPackages: react: 16.13.1 => 16.13.1 react-native: 0.63.3 => 0.63.3 react-native-windows: ^0.63.0-0 => 0.63.5 Installed UWP SDKs: 10.0.18362.0 10.0.19041.0reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock AllowDevelopmentWithoutDevLicense REG_DWORD 0x1 AllowAllTrustedApps REG_DWORD 0x1
Steps To Reproduce
Reload an app while an animation which uses native driver is running.
- Create a new react native app (typescript template) with the instructions here https://microsoft.github.io/react-native-windows/docs/getting-started
- Replace App.tsx with the code bellow
- launch the app
- Press 'r' in the metro console to reload the app
Expected Results
App reloads and does not crash.
Note this does not reproduce on rnw 0.60
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, { useEffect } from 'react';
import { useState } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Animated,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
declare const global: {HermesInternal: null | {}};
const App = () => {
const [opacity] = useState(new Animated.Value(1));
const bodyStyle = {
...styles.body,
opacity
};
useEffect(() => {
Animated.timing(opacity, {
toValue: 0,
duration: 10000000,
useNativeDriver: true,
}).start();
}, [])
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<Header />
{global.HermesInternal == null ? null : (
<View style={styles.engine}>
<Text style={styles.footer}>Engine: Hermes</Text>
</View>
)}
<Animated.View style={bodyStyle}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Step One</Text>
<Text style={styles.sectionDescription}>
Edit <Text style={styles.highlight}>App.tsx</Text> to change this
screen and then come back to see your edits.
</Text>
</View>
<LearnMoreLinks />
</Animated.View>
</ScrollView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;
I debugged further into this but did not find a solution. Just posting what I observed in case it can save anyone some time.
Looking at why this does not crash in 0.60 was very informative. In 0.60 this destructor is running on the UI thread, kind of obvious but 'why' it was running on the ui thread is more interesting.
AnimationDriver::~AnimationDriver() {
if (m_scopedBatch)
m_scopedBatch.Completed(m_scopedBatchCompletedToken);
}
In 0.60 the AnimationDriver is destroyed in a callstack originating from stopAnimation on the AnimatedValue, whereas in 0.63 the AnimationDriver destructor is running when the AnimatedModule is getting destroyed. The call to stopAnimation occurs in componentWillUnmount from createAnimatedComponent.
0.63 is missing the call to AppRegistry.unmountApplicationComponentAtRootTag (at least when using ReactRootControl). In RNW this function is getting called during DetachRootView
It looks like it gets called https://github.com/microsoft/react-native-windows/blob/29eeaae16d920fb7bd7d9b96e9fc84e0be49e829/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp#L216
if (auto fbReactInstance = m_fbReactInstance.lock()) {
fbReactInstance->DetachRootView(this);
}
But m_fbReactinstance is always empty.
I tried out changing that snippet to this.
if (auto reactInstance = m_weakReactInstance.GetStrongPtr()) {
auto& legacyInstance = query_cast<Mso::React::ILegacyReactInstance&>(*reactInstance);
legacyInstance.DetachRootView(this);
}
This results in unmountApplicationComponentAtRootTag correctly getting called but then there is an error in PropsAnimatedNode.cpp during stopAnimation
at this point instance->NativeUIManager() is nullptr
ShadowNodeBase *PropsAnimatedNode::GetShadowNodeBase() {
if (const auto instance = m_instance.lock()) {
if (const auto nativeUIManagerHost = static_cast<NativeUIManager *>(instance->NativeUIManager())->getHost()) {
return static_cast<ShadowNodeBase *>(nativeUIManagerHost->FindShadowNodeForTag(m_connectedViewTag));
}
}
return nullptr;
}