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.05k stars 429 forks source link

SubscribeToAllBookTickerUpdatesAsync connection lost #1145

Open vaniakurd opened 2 years ago

vaniakurd commented 2 years ago

SubscribeToAllBookTickerUpdatesAsync() drop connection every 5 minute with this error:

2022.09.09 00:22:00:909 | Trace | Binance | Socket 1 received data: {"stream":"!bookTicker","data":{"u":340768750,"s":"IMXUSDT","b":"0.81900000","B":"23549.87000000","a":"0.82000000","A":"243.82000000"}} 2022.09.09 00:22:00:911 | Trace | Binance | Socket 1 subscription 3 message processed in 2ms, (0ms user code) 2022.09.09 00:22:00:916 | Warning | Binance | Socket 1 error: Websocket error code ConnectionClosedPrematurely, details: WebSocketException - The remote party closed the WebSocket connection without completing the close handshake. at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory1 payloadBuffer, CancellationToken cancellationToken) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token) at System.Threading.Tasks.ValueTask1.GetTaskForValueTaskSource(IValueTaskSource1 t) --- End of stack trace from previous location --- at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ReceiveLoopAsync() IOException - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host​.. at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](TIOAdapter adapter) at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory1 buffer) at System.Net.Http.HttpConnection.ReadBufferedAsyncCore(Memory1 destination) at System.Net.Http.HttpConnection.RawConnectionStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory1 payloadBuffer, CancellationToken cancellationToken) SocketException - An existing connection was forcibly closed by the remote host​. at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ReceiveAsync(Socket socket, CancellationToken cancellationToken) at System.Net.Sockets.NetworkStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](TIOAdapter adapter) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory1 buffer) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.Security.SslStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.ReadBufferedAsyncCore(Memory1 destination) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.Http.HttpConnection.ReadBufferedAsyncCore(Memory1 destination) at System.Net.Http.HttpConnection.RawConnectionStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.Http.HttpConnection.RawConnectionStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory1 payloadBuffer, CancellationToken cancellationToken) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at System.Net.WebSockets.ManagedWebSocket.ReceiveAsync(ArraySegment1 buffer, CancellationToken cancellationToken) at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.ReceiveLoopAsync() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread) at System.Runtime.CompilerServices.TaskAwaiter.<>c.b12_0(Action innerContinuation, Task innerTask) at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining) at System.Threading.Tasks.Task.RunContinuations(Object continuationObject) at System.Threading.Tasks.Task1.TrySetResult(TResult result) at System.Threading.Tasks.ValueTask1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory`1 payloadBuffer, CancellationToken cancellationToken) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.TaskAwaiter.<>c.b12_0(Action innerContinuation, Task innerTask) at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining) at System.Threading.Tasks.Task.RunContinuations(Object continuationObject) at System.Threading.Tasks.Task1.TrySetResult(TResult result) at System.Net.Http.HttpConnection.RawConnectionStream.ReadAsync(Memory1 buffer, CancellationToken cancellationToken) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread) at System.Runtime.CompilerServices.TaskAwaiter.<>c.b12_0(Action innerContinuation, Task innerTask) at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining) at System.Threading.Tasks.Task.RunContinuations(Object continuationObject) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetExistingTaskResult(Task1 task, TResult result) at System.Net.Http.HttpConnection.ReadBufferedAsyncCore(Memory1 destination) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread) at System.Runtime.CompilerServices.TaskAwaiter.<>c.<OutputWaitEtwEvents>b__12_0(Action innerContinuation, Task innerTask) at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining) at System.Threading.Tasks.Task.RunContinuations(Object continuationObject) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetExistingTaskResult(Task1 task, TResult result) at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory1 buffer) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread) at System.Runtime.CompilerServices.TaskAwaiter.<>c.b120(Action innerContinuation, Task innerTask) at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining) at System.Threading.Tasks.Task.RunContinuations(Object continuationObject) at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](TIOAdapter adapter) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.InvokeContinuation(Action`1 continuation, Object state, Boolean forceAsync, Boolean requiresExecutionContextFlow) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs ) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pNativeOverlapped) --- End of stack trace from previous location ---

2022.09.09 00:22:00:922 | Debug | Binance | Socket 1 Receive loop finished 2022.09.09 00:22:00:924 | Debug | Binance | Socket 1 Send loop finished 2022.09.09 00:22:00:925 | Debug | Binance | Socket 1 processing tasks finished 2022.09.09 00:22:00:926 | Debug | Binance | Socket 1 status changed from Connected to Reconnecting 2022.09.09 00:22:00:929 | Debug | Binance | Socket 1 attempting to reconnect 2022.09.09 00:22:00:930 | Debug | Binance | Socket 1 connecting 2022.09.09 00:22:02:095 | Debug | Binance | Socket 1 connected to wss://stream.binance.com:9443/stream 2022.09.09 00:22:02:096 | Debug | Binance | Socket 1 status changed from Reconnecting to Resubscribing 2022.09.09 00:22:02:098 | Trace | Binance | Socket 1 sending data: {"method":"SUBSCRIBE","params":["!bookTicker"],"id":2} 2022.09.09 00:22:02:099 | Trace | Binance | Socket 1 Adding 54 to sent buffer 2022.09.09 00:22:02:101 | Debug | Binance | Socket 1 starting processing tasks 2022.09.09 00:22:02:103 | Trace | Binance | Socket 1 sent 54 bytes 2022.09.09 00:22:02:105 | Debug | Binance | Socket 1 Starting task checking for no data received for 00:01:00 2022.09.09 00:22:02:396 | Trace | Binance | Socket 1 received 22 bytes in single message 2022.09.09 00:22:02:398 | Trace | Binance | Socket 1 received data: {"result":null,"id":2} 2022.09.09 00:22:02:399 | Trace | Binance | Socket 1 Subscription completed 2022.09.09 00:22:02:401 | Debug | Binance | Socket 1 all subscription successfully resubscribed on reconnected socket. 2022.09.09 00:22:02:403 | Debug | Binance | Socket 1 status changed from Resubscribing to Connected

Femidko commented 2 years ago

Same issue. But I tried like SubscribeToAllBookTickerUpdatesAsync and SubscribeToBookTickerUpdatesAsync. With different options and on different computers (local PC, dedicated server). Same results. It seems that the disconnection occurs with the frequency set in TimestampRecalculationInterval. Last version of the binance.net & cryptoExchange.net. I would appreciate any help... Log and example attached. log.zip Example.txt

vaniakurd commented 2 years ago

I noticed that with these settings the socket works more stable:

socketClientOptions.LogLevel = Microsoft.Extensions.Logging.LogLevel.None; socketClientOptions.LogWriters = new () { };

vaniakurd commented 2 years ago

And i have another isseue. I write my own NoDataTimer for ticker socket. And after no data event he trying to recreate client and resubsribe:

res

But about once every three days I see it: er

Hulkstance commented 1 year ago

ConnectionClosedPrematurely means you're getting disconnected and most likely your connection is unstable.

I noticed that with these settings the socket works more stable:

socketClientOptions.LogLevel = Microsoft.Extensions.Logging.LogLevel.None; socketClientOptions.LogWriters = new () { };

The logger has nothing to do with stability.