Open Sveagruva opened 2 years ago
Interesting. I was able to reproduce this, and even tried enclosing the provider
in its own scope just in case the while loop was keep a reference to it some how, but still wasn't seeing the GC happen.
How did you stumble on this issue?
I was creating new provider for every operation on backend with user provided pk. every instance was continuing making web3 requests
I think I've figured out what is going on. During the lifecycle of a truffle
command, like migrate or
test, a
truffle-config.jsfile may be loaded several times. Each config instance may have its own
HDWalletProvider, but all set with the same accounts. Because
HDWalletProvider` tracks the current nonce of each account, if different instances of a provider is used during the life of the command they won't track nonces accurately. To work around this, a nonce tracker singleton object is created and shared among any and all HDWalletProviders. The tracker holds on to a reference to the provider, and since it can never be garbage collected, neither can the providers it references.
You can work around this workaround by setting shareNonce
to false
(new HDWalletProvider({shareNonce: false}))
. Which should work fine for you.
The workaround for nonce tracking was the wrong solution and a should be reverted. A proper fix that does not involve a global shared singleton should be implemented.
Closing for issue maintenance. Thanks @Sveagruva for bringing this up. If the shareNonce
fix suggested by David doesn't work for you feel free to re-open! Thanks.
@cliffoo this is still an issue I think Truffle should fix the root cause of.
Issue
hdwallet-provider not being garbage collected
Steps to Reproduce
create hdwallet-provider track weak reference stop engine
Environment