datso / react-native-pjsip

A PJSIP module for React Native.
http://datso.github.io/react-native-pjsip
GNU General Public License v3.0
272 stars 229 forks source link

[Android] Handling crash due to Garbage Collection when in background #152

Closed cmendes0101 closed 5 years ago

cmendes0101 commented 5 years ago

Performing a unregister and register works fine with backgrounding and foregrounding the android app but when the app is backgrounded after a few minutes it seems like GC goes through and destroys the pjsua.

This causes the app to crash when it is brought back into the foreground and endpoint.registerAccount is ran. "A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 30997 (PjSipService), pid 30532 (COM.PACKAGE)"

Is there a good way to work around this? Per some PJSIP bug reports it mentions trying to destroy before GC runs, so I tried to do that with deleting the accounts but still no luck.

This does not happen on iOS, just for android

cmendes0101 commented 5 years ago

Ok well I ended up doing a crazy workaround. It just seems to be the changes with Oreo. I didn't have issues with previous versions.

I made some changes to this repo to allow destroy pjsip when it goes to the background and open/bring to foreground if an incoming call comes in.

It's not the best solution but app no longer crashes due to that and when in the background and receives a call, it quickly can get the notification, register and then bring itself to the foreground to answer/decline. The buggy one is if the app is not running, it will open and send a special param in the Intent to skip my splash screen(which did some initial loading) and go to the home screen that then can take the call. Issue there is I think something in parallel is messing with the call trigger and home screen but hope to clean that up. Another note on opening the non-running app, is it does take a while like an additional 15sec so definitely need to rework to get it to open faster.

I just wanted to post a quick description of my work around. I don't have any acceptable changes to send to a PR for this repo but if I ended up finding a better solution I'll drop something in.

YawenD commented 5 years ago

Hi ,

That happened to me after changing the targetSdkVersion to 26. I have already tried to unregister the PhoneStateChangedReceiver in the onDestroy function but without success.

Can you provide more informations about how you managed to destroy pjsip when it goes into the background please @cmendes0101 ?

cmendes0101 commented 5 years ago

It's pretty horrible, like hacks on top of hacks but if your interested.

I added a stop() to PjSipModule: https://github.com/cmendes0101/react-native-pjsip/blob/master/android/src/main/java/com/carusto/ReactNativePjSip/PjSipModule.java#L38 So when doing AppState.addEventListener() and it goes to background instead of unregister it does this stop() for android only. You can do a diff on my repo to see more plus other random stuff I added.

YawenD commented 5 years ago

Life hack is still better than a popup "app crashed".

Thank you :thumbsup:, I will take a look at your repo.

YawenD commented 5 years ago

Feedback:

If you call the stop method and try to register the lib immediately after, the app crash. So if the user put the app in background and then try to going back in the app to make a call for example, the app crash. I tried to play with a timer on the js side but without success.

So at this moment I just kill the app in the onDestroy function of PjSipService... :grimacing:

Maybe we should keep this issue open since we don't have a real solution.

cmendes0101 commented 5 years ago

Sounds like you have an issue handling on the resume side. I don't have an issue with backgrounding, that actually works pretty well for me. There's a lot I did in my own app lifecycle logic.

I'm happy with my ticket being closed for now since theres a difference between my apps logic and this repo. Might be best to open a new ticket.

If I revisit this and come up with a better solution that I can PR I'll update

YawenD commented 5 years ago

I don't know if you already solve this but with your solution, all calls will ends prematurely if the user accidentally press the home button.

But anyway, thanks for your help :thumbsup:

cmendes0101 commented 5 years ago

Yup, you might have noticed how i kept saying hack over and over and that i had nothing to contribute to the repo 😉