LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
677 stars 166 forks source link

Exception ConnectionClosed - error_msg: no close frame received or sent #329

Closed skmanish closed 1 year ago

skmanish commented 1 year ago

Version of this library.

unicorn-binance-websocket-api - version 1.46.0 websockets 10.4

Solution to Issue cannot be found in the documentation or other Issues and also occurs in the latest version of this library.

Hardware?

Local server/workstation

Operating System?

macOS

Python version?

Python3.7

Installed packages

No response

Logging output

No response

Processing method?

process_stream_data

Used endpoint?

binance.com

Issue

Here is the small code that I am using to get BTC/USDT and ETH/USDT prices - I want to run this script over days without crashing:

ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream(['ticker'], ['BTCUSDT', 'ETHUSDT'], process_stream_data=process_new_receives)

I am getting the following error

BinanceWebSocketApiSocket.start_socket(90aff70187a0-ef15-a604-85c8-4e84b2cb, ['ticker'], ['btcusdt', 'ethusdt']) - Exception ConnectionClosed - error_msg: no close frame received or sent
BinanceWebSocketApiManager.stream_is_crashing(90aff70187a0-ef15-a604-85c8-4e84b2cb)
BinanceWebSocketApiManager._create_stream_thread() stream_id=90aff70187a0-ef15-a604-85c8-4e84b2cb  - RuntimeError `error: 11` - error_msg:  cannot schedule new futures after interpreter shutdown - Info: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/issues/299

I understand that Binance websocket streams might not work after 24 hours, but the websocket is terminating after random periods - could be 40 minutes, 2 hours, 20 hours etc

oliver-zehentleitner commented 1 year ago

I am pretty sure that this is not a bug.

This is not an educated guess, but since you are using macOS, I assume this is not a server in a data center. Binance usually sends an ECONNRESET. I suspect your internet connection, power saving mode or something like that is the reason. Try using a small server in a data center.

You may be interested in this discussion: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/discussions/255

You could try a CX21 VM with Debian or another lean Linux: https://www.hetzner.com/cloud

I signed up for Hetzner's referral program so I can offer 20 EUR startup credit and also get a small benefit if the customer stays. I am a customer there myself and also some of my customers have servers there. We are very satisfied.

If the error remains, I need please a logfile and a whole script, I don't know your process_new_receives() function, also here the error could be caused (not if only a print() is done).

skmanish commented 1 year ago

Thanks for your very elaborate reply Oliver - and awesome job maintaining this script!!

I already have a google compute engine instance, so I will run the script there and hope it works out fine - will let you know if this fixes

oliver-zehentleitner commented 1 year ago

With pleasure! I would be happy to receive feedback!

oliver-zehentleitner commented 1 year ago

Just an other question... the stream did restart after crashing oder did it break the execution?

skmanish commented 1 year ago

Stream did not restart itself (atleast not until several hours)

oliver-zehentleitner commented 1 year ago

Ok, then you should also look into the mentioned link from the error log: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/issues/299

And try this code structure: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/issues/299#issuecomment-1482809641

oliver-zehentleitner commented 1 year ago

So here are 2 issues...

  1. Avoid the error
  2. Care that it can restart after the crash
oliver-zehentleitner commented 1 year ago

I think its all answered, if you still have problems, please reopen or post an update here. thx.