polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
240 stars 116 forks source link

compose_call() raises brokenPipe error sometimes! #87

Closed haniyeh-habibi closed 3 years ago

haniyeh-habibi commented 3 years ago

File "/home/haniyeh/uinew/cold-ui/blockchain/polkadot/tdot.py", line 82, in create_raw_transactions call = substrate.compose_call( File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/substrateinterface/base.py", line 1334, in compose_call self.init_runtime(block_hash=block_hash) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/substrateinterface/base.py", line 1039, in init_runtime block_header = self.get_block_header(block_hash=self.block_hash) or {} File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/substrateinterface/base.py", line 719, in get_block_header response = self.rpc_request("chain_getHeader", [block_hash]) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/substrateinterface/base.py", line 495, in rpc_request self.websocket.send(json.dumps(payload)) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/websocket/_core.py", line 253, in send return self.send_frame(frame) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/websocket/_core.py", line 279, in send_frame l = self._send(data) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/websocket/_core.py", line 449, in _send return send(self.sock, data) File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/websocket/_socket.py", line 157, in send return _send() File "/home/haniyeh/newColdUi/cold-ui/venv/lib/python3.8/site-packages/websocket/_socket.py", line 139, in _send return sock.send(data) File "/usr/lib/python3.8/ssl.py", line 1173, in send return self._sslobj.write(data) BrokenPipeError: [Errno 32] Broken pipe

arjanz commented 3 years ago

I had this exception also from time to time when I was using the public endpoints, apparently there are policies to disconnect idle connections.

There is no way to prevent this, but what I recommend is catching the exception and reconnect, something like:

substrate = SubstrateInterface(
    url=SUBSTRATE_RPC_URL
)

try:
    # app logic

except (WebSocketConnectionClosedException, ConnectionRefusedError,
        WebSocketBadStatusException, BrokenPipeError, SubstrateRequestException) as e:

    # restablish connection
    substrate.connect_websocket()
haniyeh-habibi commented 3 years ago

Thanku