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

MaxListenersExceededWarning when sending contract (v1.0.0-beta.33) #1648

Closed
hexcowboy opened this issue May 22, 2018 · 21 comments · Fixed by #3190
Closed

MaxListenersExceededWarning when sending contract (v1.0.0-beta.33) #1648

hexcowboy opened this issue May 22, 2018 · 21 comments · Fixed by #3190
Assignees
Labels
1.x 1.0 related issues Bug Addressing a bug In Progress Currently being worked on

Comments

@hexcowboy
Copy link

hexcowboy commented May 22, 2018

Situation: When I send a deployed contract using contract.deploy().send(), the command line prints a warning:

  contracts
(node:23488) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit
    √ gets the accounts

  6 passing (1s)

Edit (14 April 2020): removed unnecessary information

@LuisOsta
Copy link

LuisOsta commented Jul 4, 2018

I have the same issue, have you found a solution yet?

@nivida nivida self-assigned this Aug 10, 2018
@nivida nivida added Bug Addressing a bug and removed Bug Addressing a bug labels Aug 10, 2018
@nivida
Copy link
Contributor

nivida commented Aug 10, 2018

@jackno Is this issue still there with beta-34?

@UnkySM
Copy link

UnkySM commented Aug 28, 2018

I've got the same issue with beta-35.

@xpepermint
Copy link

This problem relates to ganache.provider(). Here's a quick fix:

    const web3 = new Web3(ganache.provider({ ... }));
    web3.currentProvider.setMaxListeners(300); // or more :)

@adrianmcli
Copy link

@xpepermint I dug into this a little bit with the Ganache team, and it seems that the problem is that every request in Web3.js seems to add on another listener on the same "data" event, instead of having a master listener.

karlfloersch added a commit to karlfloersch/plasma-operator that referenced this issue Jan 15, 2019
Changes:
- Use Web3.utils instead of importing eth.js in multiple files
- Add timeout before tests start -- this is because now startup takes
too long
- Add some config options
- Suppress node warning coming from a web3 bug. See:
web3/web3.js#1648
@levino
Copy link
Contributor

levino commented Feb 8, 2019

Same problem here. @adrianmcli Could you be somewhat more specific please?

@nivida
Copy link
Contributor

nivida commented Feb 8, 2019

This is a problem of the ganache provider and not one of the Web3.js library. Please open an issue there for solving it.

@nivida nivida closed this as completed Feb 8, 2019
@nivida nivida added Bug Addressing a bug and removed more information needed labels Feb 8, 2019
@levino
Copy link
Contributor

levino commented Feb 8, 2019

No it is a bug of web3. I encounter the problem when using metamask in the browser.

@eccentricexit
Copy link

Same here.

@eggplantzzz
Copy link
Contributor

Is this still an issue for everyone or has a fix been made for this?

@adrianmcli
Copy link

adrianmcli commented Jun 12, 2019

@eggplantzzz no fix still. The problem is with how Web3.js attaches listeners haphazardly. I haven't looked at this in a few months, but I recall that a new listener is attached every time a constructor of some object is created. But it should really just be a singleton that each instance refers to.

I know that's pretty vague, but that's the only recollection I have. I'll have to dig in to this again later this month. Or maybe someone else can.

@adrianmcli
Copy link

adrianmcli commented Jun 12, 2019

Let's unpack this further, this is the stack trace I get when I enable --trace-warnings in node:

(node:49758) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:251:17)
    at Provider.addListener (events.js:267:10)
    at RequestManager.Object.<anonymous>.RequestManager.setProvider (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core-requestmanager/src/index.js:98:23)
    at Object.packageInit (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core/src/index.js:57:33)
    at Contract (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:63:10)
    at new Contract (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/src/index.js:148:22)
    at Contract.Object.<anonymous>.Contract.clone (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:560:12)
    at Object.contractDeployFormatter (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:878:57)
    at Object.callback (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core-method/src/index.js:323:70)
    at sendTxCallback (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core-method/src/index.js:487:29)

Do note that this is a project that is relying on Beta35, but I believe the cause is probably the same.

Whenever a new sendTx is created during deployment, RequestManager.setProvider is called, and each time new listeners are added (w/ Provider.addListener).

This also seems to happen when just creating new contract instances:

(node:49763) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:251:17)
    at Provider.addListener (events.js:267:10)
    at RequestManager.Object.<anonymous>.RequestManager.setProvider (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core-requestmanager/src/index.js:98:23)
    at Object.packageInit (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-core/src/index.js:57:33)
    at Contract (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:63:10)
    at new Contract (/Users/adrianli/dev/drizzle-utils/node_modules/web3-eth/src/index.js:148:22)
    at instanceFromArtifact (/Users/adrianli/dev/drizzle-utils/packages/get-contract-instance/instanceFromArtifact.js:559:27)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)

The real question is why setProvider is getting called each time, and even if it is called, shouldn't it clean up listeners before adding? I haven't looked into the provider code yet.

@skozin
Copy link

skozin commented Aug 18, 2019

I'm also seeing the same memory leak generated by the second stack trace when creating multiple contract instances connected to the MetaMask's provider. It turns out that web3 v1.2.1 adds a new listener each time a new contract instance is created:

image

Maybe this issue is worth to be reopened and investigated further? For our Dapp, this is a critical bug since we create a lot of contract instances through the lifetime of the application, and we cannot cache them.

A related issue in truffle: trufflesuite/truffle#1942.

@adrianmcli
Copy link

@nivida Can you re-open this issue? I think everyone is in agreement that this is not solved (and the problem is not with Ganache).

@gnidan gnidan reopened this Sep 18, 2019
@gnidan
Copy link
Contributor

gnidan commented Sep 18, 2019

@nivida I hope I'm not overstepping -___-

"with great power comes great responsibility"?

@nivida
Copy link
Contributor

nivida commented Sep 18, 2019

@gnidan Thanks for re-opening this issue. I had the same bug in the 2.x branch and already fixed it. We will fix it as soon as possible in the 1.x branch.

@nivida nivida added the 1.x 1.0 related issues label Oct 13, 2019
@wbt
Copy link
Contributor

wbt commented Apr 14, 2020

FYI: When git cloning and building the latest version of web3 from the 1.x branch with #3190 merged, I still get the MaxListenersExceededWarning.

@cgewecke
Copy link
Collaborator

@wbt We've seen this go away in our E2E tests but they are relatively modest in scope...what needs to be done to trigger the warning?

@wbt
Copy link
Contributor

wbt commented Apr 14, 2020

@cgewecke Does trufflesuite/truffle#1942 help?
To be clear, I'm seeing this in a more complex dapp, but the top post there was the result of spending the time to try to boil the issue down to a public test case.

@cgewecke
Copy link
Collaborator

cgewecke commented Apr 14, 2020

@wbt Ok yes I can reproduce with the test written #3469. Thanks for flagging this.

From what I can see the problem is better than before but if you're calling setProvider repeatedly (e.g on lots of contract objects) it will hit the 10 limit.

@cgewecke cgewecke reopened this Apr 14, 2020
@ryanio ryanio mentioned this issue Apr 15, 2020
13 tasks
@cgewecke
Copy link
Collaborator

cgewecke commented Jun 9, 2020

This is now configurable. If you're using Websockets and calling contract.setProvider, Web3 will attach four listeners.

@cgewecke cgewecke closed this as completed Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug In Progress Currently being worked on
Projects
None yet