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

Memory leak fixes - stream and filter life cycles #2070

Merged
merged 20 commits into from
Sep 15, 2017
Merged

Conversation

kumavis
Copy link
Member

@kumavis kumavis commented Sep 12, 2017

filters:

  • filters are tied to the life cycle of their provider-engine/json-rpc-engine
  • previous provider-engine never died so it kept everything
  • now we create a mini json-rpc-engine for each page that does the filters, then passed on to our main stack
  • that mini json-rpc-engine dies when the page closes

streams:

  • we now clean up streams after the page disconnects

@kumavis
Copy link
Member Author

kumavis commented Sep 12, 2017

This will likely need #2069 to be merged in before tests will pass (seems we need newer versions of chrome/firefox?)

@kumavis
Copy link
Member Author

kumavis commented Sep 12, 2017

I think the browser support issue is from eth-json-rpc-filters es7

Copy link
Contributor

@danfinlay danfinlay left a comment

Choose a reason for hiding this comment

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

This is honestly a very hard PR to review. I caught some linty issues, but nothing worth blocking over. I pulled the branch down, it continues to work. It would be cool to have some explanation of the exact leaks it fixes (I know there are filter leaks at several points in the lifecycle, but at least explaining here on the PR which spot this cleans up would be helpful).

Anyways, it works, doesn't break anything, cleans some things up, claims to fix a leaky realm of code, so I'm in favor of it, you can make changes I requested or merge as you like.

function logStreamDisconnectWarning (remoteLabel, err) {
let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}`
if (err) warningMsg += '\n' + err.stack
console.warn(warningMsg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer log.warn() for loglevel support.

connectionStream,
multiStream,
mux,
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that you've made the naming more uniform across files.

outStream
outStream,
(err) => {
if (err) console.error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer log.error() for loglevel support.

function loggerMiddleware (req, res, next, end) {
next((cb) => {
if (res.error) {
console.error('Error in RPC response:\n', res)
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer log.error(), although I wouldn't block over this, as it was already a console.error.

@kumavis
Copy link
Member Author

kumavis commented Sep 13, 2017

the global log is only available in the background, not inpage and contentscript

@kumavis
Copy link
Member Author

kumavis commented Sep 13, 2017

  • breakout originMiddleware, loggerMiddleware, createProviderMiddleware to reduce MetamaskController file size
  • disconnect filter-blockTracker handler on stream teardown

@kumavis
Copy link
Member Author

kumavis commented Sep 13, 2017

@FlySwatter updated!

@danfinlay danfinlay merged commit 693655e into master Sep 15, 2017
@danfinlay danfinlay deleted the filter-leak-fix3 branch September 15, 2017 02:40
danjm added a commit that referenced this pull request Aug 2, 2024
…y match 'Premature close' (#26336)

## **Description**

We have long seen many errors in Sentry that have the message "Premature
close". This PR addresses those that occur when the UI is closed, and
when a connected dapp is closed.

This error is thrown from within the readable-stream module when
multiple streams are passed to the same pipeline. We do not fully
understand the issue yet, but it is suspected that we are incorrectly
triggering or handling the destroy or end of a stream.

Some relevant findings:
- others have experienced similar problems:
mafintosh/pump#25
- there are two separate "premature close" errors. One pre-existed this
PR (and v12.0.0)
#24533, and one was
introduced with that PR
- the one that was introduced with that PR goes away if the
readable-stream dependency of object-multiplex is revert back to v2.3.3
- "so I think the problem is that we are explicitly calling destroy on
some of the stream's in the pipeline, before the pipeline is finished
doing whatever it needs to do when when the streams are
ended/closed/finished"
- if we use `connectionStream.pipe(mux).pipe(connectionStream);` instead
of `pipeline(connectionStream, mux, connectionStream`, the error goes
away, but that was changed almost 7 years ago in a PR titled "Memory
leak fixes - stream and filter life cycles"
#2070

- With reference to `setupMultiplex`:

- if I change that line to pipeline(connectionStream, mux, (err) => {,
the error goes away
- but if I change it to pipeline(mux, connectionStream, (err) => {, the
error does not go away


We understand enough at this point to know that these error are not
affecting user experience. For the moment, we are going to explicitly
stop logging them, as the are clogging sentry, and [we can revisit the
search for root cause in the future
](#26337)

Note that there is at least one outstanding case of a "Premature close"
error that can occur when the application is loading. This PR does not
attempt to remove that error from being logged.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26336?quickstart=1)

## **Related issues**

Fixes: #26284

## **Manual testing steps**

1. Open the UI
2. Clear the background console
3. Close the UI
4. There should be no "Premature close" error

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
danjm added a commit that referenced this pull request Aug 5, 2024
…y match 'Premature close' (#26336)

## **Description**

We have long seen many errors in Sentry that have the message "Premature
close". This PR addresses those that occur when the UI is closed, and
when a connected dapp is closed.

This error is thrown from within the readable-stream module when
multiple streams are passed to the same pipeline. We do not fully
understand the issue yet, but it is suspected that we are incorrectly
triggering or handling the destroy or end of a stream.

Some relevant findings:
- others have experienced similar problems:
mafintosh/pump#25
- there are two separate "premature close" errors. One pre-existed this
PR (and v12.0.0)
#24533, and one was
introduced with that PR
- the one that was introduced with that PR goes away if the
readable-stream dependency of object-multiplex is revert back to v2.3.3
- "so I think the problem is that we are explicitly calling destroy on
some of the stream's in the pipeline, before the pipeline is finished
doing whatever it needs to do when when the streams are
ended/closed/finished"
- if we use `connectionStream.pipe(mux).pipe(connectionStream);` instead
of `pipeline(connectionStream, mux, connectionStream`, the error goes
away, but that was changed almost 7 years ago in a PR titled "Memory
leak fixes - stream and filter life cycles"
#2070

- With reference to `setupMultiplex`:

- if I change that line to pipeline(connectionStream, mux, (err) => {,
the error goes away
- but if I change it to pipeline(mux, connectionStream, (err) => {, the
error does not go away


We understand enough at this point to know that these error are not
affecting user experience. For the moment, we are going to explicitly
stop logging them, as the are clogging sentry, and [we can revisit the
search for root cause in the future
](#26337)

Note that there is at least one outstanding case of a "Premature close"
error that can occur when the application is loading. This PR does not
attempt to remove that error from being logged.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26336?quickstart=1)

## **Related issues**

Fixes: #26284

## **Manual testing steps**

1. Open the UI
2. Clear the background console
3. Close the UI
4. There should be no "Premature close" error

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
dawnseeker8 pushed a commit that referenced this pull request Aug 12, 2024
…y match 'Premature close' (#26336)

## **Description**

We have long seen many errors in Sentry that have the message "Premature
close". This PR addresses those that occur when the UI is closed, and
when a connected dapp is closed.

This error is thrown from within the readable-stream module when
multiple streams are passed to the same pipeline. We do not fully
understand the issue yet, but it is suspected that we are incorrectly
triggering or handling the destroy or end of a stream.

Some relevant findings:
- others have experienced similar problems:
mafintosh/pump#25
- there are two separate "premature close" errors. One pre-existed this
PR (and v12.0.0)
#24533, and one was
introduced with that PR
- the one that was introduced with that PR goes away if the
readable-stream dependency of object-multiplex is revert back to v2.3.3
- "so I think the problem is that we are explicitly calling destroy on
some of the stream's in the pipeline, before the pipeline is finished
doing whatever it needs to do when when the streams are
ended/closed/finished"
- if we use `connectionStream.pipe(mux).pipe(connectionStream);` instead
of `pipeline(connectionStream, mux, connectionStream`, the error goes
away, but that was changed almost 7 years ago in a PR titled "Memory
leak fixes - stream and filter life cycles"
#2070

- With reference to `setupMultiplex`:

- if I change that line to pipeline(connectionStream, mux, (err) => {,
the error goes away
- but if I change it to pipeline(mux, connectionStream, (err) => {, the
error does not go away


We understand enough at this point to know that these error are not
affecting user experience. For the moment, we are going to explicitly
stop logging them, as the are clogging sentry, and [we can revisit the
search for root cause in the future
](#26337)

Note that there is at least one outstanding case of a "Premature close"
error that can occur when the application is loading. This PR does not
attempt to remove that error from being logged.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26336?quickstart=1)

## **Related issues**

Fixes: #26284

## **Manual testing steps**

1. Open the UI
2. Clear the background console
3. Close the UI
4. There should be no "Premature close" error

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants