-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cc56d0d
inpage - use json-rpc-engine for inpage-provider
kumavis 440a42b
inpage - add idRemapMiddleware
kumavis 57e4805
streams - use pump and published obj-multiplex
kumavis 0e8e655
inpage - distinguish pump vs pipe
kumavis 9d4c02e
metamask - add jsonrpc filter middleware on per-connection engine
kumavis f5d0a0b
deps - bump jsonrpc filters for log filter formate fix
kumavis 7040162
lint - remove dead code
kumavis 671dafe
Merge branch 'master' of github.com:MetaMask/metamask-extension into …
kumavis ef3bf81
inpage - use obj-multiplex module
kumavis 41164f6
Merge branch 'master' of github.com:MetaMask/metamask-extension into …
kumavis 8545453
contentscript - fix obj-multiplex instantiation and use pump for streams
kumavis 114dae5
Merge branch 'master' of github.com:MetaMask/metamask-extension into …
kumavis a265144
metamask cont - standardize multiplex stream naming
kumavis 96d1175
debug - prefer logger over console
kumavis 245c0f0
metamask controller - move middleware into seperate files
kumavis 765ef64
metamask controller - destroy filter polyfill on disconnect
kumavis 6ba448e
changelog - add note of filter memory leak fix
kumavis c634ca3
Merge branch 'master' into filter-leak-fix3
kumavis d7097db
createOriginMiddleware - fix var name
kumavis 0687c82
Merge branch 'filter-leak-fix3' of github.com:MetaMask/metamask-exten…
kumavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// log rpc activity | ||
module.exports = createLoggerMiddleware | ||
|
||
function createLoggerMiddleware({ origin }) { | ||
return function loggerMiddleware (req, res, next, end) { | ||
next((cb) => { | ||
if (res.error) { | ||
log.error('Error in RPC response:\n', res) | ||
} | ||
if (req.isMetamaskInternal) return | ||
log.info(`RPC (${origin}):`, req, '->', res) | ||
cb() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// append dapp origin domain to request | ||
module.exports = createOriginMiddleware | ||
|
||
function createOriginMiddleware({ origin }) { | ||
return function originMiddleware (req, res, next, end) { | ||
req.origin = origin | ||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
module.exports = createProviderMiddleware | ||
|
||
// forward requests to provider | ||
function createProviderMiddleware({ provider }) { | ||
return (req, res, next, end) => { | ||
provider.sendAsync(req, (err, _res) => { | ||
if (err) return end(err) | ||
res.result = _res.result | ||
end() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,56 @@ | ||
const pipe = require('pump') | ||
const StreamProvider = require('web3-stream-provider') | ||
const pump = require('pump') | ||
const RpcEngine = require('json-rpc-engine') | ||
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware') | ||
const createStreamMiddleware = require('json-rpc-middleware-stream') | ||
const LocalStorageStore = require('obs-store') | ||
const ObjectMultiplex = require('./obj-multiplex') | ||
const createRandomId = require('./random-id') | ||
const ObjectMultiplex = require('obj-multiplex') | ||
|
||
module.exports = MetamaskInpageProvider | ||
|
||
function MetamaskInpageProvider (connectionStream) { | ||
const self = this | ||
|
||
// setup connectionStream multiplexing | ||
var multiStream = self.multiStream = ObjectMultiplex() | ||
pipe( | ||
const mux = self.mux = new ObjectMultiplex() | ||
pump( | ||
connectionStream, | ||
multiStream, | ||
mux, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you've made the naming more uniform across files. |
||
connectionStream, | ||
(err) => logStreamDisconnectWarning('MetaMask', err) | ||
) | ||
|
||
// subscribe to metamask public config (one-way) | ||
self.publicConfigStore = new LocalStorageStore({ storageKey: 'MetaMask-Config' }) | ||
pipe( | ||
multiStream.createStream('publicConfig'), | ||
pump( | ||
mux.createStream('publicConfig'), | ||
self.publicConfigStore, | ||
(err) => logStreamDisconnectWarning('MetaMask PublicConfigStore', err) | ||
) | ||
|
||
// ignore phishing warning message (handled elsewhere) | ||
multiStream.ignoreStream('phishing') | ||
mux.ignoreStream('phishing') | ||
|
||
// connect to async provider | ||
const asyncProvider = self.asyncProvider = new StreamProvider() | ||
pipe( | ||
asyncProvider, | ||
multiStream.createStream('provider'), | ||
asyncProvider, | ||
const streamMiddleware = createStreamMiddleware() | ||
pump( | ||
streamMiddleware.stream, | ||
mux.createStream('provider'), | ||
streamMiddleware.stream, | ||
(err) => logStreamDisconnectWarning('MetaMask RpcProvider', err) | ||
) | ||
// start and stop polling to unblock first block lock | ||
|
||
self.idMap = {} | ||
// handle sendAsync requests via dapp-side rpc engine | ||
const rpcEngine = new RpcEngine() | ||
rpcEngine.push(createIdRemapMiddleware()) | ||
rpcEngine.push(streamMiddleware) | ||
self.rpcEngine = rpcEngine | ||
} | ||
|
||
// handle sendAsync requests via asyncProvider | ||
// also remap ids inbound and outbound | ||
MetamaskInpageProvider.prototype.sendAsync = function (payload, cb) { | ||
const self = this | ||
|
||
// rewrite request ids | ||
const request = eachJsonMessage(payload, (_message) => { | ||
const message = Object.assign({}, _message) | ||
const newId = createRandomId() | ||
self.idMap[newId] = message.id | ||
message.id = newId | ||
return message | ||
}) | ||
|
||
// forward to asyncProvider | ||
self.asyncProvider.sendAsync(request, (err, _res) => { | ||
if (err) return cb(err) | ||
// transform messages to original ids | ||
const res = eachJsonMessage(_res, (message) => { | ||
const oldId = self.idMap[message.id] | ||
delete self.idMap[message.id] | ||
message.id = oldId | ||
return message | ||
}) | ||
cb(null, res) | ||
}) | ||
self.rpcEngine.handle(payload, cb) | ||
} | ||
|
||
|
||
|
@@ -124,14 +107,6 @@ MetamaskInpageProvider.prototype.isMetaMask = true | |
|
||
// util | ||
|
||
function eachJsonMessage (payload, transformFn) { | ||
if (Array.isArray(payload)) { | ||
return payload.map(transformFn) | ||
} else { | ||
return transformFn(payload) | ||
} | ||
} | ||
|
||
function logStreamDisconnectWarning (remoteLabel, err) { | ||
let warningMsg = `MetamaskInpageProvider - lost connection to ${remoteLabel}` | ||
if (err) warningMsg += '\n' + err.stack | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer
log.warn()
forloglevel
support.