timeout is not working with providers
Web3 provider timeout is getting ignored .
In my dapp I have set timeout to 5000 . web3.eth.getBlockNumber() doesn't throw even when public node takes more than 5s to respond .
Minimal example
var {Web3} = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://evm.confluxrpc.com", {timeout: 0 }));
async function getNewBlock (){
newBlock = await web3.eth.getBlockNumber();
return newBlock
}
getNewBlock().then((nb)=>{
console.log(" SHOULDN'T BE PRINTING THE BLOCK BUT IT IS: ",nb," block number")
})
Expected behavior
innerError: { code: -32000, message: 'failed after 0 retries: timeout' },
OUTPUT
SHOULDN'T BE PRINTING THE BLOCK BUT IT IS: 72889142n block number
Environment
Windows Node.js v19.0.0. web3 8.19.2
Thanks @hammersharkfish for opening this issue.
What is the version of web3 you are using? Seems there is a typo because there is no version 8.19.2.
My bad [email protected]
Thanks @hammersharkfish,
It seems this is a missed feature in version 4.x that was available in version 1.x.
Feel free to implement it if you like to.
And in the meantime, till this feature is added, you can use Promise.race with something like:
const blockNumber = await Promise.race([
getNewBlock(),
new Promise(function(resolve, reject) {
setTimeout(function() {
reject("Timeout");
}, 5000);
})
]);
Please assign the issue to me .
Hi @hammersharkfish @Muhammad-Altabba is this issue still open ?? If yes, would you allow me to give it a try??
@shubhamshd I have written the code for the timeout already. I have run into installing dependencies using wsl so can't test it . https://github.com/web3/web3.js/issues/6228 .
ok cool thanks @hammersharkfish!!!
@hammersharkfish can you share link to PR? if you have any issue, one of our team member can look that.
@jdevcs Hi . So testing was not possible that's why I didn't open a PR. Give me a few more days I will check after my new setup is ready . https://github.com/web3/web3.js/issues/6228 ^If someone could solve this , it would be very helpful.
My system is still not setup , If someone wants to go ahead and take up this https://github.com/web3/web3.js/issues/6178 they can
I noticed that HttpProvider does not have timeout option. You can find more on:
https://docs.web3js.org/guides/web3_providers_guide/#httpprovider
The options is of type HttpProviderOptions, which is an object with a single key named providerOptions and its value is an object of type RequestInit. Regarding RequestInit see microsoft's github.
However, the timeout is available for WebSocket: https://docs.web3js.org/guides/web3_providers_guide/#websocketprovider
The options object is of type ClientRequestArgs or of ClientOptions. See here for ClientRequestArgs and here for ClientOptions.
we will add timeout option for web3 http provider.
Hey, is anyone working on this? I am new to web3 so I am looking for a good first issue to work on and this one seems great. @jdevcs Have you started working on it? If not, I'd love to give this one a try
@michaelbudko sure, Thanks for your interest in web3 lib contributions.
you may use promise.race and setTimeout in http provider, and in case of timeout first resolving , throw error. Also this should be configurable if user provides timeout value.
Hi @jdevcs , I'm looking for good first issue and came across this one. Has anyone already solved it? Can I work on it? Thanks
@vallinayakichidambaram this feature is not added yet
Hey - I would love to see this issue resolved too.
In fact, I added a $10 bounty in GitGig for anyone willing to work on this issue. Once your PR with a fix is merged, you will be able to claim the bounty.
Sorry I can't contribute more, but hopefully it helps cover a couple of coffees for your efforts!
If anyone else wants to add an incentive to get this issue resolved, you can chip in here: https://app.gitgig.io/create-bounty?shortcode=web3/web3.js:6178