Skip to content

Prevent attempts to persist native errors#10011

Closed
Gudahtt wants to merge 1 commit intodevelopfrom
prevent-attempts-to-persist-native-error
Closed

Prevent attempts to persist native errors#10011
Gudahtt wants to merge 1 commit intodevelopfrom
prevent-attempts-to-persist-native-error

Conversation

@Gudahtt
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt commented Dec 7, 2020

If a native Error is present in the state object we're persisting, it will fail to persist on Firefox. This is because the structured clone
implementation on Firefox doesn't support native errors. As a result, the state will fail to persist from the point the error is introduced, leading to a loss in state whenever the extension is next reset.

This has been fixed by deep cloning the state before persisting, and removing any native error objects present. If a native error is found, it is converted into a plain object. An error is sent to Sentry the first time a native error is encountered in each extension session.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could probably replace this with serialize-error or something similar, but there were a few bugs on the serialize-error GitHub repo that might affect us. This seemed effective enough for now 🤷

If a native Error is present in the state object we're persisting, it
will fail to persist on Firefox. This is because the structured clone
implementation on Firefox doesn't support native errors [1]. As a
result, the state will fail to persist from the point the error is
introduced, leading to a loss in state whenever the extension is next
reset.

This has been fixed by deep cloning the state before persisting, and
removing any native error objects present. If a native error is found,
it is converted into a plain object. An error is sent to Sentry the
first time a native error is encountered in each extension session.

[1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1556604
@Gudahtt Gudahtt force-pushed the prevent-attempts-to-persist-native-error branch from ad6541f to 5868cf9 Compare December 7, 2020 18:55
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [5868cf9]
Page Load Metrics (340 ± 34 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint297755168
domContentLoaded2695003387034
load2715023407034
domInteractive2695003387034

@Gudahtt Gudahtt marked this pull request as ready for review December 7, 2020 19:51
@Gudahtt Gudahtt requested a review from a team as a code owner December 7, 2020 19:51
@Gudahtt Gudahtt requested a review from brad-decker December 7, 2020 19:51
@Gudahtt
Copy link
Copy Markdown
Member Author

Gudahtt commented Dec 7, 2020

I reproduced this error on develop with these steps:

  1. Add this line to the end of logResponse in app/scripts/controllers/permissions/permissionsLog.js:
entry.testError = new Error("test error")
  1. Build and load the extension in Firefox (I used v83)
  2. Open the background dev console to watch for errors
  3. Connect to the test dapp
  4. Click "Request permissions"
  5. Cancel the request in the notification that pops up

After that, you should see an error in the background dev console, and it should re-appear whenever state changes (e.g. when you add a token, change networks, add a contact, etc.). After reloading the extension, any state changes you made after this error will be erased.

Note that until #10012 is merged, the resulting error message may be obfuscated though. Look at the stack trace to determine whether it's the correct one.

The same steps can be followed with any MetaMask extension version between v8.0.0 and v8.1.5 without the need to modify the source code (the error won't be obfuscated either!).

With this PR, these steps should no longer break state persistence.

@Gudahtt
Copy link
Copy Markdown
Member Author

Gudahtt commented Dec 8, 2020

So I'm not sure if this is something we should do. This new deep clone introduces a runtime cost that may be substantial for users with a large state. We could instead let this fail, and rely upon Sentry reports for failure 🤔

@Gudahtt
Copy link
Copy Markdown
Member Author

Gudahtt commented Dec 8, 2020

I'm going to close this for now. We can log the error in Sentry instead (#10018). We can revisit this idea if we see this error in Sentry and want additional context.

@Gudahtt Gudahtt closed this Dec 8, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2020
@Gudahtt Gudahtt deleted the prevent-attempts-to-persist-native-error branch December 11, 2020 03:59
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.

2 participants