Android Question ANR Crash: ANR Input dispatching timed out

aaronk

Well-Known Member
Licensed User
Longtime 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.
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: 5.)

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.
 

Attachments

  • SocketSSL-Source.zip
    17.8 KB · Views: 289
  • ANR.txt
    128.2 KB · Views: 790

aaronk

Well-Known Member
Licensed User
Longtime User
The only code that is related to the SSL socket is:

B4X:
    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:

B4X:
    Dim myMap As Map
    myMap.Initialize
    ThreadConnect.Start(Me, "connectTo_Panel",Array As Object(myMap))

in my connectTo_panel sub I have:

B4X:
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 ?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime 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?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Do you mean Initialize the AStreams_ssl using the threading library like I did when I created the connection?

Like:

From:
B4X:
If Successful Then
AStreams_ssl.Initialize(ssl_socket.InputStream ,ssl_socket.OutputStream ,"M1Connect")
End If

To:
B4X:
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?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime 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:

B4X:
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 ?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime 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 ?

https://www.b4x.com/android/forum/t...-networkonmainthread-exception.44760/#content
B4X:
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.
 

Attachments

  • ANR.txt
    128 KB · Views: 466
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do you think if I used the following code it will fix the issue ?
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.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Please try it with this library.
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.
 
Upvote 0
Top