binance-connector-python
binance-connector-python copied to clipboard
SpotWebsocketClient memory leak
If you stop and delete SpotWebsocketClient object - it can still be in memory without calling the destructor. One reference to object will be in twisted.internet.selectreactor.SelectReactor object
Steps to reproduce:
import ctypes
import sys
import time
import gc
import os
import certifi
from binance.websocket.spot.websocket_client import SpotWebsocketClient
#comment if don't need
os.environ['SSL_CERT_FILE'] = certifi.where()
def echo(message) -> None:
print(message)
ws_client = SpotWebsocketClient(stream_url='wss://testnet.binance.vision')
ws_client.start()
ws_client.mini_ticker(
id=0,
callback=echo
)
time.sleep(5)
ws_client.stop()
time.sleep(5)
ws_client.join()
time.sleep(5)
obj_id = id(ws_client)
del(ws_client)
gc.collect()
print(sys.getrefcount(ctypes.cast(obj_id, ctypes.py_object).value), '\n')
print(gc.get_referrers(ctypes.cast(obj_id, ctypes.py_object).value))
>>> 2
>>> [{'reactor': <twisted.internet.selectreactor.SelectReactor object at 0x108bc4a10>, 'logOctets': False, 'logFrames': False, 'trackTimings': False, '_batched_timer': <txaio._common._BatchedTimer object at 0x10a3e6f10>, 'url': 'wss://testnet.binance.vision/ws', 'isSecure': True, 'host': 'testnet.binance.vision', 'port': 443, 'resource': '/ws', 'path': '/ws', 'params': {}, 'origin': None, 'protocols': [], 'useragent': 'AutobahnPython/23.1.1', 'headers': {}, 'proxy': None, 'version': 18, 'utf8validateIncoming': True, 'acceptMaskedServerFrames': False, 'maskClientFrames': True, 'applyMask': True, 'maxFramePayloadSize': 0, 'maxMessagePayloadSize': 0, 'autoFragmentSize': 0, 'failByDrop': True, 'echoCloseCodeReason': False, 'serverConnectionDropTimeout': 1, 'openHandshakeTimeout': 5, 'closeHandshakeTimeout': 1, 'tcpNoDelay': True, 'perMessageCompressionOffers': [], 'perMessageCompressionAccept': <function WebSocketClientFactory.resetProtocolOptions.<locals>.<lambda> at 0x10aacb6a0>, 'autoPingInterval': 300, 'autoPingTimeout': 5, 'autoPingSize': 12, 'autoPingRestartOnAnyTraffic': True, 'protocol_instance': <binance.websocket.binance_client_protocol.BinanceClientProtocol object at 0x10a80a5d0>, 'base_client': <SpotWebsocketClient(Thread-1, stopped 123145484791808)>, 'payload': b'{"method": "SUBSCRIBE", "params": ["!miniTicker@arr"], "id": 0}', '_logger': <Logger binance.websocket.binance_client_factory (WARNING)>, 'protocol': <class 'binance.websocket.binance_client_protocol.BinanceClientProtocol'>, 'callback': <function echo at 0x108b96e80>, 'numPorts': 1, 'delay': 0.1, 'retries': 0, '_callID': None, 'continueTrying': 1}]
Conda env
dependencies:
- autopep8=1.6.0=pyhd3eb1b0_1
- bzip2=1.0.8=h1de35cc_0
- ca-certificates=2022.10.11=hecd8cb5_0
- certifi=2022.9.24=py311hecd8cb5_0
- libffi=3.4.2=hecd8cb5_6
- ncurses=6.3=hca72f7f_3
- openssl=1.1.1s=hca72f7f_0
- pip=22.2.2=py311hecd8cb5_0
- pycodestyle=2.8.0=pyhd3eb1b0_0
- python=3.11.0=h1fd4e5f_2
- readline=8.2=hca72f7f_0
- setuptools=65.5.0=py311hecd8cb5_0
- sqlite=3.40.1=h880c91c_0
- tk=8.6.12=h5d9f67b_0
- toml=0.10.2=pyhd3eb1b0_0
- tzdata=2022g=h04d1e81_0
- wheel=0.37.1=pyhd3eb1b0_0
- xz=5.2.10=h6c40b1e_1
- zlib=1.2.13=h4dc903c_0
- pip:
- attrs==22.2.0
- autobahn==23.1.1
- automat==22.10.0
- binance-connector==2.0.0
- cffi==1.15.1
- charset-normalizer==3.0.1
- constantly==15.1.0
- cryptography==39.0.0
- hyperlink==21.0.0
- idna==3.4
- incremental==22.10.0
- pyasn1==0.4.8
- pyasn1-modules==0.2.8
- pycparser==2.21
- pycryptodome==3.16.0
- pyopenssl==23.0.0
- requests==2.28.2
- service-identity==21.1.0
- six==1.16.0
- twisted==22.10.0
- txaio==23.1.1
- typing-extensions==4.4.0
- urllib3==1.26.14
- zope-interface==5.5.2