ReceiveHTTPFile in threads? Possible bug?

Mac OSX specific forum
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

ReceiveHTTPFile in threads? Possible bug?

Post 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
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post 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.
-= endo (registered user of purebasic since 98) =-
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post by jesperbrannmark »

Yep. Try it yourself. Threadsafe makes no difference.
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post 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.
-= endo (registered user of purebasic since 98) =-
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post 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?)
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post 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.
-= endo (registered user of purebasic since 98) =-
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post 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.
Regards,
JamiroKwai
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: ReceiveHTTPFile in threads? Possible bug?

Post by jesperbrannmark »

Ok, then its time for a bug report. Thanks for confirming.
Post Reply