MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites
https://metamask.io
Other
11.84k stars 4.83k forks source link

Proper Subscription Cleanup #6554

Closed mdcoon closed 3 years ago

mdcoon commented 5 years ago

What is the proper way to cleanup MetaMask subscriptions for events or new blocks in the browser? While developing locally with ganache-cli, I noticed that I was seeing increasing duplication of event notifications as time went on. Sometimes resetting the MetaMask plugin would bring things back to single notifications for events. I know there is an "unsubscribe" function that web3 offers for the subscriptions but:

1) I don't know if that actually cleans up the MM subscription and 2) Unsubscribe is an asynchronous function and window.beforeunload requires a synchronous set of actions to be executed before page actually unloads

So what is the proper way to clean up subscriptions prior to unloading a page? Does MetaMask do this automagically in the background after some timeout? What's the best practice?

Gudahtt commented 3 years ago

I'm assuming here that by subscriptions you are referring to eth_subscribe. The only means we have of manually unsubscribing is eth_unsubscribe. However, we do automatically clean up any existing subscriptions when our connection to a dapp is broken. If you are still noticing duplicate events, that is a bug and I'd be very interested to hear more about it!

The best practice here would be to unsubscribe manually if you're no longer using the subscription, but let us handle unsubscribing if you're closing the page. If we fail to unsubscribe after the dapp connection is broken (i.e. after the page closes), that's on us.

If you have any further questions, I'd suggest taking a look at our Community Forum. If you experience anything that seems like a bug (e.g. duplicate events), please create a new issue and we will investigate further. Thanks!