JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.02k stars 420 forks source link

Crash when using socket on 9.9.8 #1384

Closed uponatime2019 closed 2 weeks ago

uponatime2019 commented 2 weeks ago

Describe the bug Console app crash when run socket about 1 day

To Reproduce

BinanceSocketClient.SpotApi.ExchangeData.SubscribeToTradeUpdatesAsync
BinanceSocketClient.UsdFuturesApi.SubscribeToTradeUpdatesAsync
BinanceSocketClient.UsdFuturesApi.SubscribeToAllLiquidationUpdatesAsync
BinanceSocketClient.UsdFuturesApi.SubscribeToAllLiquidationUpdatesAsync

BybitSocketClient.V5SpotApi.SubscribeToTradeUpdatesAsync
BybitSocketClient.V5LinearApi.SubscribeToLiquidationUpdatesAsync

Expected behavior When it reach limit it should give exception instead of crash. I surround with try catch on every socket call

Debug logging

Unhandled exception.Unhandled exception.  System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at CryptoExchange.Net.RateLimiting.RateLimitGate.CheckGuardsAsync(IEnumerable`1 guards, ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.RateLimiting.RateLimitGate.ProcessAsync(ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ProcessAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(Subscription subscription, Boolean unsubEvenIfNotConfirmed)
   at CryptoExchange.Net.Clients.SocketApiClient.<>c__DisplayClass46_0.<<SubscribeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at CryptoExchange.Net.RateLimiting.RateLimitGate.CheckGuardsAsync(IEnumerable`1 guards, ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.RateLimiting.RateLimitGate.ProcessAsync(ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ProcessAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(Subscription subscription, Boolean unsubEvenIfNotConfirmed)
   at CryptoExchange.Net.Clients.SocketApiClient.<>c__DisplayClass46_0.<<SubscribeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Unhandled exception. Unhandled exception. Unhandled exception. System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at CryptoExchange.Net.RateLimiting.RateLimitGate.CheckGuardsAsync(IEnumerable`1 guards, ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.RateLimiting.RateLimitGate.ProcessAsync(ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ProcessAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(Subscription subscription, Boolean unsubEvenIfNotConfirmed)
   at CryptoExchange.Net.Clients.SocketApiClient.<>c__DisplayClass46_0.<<SubscribeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at CryptoExchange.Net.RateLimiting.RateLimitGate.CheckGuardsAsync(IEnumerable`1 guards, ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.RateLimiting.RateLimitGate.ProcessAsync(ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ProcessAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(Subscription subscription, Boolean unsubEvenIfNotConfirmed)
   at CryptoExchange.Net.Clients.SocketApiClient.<>c__DisplayClass46_0.<<SubscribeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at CryptoExchange.Net.RateLimiting.RateLimitGate.CheckGuardsAsync(IEnumerable`1 guards, ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.RateLimiting.RateLimitGate.ProcessAsync(ILogger logger, Int32 itemId, RateLimitItemType type, RequestDefinition definition, String host, SecureString apiKey, Int32 requestWeight, RateLimitingBehaviour rateLimitingBehaviour, CancellationToken ct)
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.SendLoopAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ProcessAsync()
   at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync()
   at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(Subscription subscription, Boolean unsubEvenIfNotConfirmed)
   at CryptoExchange.Net.Clients.SocketApiClient.<>c__DisplayClass46_0.<<SubscribeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
JKorf commented 2 weeks ago

Hi, what options are you using for the clients, specifically the RatelimitBehaviour? This seems to happen during reconnection then it seems?

uponatime2019 commented 2 weeks ago

I just call

Hi, what options are you using for the clients, specifically the RatelimitBehaviour? This seems to happen during reconnection then it seems?

I just call subscribe function, no RatelimitBehaviour, no client option. My code look like this

  while (true)
{
    try
    {
        var listCts = new List<CancellationTokenSource>();

        var symbols = await GetTopFuturesSymbols(0, 50);
        foreach (var symbol in symbols)
        {
            listCts.Add(await StartLiquidationData(symbol));
        }

        await Task.Delay(TimeSpan.FromDays(3));

        foreach (var cts in listCts)
        {
            cts.Cancel();
        }

        await BybitSocketClient.V5LinearApi.UnsubscribeAllAsync();
    }
    catch (Exception ex)
    {
    }
}