MaikuB / flutter_appauth

A Flutter wrapper for AppAuth iOS and Android SDKs
270 stars 243 forks source link

Login fails due to Java.lang.NullPointerException, Attempt to invoke virtual method on a null object reference #341

Closed shihaohong closed 1 year ago

shihaohong commented 2 years ago

On our Android app, we've been running into intermittent crashers related to null exceptions involving the Android Activity being null. We suspect that this is caused by the OS killing the activity before the user completes login.

Steps to reproduce

  1. On your emulator or device, turn on "Don't keep activities"
  2. Clone down flutter_appauth's example repo and run the example app with emulator or device with "Don't keep activities" enabled.
    git clone https://github.com/MaikuB/flutter_appauth.git
    cd flutter_appauth/flutter_appauth/example
    flutter run
  3. Attempt to login with auto code exchange. The first time, it'll fail. It only works on the second attempt. I'm assuming is because the activity doesn't get killed as the custom chrome tab closes quickly enough (as the user remains logged in) before the activity gets the chance to be killed

Error logs on Crashlytics

These logs are what we think are related to the issue, but we're just speculating and these errors might be a symptom of a different problem. We think this line of code is to blame:

https://github.com/MaikuB/flutter_appauth/blob/09cc5e29d88b833f714ba2cfc0a5da7b707004ba/flutter_appauth/android/src/main/java/io/crossingthestreams/flutterappauth/FlutterAppauthPlugin.java#L362

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'android.content.Intent net.openid.appauth.g.d(net.openid.appauth.e)' on a null object reference

io.crossingthestreams.flutterappauth.FlutterAppauthPlugin.performAuthorization (FlutterAppauthPlugin.java:128)
io.crossingthestreams.flutterappauth.FlutterAppauthPlugin.access$400 (FlutterAppauthPlugin.java)
io.crossingthestreams.flutterappauth.FlutterAppauthPlugin$2.onFetchConfigurationCompleted (FlutterAppauthPlugin.java:23)
net.openid.appauth.AuthorizationServiceConfiguration$ConfigurationRetrievalAsyncTask.onPostExecute (AuthorizationServiceConfiguration.java:13)
net.openid.appauth.AuthorizationServiceConfiguration$ConfigurationRetrievalAsyncTask.onPostExecute (AuthorizationServiceConfiguration.java:2)
android.os.AsyncTask.finish (AsyncTask.java:771)
android.os.AsyncTask.access$900 (AsyncTask.java:199)
android.os.AsyncTask$InternalHandler.handleMessage (AsyncTask.java:788)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:257)
android.app.ActivityThread.main (ActivityThread.java:8220)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:626)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1015)
MaikuB commented 2 years ago

Have you looked at the logs for the steps you provided? They don't reproduce the crash you've been trying to investigate. The steps you shared are to do with how plugin gets detached from the engine (i.e. gets disposed) and recreates so the plugin won't get to process the response. As far as I know, this is because the Flutter engine itself gets disposed so there's nothing else that can be done here. This means others plugins will show a similar behaviour e.g. see google_sign_in.

Unless you can provide actual steps to reproduce this, it's difficult to say what is going on and would be based on speculation. It'd be best if the actual root cause can be identified and fixed rather than putting in speculative fixes that get pushed to the community. There have been similar reports on this before but no one has yet to provide info on how to reproduce this. For all I know, those reporting this issue may also be trying to call the plugin when the Flutter engine/app is being disposed.