Skip to content

Commit c93ae0f

Browse files
Fix: add retry logic to Esplora requests to handle connection issues
- Add timeout, max retries, and error handling to wait_for_block and wait_for_tx functions - Catch and log connection errors instead of failing immediately - Use shorter sleep interval (0.5s) to match Kotlin implementation - Add helpful error messages during retries This addresses the "Connection reset by peer" errors in CI by making the Python tests more resilient to temporary network issues when connecting to the Esplora API.
1 parent 9c02f23 commit c93ae0f

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,48 @@ def mine_and_wait(esplora_endpoint, blocks):
5151
def wait_for_block(esplora_endpoint, block_hash):
5252
url = esplora_endpoint + "/block/" + block_hash + "/status"
5353
esplora_picked_up_block = False
54-
while not esplora_picked_up_block:
55-
res = requests.get(url)
54+
max_retries = 30
55+
retry_count = 0
56+
57+
while not esplora_picked_up_block and retry_count < max_retries:
5658
try:
57-
json = res.json()
58-
esplora_picked_up_block = json['in_best_chain']
59-
except:
60-
pass
61-
time.sleep(1)
59+
res = requests.get(url, timeout=10)
60+
try:
61+
json = res.json()
62+
esplora_picked_up_block = json['in_best_chain']
63+
except:
64+
pass
65+
except Exception as e:
66+
print(f"Request error: {e}, retrying ({retry_count+1}/{max_retries})...")
67+
68+
retry_count += 1
69+
time.sleep(0.5)
70+
71+
if not esplora_picked_up_block:
72+
raise TimeoutError(f"Block {block_hash} not found after {max_retries} attempts")
6273

6374
def wait_for_tx(esplora_endpoint, txid):
6475
url = esplora_endpoint + "/tx/" + txid
6576
esplora_picked_up_tx = False
66-
while not esplora_picked_up_tx:
67-
res = requests.get(url)
77+
max_retries = 30
78+
retry_count = 0
79+
80+
while not esplora_picked_up_tx and retry_count < max_retries:
6881
try:
69-
json = res.json()
70-
esplora_picked_up_tx = json['txid'] == txid
71-
except:
72-
pass
73-
time.sleep(1)
82+
res = requests.get(url, timeout=10)
83+
try:
84+
json = res.json()
85+
esplora_picked_up_tx = json['txid'] == txid
86+
except:
87+
pass
88+
except Exception as e:
89+
print(f"Request error: {e}, retrying ({retry_count+1}/{max_retries})...")
90+
91+
retry_count += 1
92+
time.sleep(0.5)
93+
94+
if not esplora_picked_up_tx:
95+
raise TimeoutError(f"Transaction {txid} not found after {max_retries} attempts")
7496

7597
def send_to_address(address, amount_sats):
7698
amount_btc = amount_sats/100000000.0

0 commit comments

Comments
 (0)