websocket stalls at close c#

trade_then edited February 2018 in .Net API client
creating an app; and have found a few times now.
That once Error: Lost ticker connection. happens. A further call on Close() results in blocking at line
_ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None).Wait(); in your code of Kite3.

It happens otherwise also.
Althou close is called after checking IsConnected. Thou it seems somehow this does not present the correct picture.
and public void Close(bool Abort = false) which is in WebSocket is not publicly available through Ticker class.

Since code is in open i can myself add such method. But what i want to know is that it would amount to calling Close( Abort:true ) everytime and would that have any effect at your end.
Because usual process of calling Close(). when gets blocked has no way of coming out since it does not have a timeout and i cannot risk blocking/stalling at any level i have to call Close( Abort:true ) .

Just to clarify. you might think why would someone call close after loosing connection. it is not always like that.
but before closing a ticker I Disable Reconnect and than close. because App checks if all tickers/connections are closed or not.
All tickers/endpoints are closed by disabling reconnect then closing and then nulling the variable at my end.
But it gets stuck at the line mentioned above sometimes.

  • tonystark
    @trade_then If you are getting "Error: Lost ticker connection" then it means the connection was closed due to connectivity issues. But in some situations after this, .Net WebSocket library still shows the connection state as Open. That was the reason I added the extra Abort parameter.

    You can add it to the Ticker class and use it as the last resort. But if it is a normal connection close make sure you close it without Abort because this will inform our servers that it is intentional. Otherwise, it might count that your connection is still alive and next time you might get rate limited for opening more connections.

    Also, have you tried disposing of the ticker instance only when this error happens?
  • trade_then
    Thanks a bunch for the reply

    wrote a reply and it is lost while pressing Post Comment. The website ate my post! nowhere to be found . Will be uploading my reply now in a file.

  • tonystark
    tonystark edited February 2018

    By disposing I meant unlinking all the events and setting ticker to null.

    I'm aborting the connection only inside OnReconnect. And ticker class calls OnReconnect only if the connection is closed or the existing connection is not responding or not steaming any data. In this situation aborting is fair. Because server will also know that connection is unstable and will close.

    What I mentioned in the previous reply is that if you are closing the connection on your end make sure you send the close status message (calling Close function is enough. But don't directly do abort). Otherwise, the connection might be still alive and next time you connect again the number of connections from your API key will be 2. If your code does this multiple times you will get rate limited. Because there is a limit to the number of simultaneous ticker connections you can make.

    And your solution for having the timeout is also fine.
  • trade_then

    Implemented the TryClose for now.

This discussion has been closed.