destroying all providers without the provider variable #4164
Replies: 7 comments
-
Interesting idea! It sounds like you don't want to do this, but could you have your provider in a higher / global scope? That way you don't need to pass it around, and could set it to Maybe even call |
Beta Was this translation helpful? Give feedback.
-
@omnus maybe... but the code is already complicated.... I need then to wrap everything in another function again etc pass many custom params etc |
Beta Was this translation helpful? Give feedback.
-
my structure of the code is too complicated to set provider outside of the main logic, I have to set it million times then and make some additional logic to set the needed chain... |
Beta Was this translation helpful? Give feedback.
-
and even if I will set provider variable at the same level as the myFunc I will still wouldn't be able to destroy it in the catch block. and I need many different providers. that now are "automatically" set inside of the mainFunction, if I put it outside I have to write hundreds separate different lines of code... which is not good at all for anything |
Beta Was this translation helpful? Give feedback.
-
That’s generally a bad design, and that would require a global variable internal to ethers to store those providers, which in turn could result in a memory leak. What might make sense is what the tests do to make sure all providers are nuked: https://github.com/ethers-io/ethers.js/blob/main/src.ts/_tests/create-provider.ts#L102 A function is used to return each provider, but they are tracked in a method to make sure when cleanup is called they are all destroyed. You would just call |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks a lot! Will the provider be garbage collected after the provider.destroy()? or it is necessary to set it to null? thank you |
Beta Was this translation helpful? Give feedback.
-
In JavaScript, as long as there is a live reference to an object, it cannot be garbage collected. But a reference in a closure that can no longer be reached and is no longer used by anything in the event loop will be garbage collected. I'm moving this to discussions. :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the Feature
Hi, sometimes a TX can be "stuck" for many reasons I guess, so I use Promise.race([myFunction, timeoutPromise]) to get out of the stuck transaction. But, when the timeout promise wins the race and the code continue working, the provider that lives inside of the myFunction keeps working, but it should be destroyed if the timeoutPromise has won the promise race... So the only way I guess to solve the issue now is to make a provider variable somewhere at the top of a code, and pass it through the whole myFunction logic, but it's kindof not so convinient and easy to do in my situation... so it would be really nice to have something like ethers.provider.destroyAll() or smth, to destroy any provider if an error is thrown somewhere far away from the provider variable itself. Thank you
Code Example
Beta Was this translation helpful? Give feedback.
All reactions