B4J Question OkHttp: Thread gets stuck?

Heuristx

Active Member
Licensed User
Longtime User
This is in no way some urgent help request, I am just wondering.
I wanted to check if the internet connection is alive, with Erel's simple async download example. I downloaded Https://www.Google.com/index.html.
The download succeeded smoothly. But when I closed my application, some background thread must have been still stuck somewhere because after the main form closed, the application was still running, I had to click the "stop" button in B4X. This happened every time.
I closed my B4X project, created a new blank one, copied and pasted Erel's example code, changed the URL again to the Google like above, and even the new application could not be closed.

However, when I change the URL to Http://... instead of Https://... then there is no problem.

Since I just want to test the web connection, this is fine with me, I was just wondering. I would expect that an HttpJob would fail or succeed, but not that it gets stuck forever.
I tested it with some web sites, both the Http and the Https connections, but they worked, so far only Google causes the problem, consistently.
I'm just curious if anyone has any thought on this.
 

TILogistic

Expert
Licensed User
Longtime User
test:
B4X:
Public Sub TestPing
    Dim URL As String
    URL = "Https://www.Google.com/index.html"
    Wait For (GetPing(URL, 3000)) Complete (Result As Boolean)
    Log(Result)
End Sub

Public Sub GetPing(URL As String, TimeOut As Int) As ResumableSub
    Dim ResultURL As Boolean = False
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        j.GetRequest.Timeout = TimeOut
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = True
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return ResultURL
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The download succeeded smoothly. But when I closed my application, some background thread must have been still stuck somewhere because after the main form closed, the application was still running, I had to click the "stop" button in B4X. This happened every time.
This has nothing to do with the http request.
Applications are never "closed" in Android. They move to the background and at some point the OS kills the process. You don't need to worry about it.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
test:
B4X:
Public Sub TestPing
    Dim URL As String
    URL = "Https://www.Google.com/index.html"
    Wait For (GetPing(URL, 3000)) Complete (Result As Boolean)
    Log(Result)
End Sub

Public Sub GetPing(URL As String, TimeOut As Int) As ResumableSub
    Dim ResultURL As Boolean = False
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        j.GetRequest.Timeout = TimeOut
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = True
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return ResultURL
End Sub

Setting a timeout makes no difference.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
Sure, I am OK with the program because it no longer downloads from Https://, I was just wondering why this can happen. Also, it leaks memory, Windows has just warned me that I ran out of memory. Can that be cured with ExitApplication? I guess OkHttp creates a rogue thread that may remain there(?) If I wrote a program where the user chooses what to download, I may wreck his computer's memory. That makes OkHttp dangerous to use. The funny thing is, the download succeeds, the HttpJob returns with Success. So the problem is hidden from the application.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
This has nothing to do with jOkHttpUtils2, but I guess that is obvious for those who are into the networking stuff more than I. jOkHttpUtils2 is very simple and straightforward. But the same stuck thread happens when If I use the OkHttp library(version 1.31) directly.

There is some discussion about OkHttp memory leaks and closing requests, on StackOverflow:


I wonder if some response.close(); or reponseBody.close(); statement is missing somewhere in the library. Or a Try/Catch? As you can see, I am not an expert in this field, just guessing.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
If I start a new B4XPages project and write this(and nothing else) in it, the Https button will cause the program not to exit, it will close the form, but will be still in the Windows TaskManager(when I run it as a packaged standalone executable). Clicking just the Http button will make it close down correctly.

You are right, of course, Erel, the ExitApplication will cause the program to shut down.
As to memory leaks, the packaged standalone executable does not seem to leak, the memory will go up and down according to garbage collection. So I guess I was wrong, it is not risky to use. The IDE will eventually cause an out of memory warning from Windows.
So the issue is not critical, I was just wondering what will prevent a program from closing down if not a stuck background thread? And why with just this one URL? Strange.


B4X:
Private Sub btnHttps_Click
    Download("Https://www.google.com/index.html")
End Sub

Private Sub btnHttp_Click
    Download("Http://www.google.com/index.html")
End Sub

Private Sub Download(AURL As String)As ResumableSub
    Dim Result As Boolean = False
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(AURL)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = True
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Upvote 0
Top