Hello all,
I experienced a situation when TWSocket's handlers confuse application workflow.
I have SessionConnected handler async-ly reporting an error and SessionClosed 
handler async-ly reconnecting/pausing/reporting an error - depends of several 
Everything worked fine for months, but today the server I was connecting to 
went down.
Here's the log (Pos/SetState are my methods):

2011-10-12      10:37:39.311    7FD10330 Socket handle created 3572
2011-10-12      10:37:39.311    SocketChangeState wsClosed -> wsOpened
2011-10-12      10:37:39.311    TWSocket will connect to ****
2011-10-12      10:37:39.311    SocketChangeState wsOpened -> wsConnecting
2011-10-12      10:37:40.326    SocketChangeState wsConnecting -> wsConnected
2011-10-12      10:37:40.326    SocketSessionConnected, Connection refused 
(#10061) // OK. Error on connect. My code commands to report an error and 
2011-10-12      10:37:40.326    PostState: csConnecting -> csError
2011-10-12      10:37:40.326    7FD10330 TCustomWSocket.Shutdown 1 3572
2011-10-12      10:37:40.326    SocketChangeState wsConnected -> wsClosed // ! 
Oops. The socket was "kinda connected" and then closed
2011-10-12      10:37:40.326    SocketSessionClosed // ! Looks like just an 
accident closing after successful connect, closing WITHOUT ANY ERROR. So my 
code commands to fall asleep
2011-10-12      10:37:40.326    PostState: csConnecting -> csSleeping
2011-10-12      10:37:40.326    SetState: csConnecting -> csError // OK. Here 
the state commanded in SocketSessionConnected is finally set
2011-10-12      10:37:40.326    PostState: csError -> csReconnect // OK. Here 
we command to retry after an error
2011-10-12      10:37:40.326    SetState: csError -> csSleeping // Oops! 
Command from SocketSessionClosed has arrived
2011-10-12      10:37:40.326    SetState: csSleeping -> csReconnect // Damn, 
the previous reconnect command has arrived

Frankly speaking, there was no serious trouble except for strange high CPU load 
which I still can't track. But I think the very principle of calling the 
methods here enables possible troubles. I see two major danger sources here:
1) Despite of error on connect, socket state changes to wsConnected anyway.
2) Close is called without any error code so it looks like an accidental line 

What have I do to handle the descibed situation properly?
* I can't use only OnSessionConnected because then I won't catch disconnects 
after successful connect
* I can't use only OnSessionClosed because then I won't realise that an error 
occured on connect
* I can't use both of them because they're messing up.

So for now I have to invent some flag to tell the OnSessionClosed that 
OnSessionConnected has already handled the situation. 
Maybe you have more clever alternative?

To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to