web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

timeout is not working with providers

Open hammersharkfish opened this issue 2 years ago • 18 comments

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

hammersharkfish avatar Jun 09 '23 07:06 hammersharkfish

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.

Muhammad-Altabba avatar Jun 11 '23 20:06 Muhammad-Altabba

My bad [email protected]

hammersharkfish avatar Jun 12 '23 00:06 hammersharkfish

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);
    })
]);

Muhammad-Altabba avatar Jun 12 '23 14:06 Muhammad-Altabba

Please assign the issue to me .

hammersharkfish avatar Jun 12 '23 19:06 hammersharkfish

Hi @hammersharkfish @Muhammad-Altabba is this issue still open ?? If yes, would you allow me to give it a try??

shubhamshd avatar Jul 04 '23 19:07 shubhamshd

@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 .

hammersharkfish avatar Jul 04 '23 23:07 hammersharkfish

ok cool thanks @hammersharkfish!!!

shubhamshd avatar Jul 05 '23 19:07 shubhamshd

@hammersharkfish can you share link to PR? if you have any issue, one of our team member can look that.

jdevcs avatar Jul 25 '23 14:07 jdevcs

@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.

hammersharkfish avatar Aug 05 '23 18:08 hammersharkfish

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

hammersharkfish avatar Aug 16 '23 20:08 hammersharkfish

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.

Muhammad-Altabba avatar Oct 05 '23 16:10 Muhammad-Altabba

we will add timeout option for web3 http provider.

jdevcs avatar Oct 06 '23 11:10 jdevcs

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 avatar Oct 07 '23 02:10 michaelbudko

@michaelbudko sure, Thanks for your interest in web3 lib contributions.

jdevcs avatar Oct 09 '23 07:10 jdevcs

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.

jdevcs avatar Oct 09 '23 07:10 jdevcs

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 avatar May 13 '24 08:05 vallinayakichidambaram

@vallinayakichidambaram this feature is not added yet

jdevcs avatar May 13 '24 10:05 jdevcs

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

WJakub avatar May 24 '24 12:05 WJakub