Binance.Net icon indicating copy to clipboard operation
Binance.Net copied to clipboard

Not handled ObjectDisposedException after disposing BinanceSocketClient

Open NektoDron opened this issue 2 years ago • 4 comments

Describe the bug Not handled exception after disposing BinanceClient after upgrade to 8.0.13 from 8.0.11

To Reproduce Dispose client in reconnecting state

Expected behavior not handled exception will be handled FATAL:UnhandledException: System.ObjectDisposedException: The CancellationTokenSource has been disposed. at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseInternalAsync() at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(SocketSubscription subscription) at CryptoExchange.Net.BaseSocketClient.<>c_DisplayClass37_01.<<SubscribeAsync>b_0>d.MoveNext() — End of stack trace from previous location — at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state) at System.Threading.QueueUserWorkItemCallback.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseInternalAsync() at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(SocketSubscription subscription) at CryptoExchange.Net.BaseSocketClient.<>c_DisplayClass37_01.<<SubscribeAsync>b_0>d.MoveNext() — End of stack trace from previous location — at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state) at System.Threading.QueueUserWorkItemCallback.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

NektoDron avatar May 19 '22 07:05 NektoDron

Should be fixed in the latest version

JKorf avatar May 22 '22 15:05 JKorf

The same exception on 8.1.0 and CryptoExchange.Net 5.1.10. I see CloseInternalAsync call "_ctsSource.Cancel()" and doesn't check what it was Disposed. I can't enable the Debug mode logs because every data from Binance will be in the log and it will be to mach.

I suspect that socketConnection.CloseAsync is called from several places at the same time in my case (CheckTimeoutAsync also? ), but I don’t see any lock.

NektoDron avatar May 24 '22 07:05 NektoDron

I'm tried to call socketApi.UnsubscribeAllAsync() before call socketApi.Dispose but exception was the same. Library used disposed CancellationTokenSource .

NektoDron avatar May 27 '22 09:05 NektoDron

The problem isn't disposing socket API. 8.0.11 version is working fine without unhandled exceptions

---> call UnsubscribeAllAsync 21:41:40.65[5163]DEBUG:[BinSpotV1] API: 2022/05/27 21:41:40:660 | Information | Binance | Socket 38 Cancellation token set, closing subscription 21:41:40.65[5163]DEBUG:[BinSpotV1] Try to close all open subscription... 21:41:40.65[5163]DEBUG:[BinSpotV1] API: 2022/05/27 21:41:40:660 | Information | Binance | Closing all 1 subscriptions 21:41:40.65[8815]DEBUG:[BinSpotV1] API: 2022/05/27 21:41:40:661 | Information | Binance | Socket 38 closed

--> unhandled exception from uncontrolled pool thread 21:41:40.65[8648]FATAL:UnhandledException: System.ObjectDisposedException: The CancellationTokenSource has been disposed. at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseInternalAsync() at CryptoExchange.Net.Sockets.CryptoExchangeWebSocketClient.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync() at CryptoExchange.Net.Sockets.SocketConnection.CloseAsync(SocketSubscription subscription) at CryptoExchange.Net.BaseSocketClient.<>c__DisplayClass41_0`1.<<SubscribeAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state) at System.Threading.QueueUserWorkItemCallback.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

----> dispose API 21:41:41.66[9198]DEBUG:[BinSpotV1] Disposing socket API... 21:41:41.66[9198]DEBUG:[BinSpotV1] API: 2022/05/27 21:41:41:662 | Information | Binance | Closing all 0 subscriptions

---> but application is crashed because unhandled exception

NektoDron avatar May 30 '22 11:05 NektoDron