From 632e6f1504c69ec84b737ec5695dd48096ec2f49 Mon Sep 17 00:00:00 2001 From: spencer-tb Date: Tue, 23 Sep 2025 18:31:13 +0100 Subject: [PATCH] chore(rpc): add retries for connection refused from post requests. --- src/ethereum_test_rpc/rpc.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ethereum_test_rpc/rpc.py b/src/ethereum_test_rpc/rpc.py index 1f289b628ff..2792b2ac42e 100644 --- a/src/ethereum_test_rpc/rpc.py +++ b/src/ethereum_test_rpc/rpc.py @@ -108,17 +108,24 @@ def post_request( } headers = base_header | extra_headers - logger.debug(f"Sending RPC request, timeout is set to {timeout}...") - response = requests.post(self.url, json=payload, headers=headers, timeout=timeout) - response.raise_for_status() - response_json = response.json() - - if "error" in response_json: - raise JSONRPCError(**response_json["error"]) - - assert "result" in response_json, "RPC response didn't contain a result field" - result = response_json["result"] - return result + for attempt in range(6): + try: + logger.debug(f"Sending RPC request, timeout is set to {timeout}...") + response = requests.post(self.url, json=payload, headers=headers, timeout=timeout) + response.raise_for_status() + response_json = response.json() + if "error" in response_json: + raise JSONRPCError(**response_json["error"]) + + assert "result" in response_json, "RPC response didn't contain a result field" + result = response_json["result"] + return result + except requests.ConnectionError as e: + if ("Connection refused" in str(e) or "Errno 111" in str(e)) and attempt < 4: + if attempt > 0: + time.sleep(1.0) + continue + raise class EthRPC(BaseRPC):