Page 1 of 1

Repeatedly aborted async HTTPRequest() will crash

Posted: Wed Aug 21, 2019 11:11 am
by rotacak
Hi,

when I will run this code then it works few times (+- 10 loops) and then it will crash at a random line.

Code: Select all

Repeat
  HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.google.com", "", #PB_HTTP_Asynchronous)
  If HttpRequest
    Debug "started"
    AbortHTTP(HttpRequest)
    FinishHTTP(HttpRequest)
    Debug "aborted"
  EndIf
ForEver
Result:
[12:08:35] Waiting for executable to start...
[12:08:35] Executable type: Windows - x86 (32bit, Unicode)
[12:08:35] Executable started.
[12:08:35] [ERROR] Line: 2
[12:08:35] [ERROR] Invalid memory access. (read error at address 74608671)
[12:08:35] [ERROR] Line: 2
[12:08:35] [ERROR] Invalid memory access. (write error at address 74047512)
[12:08:35] The Program execution has finished.

After this crash any further execution of Purebasic code can start crashig on random lines (opening windows for example) and it needs a restart.

Happens on:
PureBasic 5.71 beta 3 LTS (Windows - x86)

Re: Repeatedly aborted async HTTPRequest() will crash

Posted: Thu Aug 22, 2019 8:45 am
by tester

Code: Select all

InitNetwork()

Repeat
   HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.google.com", "", #PB_HTTP_Asynchronous)
   If HttpRequest
      Debug "started"
      AbortHTTP(HttpRequest)
      Repeat
         Progress = HTTPProgress(HttpRequest)
         Select Progress
            Case #PB_HTTP_Aborted
               Debug "aborted"
               FinishHTTP(HttpRequest)
               Break
         EndSelect
         Delay(300) ; Don't stole the whole CPU
      ForEver
   EndIf
ForEver

Re: Repeatedly aborted async HTTPRequest() will crash

Posted: Fri Aug 23, 2019 2:11 pm
by tester

Code: Select all

InitNetwork()

Debug "#PB_HTTP_Success: "+#PB_HTTP_Success
Debug "#PB_HTTP_Failed: "   +#PB_HTTP_Failed
Debug "#PB_HTTP_Aborted: "+#PB_HTTP_Aborted    

Repeat
   HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.google.com", "", #PB_HTTP_Asynchronous)
   If HttpRequest
      Debug "started"
      AbortHTTP(HttpRequest)
      Repeat
         Progress = HTTPProgress(HttpRequest)
         Select Progress
            Case #PB_HTTP_Aborted
               Debug "aborted"
               FinishHTTP(HttpRequest)
               Break
            Default
               Debug "status: "+Progress
         EndSelect
         Delay(300) ; Don't stole the whole CPU
      ForEver
   EndIf
ForEver

Code: Select all

[16:02:40] Waiting for executable to start...
[16:02:40] Executable type: Windows - x86  (32bit, Unicode)
[16:02:40] Executable started.
[16:02:40] [Debug] #PB_HTTP_Success: -2
[16:02:40] [Debug] #PB_HTTP_Failed: -3
[16:02:40] [Debug] #PB_HTTP_Aborted: -4
[16:02:40] [Debug] started
[16:02:40] [Debug] status: -5
[16:02:40] [Debug] aborted
[16:02:40] [Debug] started
[16:02:40] [Debug] status: -5
[16:02:40] [Debug] aborted
Hello!
Does anyone know what HTTPProgress status=-5 means?

Re: Repeatedly aborted async HTTPRequest() will crash

Posted: Fri Aug 23, 2019 3:02 pm
by skywalk
No crash with PB v571 LTS x64.
I rarely use x86 except for dll's to talk with legacy x86 app's.

Re: Repeatedly aborted async HTTPRequest() will crash

Posted: Sat Aug 24, 2019 4:38 am
by BarryG
Crashes here for me with the code as posted, but only when the #PB_HTTP_Asynchronous flag is used. So it seems the code is getting its "HttpRequest" variable value overwritten too quickly. Adding a short delay (250 ms or so) seems to fix it:

Code: Select all

InitNetwork()
Repeat
  HttpRequest= HTTPRequest(#PB_HTTP_Post, "https://www.google.com", "", #PB_HTTP_Asynchronous)
  If HttpRequest
    Debug "started"
    AbortHTTP(HttpRequest)
    FinishHTTP(HttpRequest)
    Debug "aborted"
    Delay(250) ; Quarter-second delay so "HttpRequest" var isn't overwritten with the next request.
  EndIf
ForEver

Re: Repeatedly aborted async HTTPRequest() will crash

Posted: Tue Jan 21, 2020 5:59 pm
by Fred
May be the doc isn't cristal clear, but you can't code the async like that. FinishHTTP needs to be called once the '#PB_Http_Aborted' status has been reached. As a thread is spawned internally, you can't free directly the resource with FinishHTTP before the thread has been fully terminated. Code should look like that:

Code: Select all

Repeat
  Debug "---------"
  HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.google.com", "", #PB_HTTP_Asynchronous)
  If HttpRequest
    Debug "started"
    AbortHTTP(HttpRequest)
    
    Repeat
      Progress = HTTPProgress(HttpRequest)
      Select Progress
        Case #PB_HTTP_Success
          Debug "Download successed"
          FinishHTTP(HttpRequest)
          Break
          
        Case #PB_HTTP_Failed
          Debug "Download failed"
          FinishHTTP(HttpRequest)
          Break
          
        Case #PB_HTTP_Aborted
          Debug "Download aborted"
          FinishHTTP(HttpRequest)
          Break
          
        Default
          If Progress >= 0
            Debug "Current download: " + Progress
          EndIf
          
      EndSelect
      
      Delay(500) ; Don't stole the whole CPU
    ForEver
    
  EndIf
ForEver
BTW, the -5 return code shouldn't be public, it has been fixed for next release.