Fix race condition with user-provided web3 #17
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
There's currently a race condition where sometimes (for me ~50% or more) the user-provided web3 is loaded on
window, after it has been checked for in the app. Which results in the app just using its own and pretending the user doesn't have a wallet.I was experimenting with that in an isolated app (not kredits-web) but where I could reproduce the error.
I could fix it there with something like:
so onload we try to get the accounts, if we get an empty array as accounts it means the accounts/metamask is locked.
Cool. However, I don't quite see how that helps with the race condition, as this issue is about web3 not being available on
windowwhen trying to set the Metamask-provided one in the service. The code you wrote is just using web3 from window later on, so that's the Metamask one after it has loaded.But there's a different issue, which you did solve: when web3 is provided, but we get that empty array, we need to tell the user to unlock their account before taking any actions like e.g. voting. We should implement that as an enhancement in addition to solving the race condition.
One option for solving the race condition would be to actually just use it from window, of course, but then we still have the issue that our own code needs to reproducibly put it there first afaics.
yeah kind of.
but I thought about initializing ember or the service later. as it seems we only can be sure that everything is working after some call like
getAccounts.for example I also had the issue of not getting the list of accounts when I am directly accessing web3.eth.accounts and not using the async method - it seems that was still too early.
We only need it to be unlocked when you want to do sth. At that point we can check and then tell the user to unlock it. No?Ah, we also need it to set the current user. Although that could theoretically be done later.
@galfert is
App.deferReadinessor whatever that was called still a thing?I was trying to have a look at this. But at the moment I just get an endless loading screen. I don't get any explicit errors in the console, just warnings about IPFS (i.e. "proposal from blockchain is missing IPFS hash").
Any ideas what's the problem here?
FYI: I had my Metamask unlocked and connected to the kosmos parity.
Those are only warnings, not errors. It's just the proposals that didn't have an IPFS hash stored (before I wrote all the IPFS stuff recently), as the message explains.
I also had an endless loading screen on Friday (see #kosmos-dev chan), when for some reason I couldn't connect to
ipfs.kosmos.orgvia my VPN, while the normal connection worked just fine. I couldn't find out wth that was, as it should just be HTTPS.Yes, I thought as much. So it's probably one of the many Promises on the index route that doesn't resolve properly.
It can only be a timeout somewhere, otherwise there would be an error or rejection. Are you certain that your connections to ipfs.kosmos.org are working fine, i.e. can you see the correct responses in the network panel?
@galfert that is strange, haven't seen that yet without an exception. do you have the console log level set to "verbose" ?
also are you using metamask? what happens if you try to load it without metamask?
@skddc I did the account loading up there because I needed one async call. After that web3 was reliably available, before that it sometimes still failed for some reason. - very strange and I can not explain it.
That's just luck then. There's no way to call a method on something that is undefined.
Ok, it's the requests to my local IPFS node that just keep in pending state. If I use the Kosmos IPFS it's working.
I am not calling something that is undefined. I just could confirm that once the callback of the first asyc call resolves web3 is fully loaded and usable.
maybe related:
https://github.com/MetaMask/metamask-plugin/issues/1575
https://github.com/MetaMask/metamask-plugin/issues/1538
Ok, the advance and defer readiness magic seems to do the trick. Now I always see
in the logs.
I'm creating a PR for this.
Yay!
@bumi Could you create a seperate issue for the accounts loading problem? I'll close this one from the PR that fixed the original race condition this one was for.
you mean for the locked accounts?
up there the
getAccountscall was only to try to "wait" for the loading.