Closed KristineTrona closed 10 months ago
Hey @KristineTrona 👋 thanks for raising this issue! Can you copy/paste the code where you're establishing the subscription and cleaning it up?
Hey @chrisbonifacio, thank you for the reply! Of course, here is simplified code:
async componentDidMount() {
await this.initializeSubscriptions();
}
componentWillUnmount() {
this.removeSubscriptions();
}
subscribeToUpdates = async ({ subscription, subscriptionName, input, onSuccess }) => {
const result = await API.graphql(graphqlOperation(subscription, input)).subscribe({
next: ({
value: {
data: { [subscriptionName]: newData },
errors,
},
}) => {
if (!newData) {
return console.log(`Error in subscription data from ${subscriptionName}`, errors);
}
return onSuccess(newData);
},
error: error => console.log(`Error subscribing to ${subscriptionName} updates`, error),
});
return result;
};
initializeSubscriptions = async () => {
const {
user: { id: userId },
} = this.props;
this.onUpdateUser = await this.subscribeToUpdates({
subscription: onUpdateUser,
subscriptionName: 'onUpdateUser',
input: { id: userId },
onSuccess: data => console.log(data),
});
};
removeSubscriptions = async () => {
if (this.onUpdateUser) {
await this.onUpdateUser.unsubscribe();
}
};
If I add the following then I no longer have the crash and subscriptions continue to work after app has come to forgeround again:
async componentDidUpdate(prevProps) {
const { appState } = this.props;
if (prevProps.appState === 'active' && appState.match(/inactive|background/)) {
await this.removeSubscriptions();
}
if (prevProps.appState.match(/inactive|background/) && appState === 'active') {
await this.initializeSubscribers();
}
}
@chrisbonifacio I've noticed now that even with the appState
listener workaround and removing all subscriptions when appstate goes to background, if I lock the app screen and reopen the app a couple of times the error still happens.
This last bit I have only been able to reproduce on iPad so far (testing on iPad mini 2, but also seen it happen on iPad mini4, iPad Air 2, iPad 7). Have not been able to get such a memory leak on an iphone.
@KristineTrona , I was unable to reproduce this issue. If possible can you provide more info about your data model?
I have the same error.
React Native doesn't provider a friendly error message, it just returns Uncaught Error - Unknown
.
I managed to get more info on the error using react-native-exception-handler
package.
Here's my subscription code.
React.useEffect(() => {
const categoryCreateSubscription = await
API.graphql(graphqlOperation(onCreateCategory)).subscribe({
next: () => {
console.log('do something...')
},
});
return () => {
categoryCreateSubscription.unsubscribe();
}, []);
Going on background and launching again to foreground crashes the app, even in production.
I have same issue as sydiaplatform.
@thenderson55 try and add the error callback, that stopped the app to crash, however, you may want to debug further as to why you are receiving the error. I believe Android does not hold memory for long which could be the cause of the memory leak.
API.graphql(graphqlOperation(onCreateCategory)).subscribe({
next: () => {
console.log('do something...')
},
error => console.warn(error)
});
@KristineTrona, @thenderson55 did you ever find a solution for this issue? I am experiencing the same thing.
@WilsonWilson
The temporary solution from sydiaplatform seems to work. Adding in some error handling.
Edit: Actually it also then creates a new issue. If switch screen on and off breaks again. Can't figure out how to handle the error and resubscribe without breaking.
Did anybody find a solution to how to handle the error correctly?
Apologies for the delayed response. This issue was opened while using v4 of the AWS JS Library, and we've since refactored the reconnection logic and are now on v6. We will attempt to reproduce this in v6 but would appreciate if anyone can confirm if upgrading might've resolved for them already.
With the release of the latest major version of Amplify (aws-amplify@>6), we highly recommend updating to the most recent version. Please refer to our release announcement, migration guide, and documentation for more information.
If anyone following this issue upgrades and still experiences the problem, please comment back and we can reopen.
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
GraphQL API
Amplify Categories
api
Environment information
Describe the bug
Hello,
I use GraphQL with API subscriptions in a RN app and noticed that after upgrading to version 0.64.2 they are causing a memory leak in release builds whenever the device screen gets locked and then unlocked while the app was running in foreground.
After unlocking the device screen the app becomes unresponsive for a few seconds and then crashes. In the error logs I see
Out of memory
reported. This only happens on iOS, could not reproduce this problem on Android.I found a temporary workaround by unsubscribing to any updates when appState changes to "inactive" or "background" state and resubscribing, when app comes to foreground, but this is not ideal.
Expected behavior
App does not crash when it comes to foreground after device is unlocked.
Reproduction steps
API
Mobile Device
iPhone 12 mini
Mobile Operating System
iOS14.6