Android Question ANR Crash: ANR Input dispatching timed out

Discussion in 'Android Questions' started by aaronk, Jun 28, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I am having a issue with one of my apps when trying to connect to a product that accepts SSL connections.

    I have had a number of issues with the way it connected and thought I had it fixed but keep getting ANR crash reports in my Developer Console for the app.

    Erel suggested that I used the treading Library to disconnect the SSL socket connection which I did but kept getting the error coming up in my developer console.

    I then thought I would use the thread Library to connect to the product and still keep getting the same error come up in my developer console.

    When I run this on my Nexus 5, Nexus 7, Nexus 9 I am not getting this error come up.

    The people that are reporting this issue in my developer console seem to be all Samsung devices.
    I then used my old Samsung Galaxy S2 to test it out, and it seems to run fine and don't see any errors.

    The ANR error I am getting in my developer console is also attached.
    I seem to get 2 or 3 ANR reports every few weeks, so it doesn't look everyone is getting these errors, except a few people.

    I was in a TeamViewer session (on the PC) with a user that had my app and the app crashed while I was in the session with them, and he said that he opened the app and it tried to connect and said when he tapped on the screen it come up with a message saying if he wanted to wait, or report the error.

    When he tapped on 'report' it then submitted a ANR crash which is attached.

    Based on the SSL Socket Library I am using (source code to this lib is attached) can anyone see anything wrong in this library that might be causing the error I am getting ?

    I am calling a service from the Main Activity, and in that service I am calling a class and in that class I am making the SSL Socket connection.

    I can't upload my B4A project since it's a released app and I don't want to publish the source code on the forum.
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    Based on the ANR log the main thread has blocked when you called SocketSSL.InputStream (in m1connect_connected event).
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    The only code that is related to the SSL socket is:

    Code:
    If Successful Then
            AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,
    "M1Connect")
        
    End If
    Should I be initializing this when the connection is made or somewhere else?

    When I am connecting I am running the following code:

    Code:
    Dim myMap As Map
        myMap.Initialize
        ThreadConnect.Start(Me, 
    "connectTo_Panel",Array As Object(myMap))
    in my connectTo_panel sub I have:

    Code:
    Sub connectTo_Panel(MapObj As Object)
    Try
        
    If ssl_socket.IsInitialized = False Then
            ssl_socket.Initialize(
    "M1Connect")
        
    End If
        ssl_socket.Connect(IP,port,
    20000)
    Am I doing anything wrong or is there something in the library that is causing it to lock up?
    If it is the library that is causing the issue what can I do to fix it ?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The problem is in the library itself. It freezes the main thread when you call ssl_socket.InputStream.
     
  5. aaronk

    aaronk Well-Known Member Licensed User

    If it's not a silly question, what do I need to do so it doesn't freeze the main thread ?

    Can I change something in the library so it doesn't freeze the main thread?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Hard to say without delving into the library code.

    You can use the threading library to get the input stream and then store it in a process global variable.
     
    thedesolatesoul likes this.
  7. aaronk

    aaronk Well-Known Member Licensed User

    Do you mean Initialize the AStreams_ssl using the threading library like I did when I created the connection?

    Like:

    From:
    Code:
    If Successful Then
    AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,
    "M1Connect")
    End If
    To:
    Code:
    If Successful Then
        
    Dim myMap As Map
        myMap.Initialize
        ThreadConnect.Start(Me, 
    "AStreams_ssl_init",Array As Object(myMap)
    End If

    Sub AStreams_ssl_init(MapObj As Object)
        AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,
    "M1Connect"))
    End Sub
    And declare AStreams_ssl in the 'Class_Globals' sub in the Class?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Yes. The important point is that ssl_socket.InputStream is called in a background thread.
     
  9. aaronk

    aaronk Well-Known Member Licensed User

    I was playing around with this, and made it so that the following code is called by the thread library after the connection is made:

    Code:
    AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,"M1Connect"))
    So far my device (Nexus 5) has locked up once. While it's connecting it sat there for about 5 seconds and if I tap the screen I get App is not responding with a Wait, or OK button.

    If I end the app using the IDE, then start it again it seems to work fine. Doesn't seem to happen all the time.

    Since I am calling the Class from a service module, Should I use the thread library in my service and call the Class from the thread instead, then the whole class will be on a different thread ?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Services code run in the main thread like all other code.
     
  11. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I am still getting the error 'ANR Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 7.)' even when using the new Network Library that supports SSL sockets.

    Attached is the new ANR crash.

    Any ideas on what might be causing this error based on the attached ANR?

    Would you think it's because it's taking longer than 5 seconds to create the connection and causing the app not to respond?

    Do you think if I used the following code it will fix the issue ?

    http://www.b4x.com/android/forum/threads/workaround-the-networkonmainthread-exception.44760/#content
    Code:
    Sub DisableStrictMode
       
    Dim jo As JavaObject
       jo.InitializeStatic(
    "android.os.Build.VERSION")
       
    If jo.GetField("SDK_INT") > 9 Then
         
    Dim policy As JavaObject
         policy = policy.InitializeNewInstance(
    "android.os.StrictMode.ThreadPolicy.Builder"Null)
         policy = policy.RunMethodJO(
    "permitAll"Null).RunMethodJO("build"Null)
         
    Dim sm As JavaObject
         sm.InitializeStatic(
    "android.os.StrictMode").RunMethod("setThreadPolicy"Array(policy))
       
    End If
    End Sub
    I have had people say it comes up saying 'Application is not responding' message on the screen.

    This last ANR was from a Galaxy Note4 (trltevzw) running Android 5.0. Mainly the ANRs with this error are coming from Samsung devices.
     

    Attached Files:

    • ANR.txt
      File size:
      128 KB
      Views:
      46
  12. Erel

    Erel Administrator Staff Member Licensed User

    It will not fix it.

    This is strange that you get this error as the library waits for the handshake to complete before it raises the Connected event. Anyway I've modified the code to actually get the input stream before the event. Please try it with this library.
     
  13. aaronk

    aaronk Well-Known Member Licensed User

    Thanks, I will use this updated Library and see if anyone reports any issues.

    Weird thing is I have 500+ people using my app and only seem to get a handful of people report this ANR and they all seem to be mainly Samsung devices.
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    It does seem like a bug in the OS itself.
     
    DonManfred likes this.
  15. aaronk

    aaronk Well-Known Member Licensed User

    I just replaced the Network library with the 1.51 from post 12 above, and now it doesn't connect at all :(

    If I then replace it with version 1.50 it then connects.. seems that getting the input stream before the event didn't work.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    The Connected event is not fired?
     
  17. aaronk

    aaronk Well-Known Member Licensed User

    No it's not.

    If I use so.Connect(... to make the connection, the so_connected sub is not triggered.
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Are you connecting to a public server? Can you send me the link?
     
  19. aaronk

    aaronk Well-Known Member Licensed User

    Hi Erel,

    Check your PM.. sent you the details.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Please try it with this jar file.
     

    Attached Files:

Loading...