Page 1 of 1

ReceiveHTTPFile in threads? Possible bug?

Posted: Fri Jul 13, 2012 11:26 am
by jesperbrannmark
Hi.
I am trying to run network stuff in threads. I dont know if there is a problem here but I am suspecting there is so.
Maybe someone could guide me along the way.
The code does the following;
set up a number of thread that download files. Right now files doesnt exist, but the result is the same.
I have put ; infront of setgadgettext, because threads and gadgets can be a bit picky. The result is the same.
In 70% of cases this works.
In 30% i get freeze of program OR strange different errors like:
[ERROR] Program aborted. (by external library)
[ERROR] Invalid memory access.

Can anyone guide me? Can anyone confirm the behavour? Should I *not* use ReceiveHTTPFile in threads? Is this unsafe?

Code: Select all

InitNetwork()
Procedure thread(i.i)
  Repeat
    ;SetGadgetText(i,"Thread "+Str(i)+": ON")
    ReceiveHTTPFile("http://google.com/"+Str(Random(29600)),GetTemporaryDirectory()+Str(Random(29600))+".x")
    ;SetGadgetText(i,"Thread "+Str(i)+": OFF")
    Delay(1)  
  ForEver
EndProcedure
OpenWindow(0,0,0,640,480,"RECEIVEHTTPFILE THREAD",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
TextGadget(1,0,0,100,24,"Thread 1: OFF")
TextGadget(2,0,30,100,24,"Thread 2: OFF")
TextGadget(3,0,60,100,24,"Thread 3: OFF")
TextGadget(4,0,90,100,24,"Thread 4: OFF")
CreateThread(@thread(), 1)
CreateThread(@thread(), 2)
CreateThread(@thread(), 3)
CreateThread(@thread(), 4)
Repeat
  a=WindowEvent()
Until a=#PB_Event_CloseWindow

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Fri Jul 27, 2012 4:13 pm
by endo
Are you compiling your code in thread safe mode? If not try it first, it's on compiler options window.
Then try using network library commands instead of Http library, so you can be sure about ReadHttp is thread safe or not.

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Fri Jul 27, 2012 8:55 pm
by jesperbrannmark
Yep. Try it yourself. Threadsafe makes no difference.

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Sat Jul 28, 2012 1:30 pm
by endo
I think ReadHttp function acts according to the return value of the http request.
So you should give a valid url that returns http/200 OK value.
Here is my version, which runs well:

Code: Select all

Threaded url.s, path.s, r.l

InitNetwork()
OpenConsole()

Procedure thread(i.i)
  url = "http://moldibi.com/"
  path = "c:\test" + Str(i)+".x"
  PrintN("Thread " + Str(i) + " started")
  r = ReceiveHTTPFile(url,path)
  PrintN("Thread " + Str(i) + " finished, return code:" + Str(r))
EndProcedure

OpenWindow(0,0,0,640,480,"RECEIVEHTTPFILE THREAD",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateThread(@thread(), 1)
CreateThread(@thread(), 2)
CreateThread(@thread(), 3)
CreateThread(@thread(), 4)
Repeat
  a=WindowEvent()
Until a=#PB_Event_CloseWindow
CloseConsole()
There is no need Forever loop in thread as ReadHttp is a blocking function, so it blocks the thread, when it finished, thread is also ends. See the console output when run in editor.

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Sat Jul 28, 2012 6:41 pm
by jesperbrannmark
I do get a crash on this.
Also, openconsole/printn is not compatible with mac so i cant really see what is going on.
Anyone else? Does anyone get this to work (except me who get a crash on mac os?)

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Sat Jul 28, 2012 11:45 pm
by endo
jesperbrannmark wrote:I do get a crash on this.
Also, openconsole/printn is not compatible with mac so i cant really see what is going on.
Anyone else? Does anyone get this to work (except me who get a crash on mac os?)
I didn't try on mac os, so may be it's a mac related bug may be. It works well on Windows.

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Sun Jul 29, 2012 7:13 am
by jamirokwai
Hi there,

I confirm (on MacBook Air, Mountain Lion, XCode 4.4, PB 4.61) ReceiveHTTPFile will die latest after 2 calls when inside of a thread.
If you do not call using Threads, all works as expected.

Re: ReceiveHTTPFile in threads? Possible bug?

Posted: Sun Jul 29, 2012 6:37 pm
by jesperbrannmark
Ok, then its time for a bug report. Thanks for confirming.