Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename provider streams, notify provider of stream failures #10006

Merged
merged 1 commit into from
Dec 8, 2020

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented Dec 5, 2020

This PR renames the streams we use to connect to the inpage-provider, and ensures that the provider is notified when the streams connecting the content script to the MetaMask background fails.

Currently, the provider streams have very generic names. Although it is unlikely that this would cause problems in production, we could encounter naming collisions with other extensions that use our stream packages in production. In any event, it doesn't cost us anything to rename the streams. IIRC, the last time I suggested we do this, @Gudahtt mentioned that it could be breaking for e.g. sites that bring our provider to get around the Firefox CSP issue where injection fails, but we are shipping so many breaking changes that those folks will have to upgrade anyway.

Now, for the second change, if the extension background process is restarted, all pages will have to be reloaded in order for the provider to establish its connection to the new background process. Historically, three warnings about MetaMask stream failures have appeared in the page console when this happens. In these cases, the provider should emit the disconnect event. However, this never happened in practice, because it was the content script streams that failed, and the provider was never notified of this. We improve upon this state of affairs by informing the provider that the connection to the background is lost, so that it can emit the disconnect event, and the user can reload the page.

Corresponding inpage-provider PR: MetaMask/providers#120

@rekmarks rekmarks force-pushed the delete-public-config-store branch from ee064fd to 79ba597 Compare December 5, 2020 01:42
@rekmarks rekmarks force-pushed the provider-stream-updates branch from e321e91 to a8c452d Compare December 5, 2020 01:45
@rekmarks rekmarks force-pushed the delete-public-config-store branch from 79ba597 to a26c84c Compare December 5, 2020 22:08
@rekmarks rekmarks force-pushed the provider-stream-updates branch from a8c452d to f3700ca Compare December 5, 2020 22:10
@metamaskbot
Copy link
Collaborator

Builds ready [f3700ca]
Page Load Metrics (355 ± 33 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint298151178
domContentLoaded2735153537033
load2755163556933
domInteractive2735143537033

Comment on lines +112 to +126
function notifyInpageOfStreamFailure() {
window.postMessage(
{
target: INPAGE, // the post-message-stream "target"
data: {
// this object gets passed to obj-multiplex
name: PROVIDER, // the obj-multiplex channel name
data: {
jsonrpc: '2.0',
method: 'METAMASK_STREAM_FAILURE',
},
},
},
window.location.origin,
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use postMessage directly here because, by the time we want to send this message, the streams will have been torn down because they are all pump:ed together, and pump tears them all down if even a single stream fails.

@rekmarks rekmarks marked this pull request as ready for review December 5, 2020 23:09
@rekmarks rekmarks requested a review from a team as a code owner December 5, 2020 23:09
@rekmarks rekmarks force-pushed the delete-public-config-store branch from a26c84c to 81618d7 Compare December 7, 2020 20:11
@rekmarks rekmarks force-pushed the provider-stream-updates branch from f3700ca to 57ce6bb Compare December 7, 2020 20:12
@rekmarks rekmarks requested a review from brad-decker December 7, 2020 20:15
@metamaskbot
Copy link
Collaborator

Builds ready [57ce6bb]
Page Load Metrics (364 ± 40 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint299253209
domContentLoaded2765383638239
load2775403648240
domInteractive2765383638239

Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple questions, but appears to be working and LGTM

pump(channelA, channelB, channelA, (err) =>
logStreamDisconnectWarning(
pump(channelA, channelB, channelA, (error) =>
console.debug(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Was this change made simply to clean up the log output (Not prefixing it with the connection lost message?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah; I just decided that these were cluttering the user's console with warnings for little benefit, and so downgraded them to debugs.

*/
async function start() {
await setupStreams()
await domIsReady()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we no longer need to know that the dom is ready?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we needed it in the past because we pulled metadata in this script instead of in the inpage provider. Now, we were just calling start, which first set up the streams, and then waited for the DOM to be ready to do, nothing.

@rekmarks rekmarks added the DO-NOT-MERGE Pull requests that should not be merged label Dec 7, 2020
@rekmarks rekmarks force-pushed the delete-public-config-store branch from 35b48d6 to 62e6602 Compare December 8, 2020 00:31
@rekmarks rekmarks force-pushed the provider-stream-updates branch from 57ce6bb to 5801a4e Compare December 8, 2020 00:33
@rekmarks rekmarks merged this pull request into delete-public-config-store Dec 8, 2020
@rekmarks rekmarks deleted the provider-stream-updates branch December 8, 2020 00:34
@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2020
@rekmarks rekmarks removed the DO-NOT-MERGE Pull requests that should not be merged label Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants