bitfinexcom / bitfinex-api-py

Apache License 2.0
200 stars 125 forks source link

exception in a thread after websocket reconnects and try to resubscribe #133

Closed slurpxbt closed 6 months ago

slurpxbt commented 3 years ago

Issue type

Brief description

Websocket get 1006 error from time to time...then it reconnects succesfully and try to resubscribe. When it tries to resubscribe it gets exception in a thread and it doesn't close itself

is there a way to completely shut down websocket when exception happens so my script can restart itself(when I manually restart it works fine)

tried this to stop ws but it doesn't work:

@bfx.ws.on("error")
async def stop_ws()
     await bfx.ws.stop()

treaceback:

[�[1m�[90mBfxWebsocket�[0m�[0m] [�[91mERROR�[0m] code = 1006 (connection closed abnormally [internal]), no reason [�[1m�[90mBfxWebsocket�[0m�[0m] [�[94mINFO�[0m] Waiting 5 seconds before retrying... [�[1m�[90mBfxWebsocket�[0m�[0m] [�[94mINFO�[0m] Reconnect attempt 1/5 [�[1m�[90mBfxWebsocket�[0m�[0m] [�[94mINFO�[0m] Websocket connected to wss://api-pub.bitfinex.com/ws/2 [�[1m�[90mBfxWebsocket�[0m�[0m] [�[94mINFO�[0m] {'event': 'info', 'version': 2, 'serverId': '028d5ea6-dbf1-49f2-9aed-605f72677161', 'platform': {'status': 1}} [�[1m�[90mBfxWebsocket�[0m�[0m] [�[94mINFO�[0m] Websocket opened. [�[1m�[90mBfxSubscriptionManager�[0m�[0m] [�[94mINFO�[0m] Subscribing to channel trades Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\Administrator\Desktop\Python\lib\threading.py", line 954, in _bootstrap_inner self.run() File "C:\Users\Administrator\Desktop\Python\lib\threading.py", line 892, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\generic_websocket.py", line 99, in start_loop loop.run_until_complete(self._run_socket()) File "C:\Users\Administrator\Desktop\Python\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\generic_websocket.py", line 143, in _run_socket await self.on_message(sId, message) File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\bfx_websocket.py", line 472, in on_message await self._ws_system_handler(socketId, msg) File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\bfx_websocket.py", line 207, in _ws_system_handler await self._WS_SYSTEM_HANDLERS[eType](socketId, msg) File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\bfx_websocket.py", line 268, in _system_error_handler self._emit('error', err_string) File "C:\Users\Administrator\Desktop\live code\bitfinex\bfxapi\websockets\generic_websocket.py", line 193, in _emit self.events.emit(event, *args, **kwargs) File "C:\Users\Administrator\Desktop\Python\lib\site-packages\pyee\_base.py", line 116, in emit self._emit_handle_potential_error(event, args[0] if args else None) File "C:\Users\Administrator\Desktop\Python\lib\site-packages\pyee\_base.py", line 86, in _emit_handle_potential_error raise error TypeError: exceptions must derive from BaseException

Steps to reproduce

 try:
            bfx = Client(API_KEY="",API_SECRET="")
            @bfx.ws.on('connected')
            async def start():
                await bfx.ws.subscribe('trades', 'tBTCUSD')

            trades = []

            @bfx.ws.on('new_trade')
            def new_trade(trade):

                global trades # -------------------------

                price = trade["price"]
                size = trade["amount"]

                if size > 0:
                    side = "buy"  
                else:
                    side = "sell"

                ticker = "BTCUSD"
                trades.append([ticker,side,round(price,1),size]) 

            bfx.ws.run()
            main()

 except Exception as e:
            traceback.print_exc()
Additional Notes:

I did not install it with pip since I get reccursion errors when I do with pip...I have folder bfx in files and import the folder...I doubt this would be the problem but posting here in case it might be

slurpxbt commented 3 years ago

any fixes on this ?

itsdeka commented 3 years ago

Hello @slurpxbt, I am trying to reproduce it without success. Can I ask which version of python are you using? And if you are using last version of bitfinex-api-py?

Maybe this example could be useful (https://github.com/bitfinexcom/bitfinex-api-py/pull/137/files)

slurpxbt commented 3 years ago

hey,

below is the whole file structure that I am using. I hope this can reproduce the error

https://drive.google.com/file/d/14Vd4aiNGurYaO_JLpXZ4RANS3ivLuL3x/view?usp=sharing

I am using python 3.9.2

this bug started happening more and more often in last 2 weeks,I will try to reinstall bitfinex-api-py because last one I was using is 2 months old.

V V pon., 10. maj 2021 ob 16:34 je oseba Dario Moceri < @.***> napisala:

Hello @slurpxbt https://github.com/slurpxbt, I am trying to reproduce it without success. Can I ask which version of python are you using? And if you are using last version of bitfinex-api-py?

Maybe this example could be useful ( https://github.com/bitfinexcom/bitfinex-api-py/pull/137/files)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitfinexcom/bitfinex-api-py/issues/133#issuecomment-836775640, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHHIEE5BWPHSGM4YC6UX723TM7VHJANCNFSM42XLNHPQ .

itsdeka commented 3 years ago

Thank you. I'll let it run to see what happens. We have made several fixes in the last months, I hope you'll not have this issue after you upgrade. Please also try to downgrade to python3.6 if just upgrading is not enough.

slurpxbt commented 3 years ago

ok thx will try and If I resolve issue I will report back to you

V V pon., 10. maj 2021 ob 17:34 je oseba Dario Moceri < @.***> napisala:

Thank you. I'll let it run to see what happens. We have made several fixes in the last months, I hope you'll not have this issue after you upgrade. Please also try to downgrade to python3.6 if just upgrading is not enough.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitfinexcom/bitfinex-api-py/issues/133#issuecomment-836846780, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHHIEE5SMZZL5XQ7QRG54ETTM74HNANCNFSM42XLNHPQ .

slurpxbt commented 3 years ago

hey

tried both and I still get this error...but now every 20 seconds or so

I don't know but It just disconnects me and then fails to recconect every time

V V pon., 10. maj 2021 ob 17:38 je oseba Aljaz Pangerc < @.***> napisala:

ok thx will try and If I resolve issue I will report back to you

V V pon., 10. maj 2021 ob 17:34 je oseba Dario Moceri < @.***> napisala:

Thank you. I'll let it run to see what happens. We have made several fixes in the last months, I hope you'll not have this issue after you upgrade. Please also try to downgrade to python3.6 if just upgrading is not enough.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitfinexcom/bitfinex-api-py/issues/133#issuecomment-836846780, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHHIEE5SMZZL5XQ7QRG54ETTM74HNANCNFSM42XLNHPQ .

itsdeka commented 3 years ago

Did you also try to downgrade to 3.6?

slurpxbt commented 3 years ago

yes I also downgraded to 3.6

one more thing is it possible that this is happening beacue I run multiple datastreams that I sent you...I run 3 files one for each ticker (btc, eth, ethbtc)

V V pon., 10. maj 2021 ob 17:59 je oseba Dario Moceri < @.***> napisala:

Did you also try to downgrade to 3.6?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bitfinexcom/bitfinex-api-py/issues/133#issuecomment-836881788, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHHIEE72IP5SKXVNMDBVEDTTM77GXANCNFSM42XLNHPQ .