Fix race condition in network controller lookup() method. #5892
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug in the network controller that can be reproduced as follows:
The bug is caused by something of a race condition in the network controller. When
lookup()
is called incontrollers/network/network.js
, as happens after a new network is selected, anet_version
rpc call is made viaethQuery.sendAsync
. If a custom rpc network which does not support thenet_version
is selected, the request for it will fail. ThenetworkMiddleware
created bycreateJsonRpcClient.js
will be retried ineth-json-rpc-middleware/fetch
5 times, with a 1 second delay between each try. It will pass an error to the callback after the 5th try.ethQuery.sendAsync({ method: 'net_version' }
incontrollers/network/network.js
handles that error by setting the network state to "loading".If a network was successfully selected while those 5 retries were in process, the network state will incorrectly be set to 'loading', even though a network is loaded.
This PR corrects this by checking that the network state before the
ethQuery.sendAsync({ method: 'net_version' }
call is the same as right after the callback resolves. It does not update the network if it has changed while waiting for the callback to be called.This is a relatively simple fix for the issue. Ideally, we would have a way of cancelling a
net_version
request that is already in process, but larger architectural changes will be needed.