ionic-team / ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
https://ionicframework.com
MIT License
51.02k stars 13.51k forks source link

bug: app restarting in background when InAppBrowser is opened (when low memory) #28786

Closed chvonrohr closed 9 months ago

chvonrohr commented 9 months ago

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

When within the Ionic app a InAppBrowser is opened and user is closing the InAppBrowser, it may happen that original app is restarting (showing the splash screen of the app again). Clearly, this only affects devices that are low on memory, but I would expect, that the app which opens (and hosts?) the InAppBrowser can't be restarted.

As we use Auth0 Universal Login - and the recommended way opening the login in the InAppBrowser to have a https context - our users sometimes can't login as the deep link with the session information is sent back to the app that can't use that information. I assume, also Ionic Auth Connect has the same issue, because it's opening the login with InAppBrowser?

Expected Behavior

App should now be allowed to restart.

Steps to Reproduce

  1. Use ionic start command to generate a new Ionic Capacitor app with Angular (tabs)
  2. Install recommended packages from quick start guide: npm i @capacitor/app @capacitor/haptics @capacitor/keyboard @capacitor/status-bar
  3. install browser plugin npm i @capacitor/browser
  4. Add Android platform ionic capacitor add android
  5. On Tab 3 add a link that opens a random page: Browser.open({ url: 'https://www.google.com' });
  6. Start app in an Android emulator with low memory: I used Pixel 1 with API 28 and 1500MB RAM
  7. Open the app, navigate to tab 3 and open the browser
  8. Sometimes this is already enough and closing the InAppBrowser shows you the splash screen of your app. Sometimes, it needs a bit more like opening some other apps and returning to app (like user goes to SMS app to read OTP code). See video for example.

https://github.com/ionic-team/ionic-framework/assets/1733057/f04d12a1-0eba-45f2-a4f7-e4956c5706f1

Code Reproduction URL

No response

Ionic Info

Ionic:

Ionic CLI : 7.1.5 (/Users/chvonrohr/.nvm/versions/node/v18.16.0/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 7.6.3 @angular-devkit/build-angular : 17.0.9 @angular-devkit/schematics : 17.0.9 @angular/cli : 17.0.9 @ionic/angular-toolkit : 9.0.0

Capacitor:

Capacitor CLI : 5.6.0 @capacitor/android : 5.6.0 @capacitor/core : 5.6.0 @capacitor/ios : not installed

Utility:

cordova-res : not installed globally native-run : 2.0.0

System:

NodeJS : v18.16.0 (/Users/chvonrohr/.nvm/versions/node/v18.16.0/bin/node) npm : 10.2.4 OS : macOS Unknown

Additional Information

No response

ionitron-bot[bot] commented 9 months ago

Thanks for the issue! This issue has been labeled as holiday triage. With the winter holidays quickly approaching, much of the Ionic Team will soon be taking time off. During this time, issue triaging and PR review will be delayed until the team begins to return. After this period, we will work to ensure that all new issues are properly triaged and that new PRs are reviewed. In the meantime, please read our Winter Holiday Triage Guide for information on how to ensure that your issue is triaged correctly. Thank you!

ionitron-bot[bot] commented 9 months ago

Thanks for the issue! This issue has been labeled as needs reproduction. This label is added to issues that need a code reproduction.

Please reproduce this issue in an Ionic starter application and provide a way for us to access it (GitHub repo, StackBlitz, etc). Without a reliable code reproduction, it is unlikely we will be able to resolve the issue, leading to it being closed.

If you have already provided a code snippet and are seeing this message, it is likely that the code snippet was not enough for our team to reproduce the issue.

For a guide on how to create a good reproduction, see our Contributing Guide.

chvonrohr commented 9 months ago

@liamdebeasi here is the codebase I used for testing: https://github.com/chvonrohr/ionic-issue-28786 (basically adding Android platform and button for opening Browser in Tab3).

Important to use low-memory emulator in tests.

liamdebeasi commented 9 months ago

I'm not able to reproduce the issue using a Pixel emulator with <1500MB of RAM. Are you able to identify the part of Ionic that is causing the added memory pressure?

chvonrohr commented 9 months ago

Not sure if the used memory is really a big issue here or if we are able to reduce it. Android Studio Profiler tells, that the app (Ionic Tabs) is using around 80MB, whereas our app is around 120MB RAM (both having that issue).

The main issue is, that when the InAppBrowser is open, the OS may close the host app in the background. We track this issue on production with Sentry and see that there are quite some users are affected. The app restarts after they signed in within the InAppBrowser Auth0 Login and the Browser closes with the deep link response. Then, the login session is not valid anymore, and they try to log in again and again. When we receive requests about that on the support line, we tell them atm to restart the device. Sometimes this works out, sometimes it doesn't.

Isn't this the same flow Ionic App Connect uses to identify with Auth0 and aren't having you similar issues there too? Do you know, if it's possible to tell the OS when opening CustomTabsIntent that the host app is not restartable, because app should be seen as "active" too when the browser is?

liamdebeasi commented 9 months ago

The OS typically kills the web process (thus causing it to restart) when the memory usage is too high and hits a pre-determined threshold. For example, the starter app may use 80 MB of memory, but perhaps the InAppBrowser is causing a temporary spike that, under the right circumstances, causes the app to consume too much memory.

Unfortunately, I'm not going to be able to help unless I can reproduce the issue on my end. If a reproducible example can't be created, then you'll need to do some debugging to identify the root cause of the issue.


Isn't this the same flow Ionic App Connect uses to identify with Auth0 and aren't having you similar issues there too? Do you know, if it's possible to tell the OS when opening CustomTabsIntent that the host app is not restartable, because app should be seen as "active" too when the browser is?

I don't work on Auth Connect, so I am not sure. However, we have not received any reports of apps restarting due to high memory used inside of Ionic Framework when using that product.

ionitron-bot[bot] commented 9 months ago

Thanks for the issue! This issue is being closed due to the lack of a reply. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Thank you for using Ionic!