Repeatedly aborted async HTTPRequest() will crash

Just starting out? Need help? Post your questions and find answers here.
rotacak
User
User
Posts: 77
Joined: Tue Feb 14, 2006 2:00 pm

Repeatedly aborted async HTTPRequest() will crash

Post 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)
tester
User
User
Posts: 30
Joined: Sun Dec 28, 2014 1:12 pm

Re: Repeatedly aborted async HTTPRequest() will crash

Post 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
tester
User
User
Posts: 30
Joined: Sun Dec 28, 2014 1:12 pm

Re: Repeatedly aborted async HTTPRequest() will crash

Post 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?
Last edited by tester on Fri Aug 23, 2019 9:15 pm, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Repeatedly aborted async HTTPRequest() will crash

Post by skywalk »

No crash with PB v571 LTS x64.
I rarely use x86 except for dll's to talk with legacy x86 app's.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Repeatedly aborted async HTTPRequest() will crash

Post 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
Fred
Administrator
Administrator
Posts: 16623
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Repeatedly aborted async HTTPRequest() will crash

Post 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.
Post Reply