From 56a98896163157bd1bd57377a3e55a6fb153276f Mon Sep 17 00:00:00 2001 From: Karl Bartel Date: Tue, 4 Jul 2023 11:57:25 +0200 Subject: [PATCH] Check RPC after geth startup to reduce flakiness (#10406) Check RPC after geth startup to reduce flakiness This is meant to avoid the following errors that happens across most (all?) e2e tests from time to time: ``` Error: Connection refused or URL couldn't be resolved: http://localhost:8545 at XMLHttpRequest.request.onreadystatechange (/home/circleci/app/node_modules/web3-providers/dist/web3-providers.cjs.js:759:22) at XMLHttpRequestEventTarget.dispatchEvent (/home/circleci/app/node_modules/xhr2-cookies/xml-http-request-event-target.ts:44:13) at XMLHttpRequest._setReadyState (/home/circleci/app/node_modules/xhr2-cookies/xml-http-request.ts:219:8) at XMLHttpRequest._onHttpRequestError (/home/circleci/app/node_modules/xhr2-cookies/xml-http-request.ts:379:8) at ClientRequest. (/home/circleci/app/node_modules/xhr2-cookies/xml-http-request.ts:266:37) at Socket.socketErrorListener (_http_client.js:406:9) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) ``` I am not sure this actually works as intended, but I could not reproduce the problem after this change, anymore. So it is worth testing it out, as we can easily revert the change or improve the approach. --- packages/celotool/src/lib/geth.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/celotool/src/lib/geth.ts b/packages/celotool/src/lib/geth.ts index c572e5efbfe..c6e4ca80866 100644 --- a/packages/celotool/src/lib/geth.ts +++ b/packages/celotool/src/lib/geth.ts @@ -1274,8 +1274,26 @@ export async function startGeth( } } - // Geth startup isn't fully done even when the port is open, so give it another second - await sleep(1000) + // Geth startup isn't fully done even when the port is open, so check until it responds + const maxTries = 5 + let tries = 0 + while (tries < maxTries) { + tries++ + let block = null + try { + block = await new Web3('http://localhost:8545').eth.getBlock('latest') + } catch (e) { + console.log(`Failed to fetch test block: ${e}`) + } + if (block) { + break + } + console.log('Could not fetch test block. Wait one second, then retry.') + await sleep(1000) + } + if (tries === maxTries) { + throw new Error(`Geth did not start within ${tries} seconds`) + } console.log( `${instance.name}: running.`,