[5.73] AbortHTTP() doesn't wait for actual abort

Just starting out? Need help? Post your questions and find answers here.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

[5.73] AbortHTTP() doesn't wait for actual abort

Post by deseven »

Code: Select all

InitNetwork()

request = HTTPRequest(#PB_HTTP_Post,"https://d7.wtf","var1=value1&" +
                                                     "var2=value2&" +
                                                     "var3=value3&" +
                                                     "var4=value4&" +
                                                     "var5=value5&" +
                                                     "var6=value6&" +
                                                     "var7=value7&" +
                                                     "var8=value8&" +
                                                     "var9=value9&" +
                                                     "var10=value10",#PB_HTTP_Asynchronous)

AbortHTTP(request)

If HTTPProgress(request) = #PB_HTTP_Aborted
  Debug "aborted successfully"
Else
  Debug "not aborted"
EndIf

Delay(1000)

If HTTPProgress(request) = #PB_HTTP_Aborted
  Debug "aborted successfully after delay"
Else
  Debug "not aborted after delay"
EndIf

FinishHTTP(request)
Output i get:

Code: Select all

not aborted
aborted successfully after delay
AbortHTTP() should either wait for abort to happen or there should be something like WaitHTTP() to wait for it to happen. Otherwise this command is pretty useless in async mode.
Last edited by deseven on Fri May 14, 2021 11:28 am, edited 1 time in total.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: AbortHTTP() doesn't wait for actual abort

Post by deseven »

I use this for now, not sure if it's the correct approach, but at least it waits for any real result.

Code: Select all

Procedure AbortHTTPSafe(request.i)
  If request
    AbortHTTP(request)
    While HTTPProgress(request) >= 0
      Delay(10)
    Wend
  EndIf
EndProcedure
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [5.73] AbortHTTP() doesn't wait for actual abort

Post by helpy »

AbortHTTP() doesn't wait for actual abort
That is the way how asynchronous requests work.

The started request is handled inside an internally started thread!
Therfore AbortHTTP() just passes the abort command to the thread.
It takes time until the abort command is received and handled by the thread ...

Therfore you need to wait until HTTPProgress will return an appropriate state.

Further hint:
Because of the asynchronous handling it can also happen, that the download finished before the abort command is received by the internal thread! Therefore it can happen that HTTPProgress can return #PB_HTTP_Success although you called AborthTTP() before!
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: [5.73] AbortHTTP() doesn't wait for actual abort

Post by deseven »

That is the way how asynchronous requests work.
I base my findings on what the documentations says and for AbortHTTP() it says that it "aborts the progress of the specified asynchronous download" and also that "the value #PB_HTTP_Aborted will be returned by HTTPProgress()". None of it actually happens right after the AbortHTTP() call.

So i would say that this is either a bug in the implementation or an error in the documentation.

It indeed does make sense that it shouldn't wait for an actual abort, but then it will also make sense to introduce something like WaitHTTP() to be able to actually wait for a result. PB already have something similar in the Process library (RunProgram() and WaitProgram()).
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: [5.73] AbortHTTP() doesn't wait for actual abort

Post by helpy »

Yes!
I also think, that the documentation of HTTP library is not complete.
;-) I drew some of my conclusions from information I read between the lines. ;-)

And now a short test how long it takes until HTTPProgress() will return #PB_HTTP_Aborted after an AbortHTTP():

Code: Select all

EnableExplicit

Define request, progress, bFinished, startAbort, timeToAbort

InitNetwork()

request = HTTPRequest(#PB_HTTP_Post,"https://d7.wtf","",#PB_HTTP_Asynchronous)
If request
	bFinished = #False
	AbortHTTP(request)
	startAbort = ElapsedMilliseconds()
	
	Repeat
		progress = HTTPProgress(request)
		Select progress
			Case #PB_HTTP_Success
				Debug "success"
				bFinished = #True
				
			Case #PB_HTTP_Aborted
				timeToAbort = ElapsedMilliseconds() - startAbort
				Debug Str(timeToAbort) + " ms until request was aborted."
				bFinished = #True
				
			Case #PB_HTTP_Failed
				Debug "failed"
				bFinished = #True
				
			Default
				Delay(0)
		EndSelect		
	Until bFinished
	
	FinishHTTP(request)
EndIf
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: [5.73] AbortHTTP() doesn't wait for actual abort

Post by deseven »

It's even worse that that, since it's tied to an actual network operation (which can be completely unreliable for obvious reasons).
Try with this:

Code: Select all

request = HTTPRequest(#PB_HTTP_Get,"http://httpstat.us/200?sleep=10000","",#PB_HTTP_Asynchronous)
You can add another 0 to the request and it will wait for 100 seconds. That makes AbortHTTP() almost completely useless, whether it waits for the result or not.
I'm not sure if you can call it a bug either, let's just say that HTTP library needs a lot more love. For example, the underlying libcurl has a CURLOPT_TIMEOUT option which can be used in such cases.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: [5.73] AbortHTTP() doesn't wait for actual abort

Post by deseven »

In order to find a workaround for this (and also to create a reliable solution for a bunch of other things that bothered me) i wrote this module:
https://github.com/deseven/pb-httprequest-manager

It will handle a lot of requests and correctly finish them all.
Post Reply