Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Too many listeners #105

Closed
danfinlay opened this issue Jan 5, 2017 · 17 comments · Fixed by #217
Closed

Too many listeners #105

danfinlay opened this issue Jan 5, 2017 · 17 comments · Fixed by #217
Labels

Comments

@danfinlay
Copy link
Contributor

When sending a transaction, I get an error:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

It's coming from the Filter subprovider onFilterChange _ready.on method. That _ready object needs its listener max raised, or has a memory leak.

@danfinlay danfinlay added the bug label Jan 5, 2017
@ghost
Copy link

ghost commented Apr 4, 2017

Trying to watch contract events causes the same warning:

(node:6485) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unlock listeners added. Use emitter.setMaxListeners() to increase limit

const Web3 = require('web3');
const ProviderEngine = require('web3-provider-engine/index.js');
const ZeroClientProvider = require('web3-provider-engine/zero.js');

const engine = ZeroClientProvider({
  getAccounts: function(){},
  rpcUrl: 'https://ropsten.infura.io/jD3JIY4fsscUvTqJVMF',
});
const web3 = new Web3(engine);

const contractAddress = '0xc740ab099da657612143c34c5f7593a3af5ee6e8';
const contractAbi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"emit","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"x","type":"uint256"}],"name":"myEvent","type":"event"}];
const contract = web3.eth.contract(contractAbi).at(contractAddress);

//THIS CAUSES THE WARNING
const event = contract.myEvent({x: null});
event.watch(function(error, result){
  console.log(error, result.args.x.toNumber());
});
//THIS CAUSES THE WARNING

@ghost
Copy link

ghost commented Apr 25, 2017

Adding this line to the top of server.js makes the error not show up again:
require('events').EventEmitter.prototype._maxListeners = 100;

@ghost
Copy link

ghost commented Apr 27, 2017

Increasing the limit doesn't help.
Still get the same error: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 101 unlock listeners added. Use emitter.setMaxListeners() to increase limit

@danfinlay
Copy link
Contributor Author

I've been inclined to think this is just a warning, because we use a very event-emitter heavy design pattern. Do you think it's a real memory leak? Have you tried looking in the memory inspector? (Just asking, otherwise we'll just have to follow up & investigate).

@ghost
Copy link

ghost commented Apr 27, 2017

@FlySwatter, After this warning shows up the application no longer receives events from the network(not sure). The code I'm using is posted above. I'm also not sure if it's memory leak or not. Could you recomend a memory inspector for Ubuntu? I haven't used memory inspectors before.

@danfinlay
Copy link
Contributor Author

Chrome (and I think Chromium) has a great memory inspector built in. Once you've pulled up the console with the warning, go to the Timeline tab, check the memory box, and record while you reproduce the warning, and then maybe stop watching the event.

If the memory goes up but doesn't go down, it's a leak.

After this warning shows up the application no longer receives events from the network(not sure)

I wish you were sure ;)

If this really is breaking the connection, you should have a message in your main page console like "MetaMask Inpage Provider lost connection". Are you seeing that?

Anyways, if it seems to really be breaking the connection, that's a much bigger deal than I thought it was. I thought it was just a warning.

Will try to put some time on this soon.

@ghost
Copy link

ghost commented Apr 27, 2017

@FlySwatter, thank you for the quick introduction into the browser memory inspection. I should have been clear that I'm using this code in the backend. I have never run this code in the browser. My NodeJS server uses it to listen to contract events. Anyway, I will post something more conclusive here when I get enough information.

@skilesare
Copy link

I'm having this issue all of a sudden as well. Breaks everything. I'll investigate more later, but closing chrome and my test rpc doesn't solve the issue. It makes me a sad panda. This was working 20 minutes ago. I've been testing and calling the filter function a bunch, but now it seems to have a bunch of open connections stored somewhere.

@seanwbren
Copy link

seanwbren commented Sep 19, 2017

I too suddenly started having this issue about 5-6 hours ago. If I load my site with metamask disabled, no errors, but when MetaMask is enabled I get the "11 listeners error". It seems to be affecting my site being able to grab the new web3.eth.accounts[0] object when accounts are switched.

@FlySwatter the error logs show the lines pump(mux, pongStream, mux, function (err) { and var phishingStream = mux.createStream('phishing'); to be the culprits, from setupStreams(); in if (shouldInjectWeb3())

And actually this seems to be locking the web3.eth.accounts[0] object to whatever it is instantiated as. My MetaMask UI says I'm on account 0xB26A.. but console.log prints accounts[0] to be 0xfd87c6...

@danfinlay
Copy link
Contributor Author

To address this bug, we've done a hotfix rollback to 3.10.0, you should see the previous behavior resume soon, and we'll fix this again soon.

@okwme
Copy link

okwme commented Sep 19, 2017

i'm having this same error this morning with no code changes

@danfinlay
Copy link
Contributor Author

What version are you on? Should be fixed again with 3.10.2.

@okwme
Copy link

okwme commented Sep 19, 2017

i'm not sure, after pulling out my hair a minute i just uninstalled and reinstalled and the issue is fixed

@seanwbren
Copy link

Thanks for the quick fix!

@danfinlay
Copy link
Contributor Author

Oh hey, sorry for missing this thread when updating MetaMask/metamask-extension#2122

If you were affected by this issue, we have a partial fix, but we're looking for people who still have issues. If you're a developer, and had this issue, it would help us if you could manually install this build and if you have issues, give us reproduction steps:

metamask-chrome-3.10.3-pre2.zip

@riteable
Copy link

I don't seem to have this issue in the browser, but I do get the warning in Node.js.

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unlock listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:262:19)
    at Stoplight.addListener (events.js:279:10)
    at Stoplight.once (events.js:308:8)
    at Stoplight.await (/.../node_modules/web3-provider-engine/util/stoplight.js:30:10)
    at FilterSubprovider.handleRequest (/.../node_modules/web3-provider-engine/subproviders/filters.js:69:24)
    at next (/.../node_modules/web3-provider-engine/index.js:112:18)
    at Web3ProviderEngine._handleAsync (/.../node_modules/web3-provider-engine/index.js:99:3)
    at /.../node_modules/async/internal/map.js:27:9
    at eachOfArrayLike (/.../node_modules/async/eachOf.js:65:9)
    at exports.default (/.../node_modules/async/eachOf.js:9:5)
    at _asyncMap (/.../node_modules/async/internal/map.js:25:5)

I'm using 0x on the server side and using the web3 provider engine to listen to certain 0x log events (fill, cancel, error). I've added the filter and RPC subproviders. The RPC subprovider is connected to Infura.

kumavis added a commit that referenced this issue Apr 10, 2018
#105 adding option for setting max stoplight listeners.
@skozin
Copy link

skozin commented Apr 22, 2018

@riteable did you sort it out? We're getting the same warning in Node. We're also connecting to Infura (via truffle-hdwallet-provider) and using a filter to watch events.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants