New topic: 

Bizarre problem with semaphores inside a TCPSocket

<http://forums.realsoftware.com/viewtopic.php?t=46549>

         Page 1 of 1
   [ 1 post ]                 Previous topic | Next topic          Author  
Message        MadOverlord          Post subject: Bizarre problem with 
semaphores inside a TCPSocketPosted: Sun Jan 13, 2013 12:54 pm                  
       
Joined: Wed Aug 30, 2006 2:03 pm
Posts: 4                Before I bug-report this, I was wondering if perhaps 
anyone can shed some light -- maybe I'm missing something.

The background: I have a little javascript webapp that needs to query a 3rd 
party API server. Because of javascript restrictions about httpRequests, I 
wrote a little web+proxy server in RealBasic that serves the javascript app and 
static files, but proxies the API calls.

This code has been running fine for years.

Anyway, this morning there were some problems with the API server, and to help 
the developer debug them, I decided it would be a good idea to serialize and 
rate limit my requests.  So I put a semaphore (not a criticalSection) inside 
the TCPSocket.

So the path is:

Serversocket gets a connection, spawns a TCPSocket (my subclass, of course). 
The TCPSocket DataAvailable fires, it determines this a proxy request, and then 
does:

    app.critProxy.signal
    app.howMany = app.howMany + 1
    
(critProxy is a semaphore created by the app, and howMany is a counter to tell 
me how deep I'm stacked -- it should always be 1 or 0).

Then I create a HTTPSocket or HTTPSecureSocket, query the API, get the data, 
then release the semaphore:

    app.howMany = app.howMany - 1
    app.critProxy.release
    
And finally I try and return the results...

So I run a single instance of the webapp, which will make 5 API calls in 
sequence -- only one in flight at once. Mostly it works, but every so often, I 
get this:

Key: First number is the TCPSocket.handle, second the tickCount, third the IP 
of the requester, type (obviously), entry or exit of the critical code section 
and the count, and then what the proxy was being asked to do (I've converted 
this into a form that's easy to read)

Proxy : 11 : 65837886 : 127.0.0.1 : GET :  IN CRITICAL(1) : API Call 1
Proxy : 11 : 65837954 : 127.0.0.1 : GET : OUT CRITICAL(1) : API Call 1
Proxy : 11 : 65837970 : 127.0.0.1 : GET :  IN CRITICAL(1) : API Call 2
Proxy : 11 : 65838041 : 127.0.0.1 : GET : OUT CRITICAL(1) : API Call 2
Proxy : 11 : 65838052 : 127.0.0.1 : GET :  IN CRITICAL(1) : API Call 3
Proxy : 11 : 65838095 : 127.0.0.1 : GET : OUT CRITICAL(1) : API Call 3
Proxy : 11 : 65838104 : 127.0.0.1 : GET :  IN CRITICAL(1) : API Call 4
Proxy : 11 : 65838115 : 127.0.0.1 : GET :  IN CRITICAL(2) : API Call 4
Proxy : 11 : 65838175 : 127.0.0.1 : GET : OUT CRITICAL(2) : API Call 4
Proxy : -1 : 65838179 :     : GET : OUT CRITICAL(1) : API Call 4
Proxy : 11 : 65838228 : 127.0.0.1 : GET :  IN CRITICAL(1) : API Call 5
Proxy : 11 : 65838298 : 127.0.0.1 : GET : OUT CRITICAL(1) : API Call 5


Now, there's more than one Sunday worth of strangeness here. Apart from the 
fact that the semaphore isn't protecting the code, each distinct API call 
should only be made once -- but for some strange reason, we are getting 
duplicate requests made to the ServerSocket (or at least, it thinks so!). And 
anytime this happens, the first TCPSocket finds that its handle is set to -1 
when it's time to release the semaphore! In this case, call 4 got duplicated, 
and it apparently got reassigned to a TCPSocket with the same handle; the 
original TCPSocket got reset somehow.

When I run multiple copies of the webapp at the same time, things get really 
funky. The in-critical count gets up to 4 or 5.

So what am I missing? How is that semaphore not working?   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 1 post ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to