othiym23 / async-listener

polyfill version of the 0.11 version of the asyncListener API
https://www.npmjs.org/package/async-listener
BSD 2-Clause "Simplified" License
174 stars 52 forks source link

When required in combination with supertest, causes eventemitter memory leak #29

Open STRML opened 9 years ago

STRML commented 9 years ago

Supertest creates and tears down an HTTP request listener per-test, so it's not uncommon to see something like:

(node) warning: possible EventEmitter memory leak detected. 11 connection listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at Server.addListener (events.js:179:15)
    at Server._listen2 (~/api/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/index.js:64:10)
    at listen (net.js:1155:10)
    at Server.listen (net.js:1240:5)
    at Test.serverAddress (~/api/node_modules/loopback-testing/node_modules/supertest/lib/test.js:59:33)
    at new Test (~/api/node_modules/loopback-testing/node_modules/supertest/lib/test.js:38:12)
    at Object.obj.(anonymous function) [as post] (~/api/node_modules/loopback-testing/node_modules/supertest/index.js:25:14)
    at Context.<anonymous> (~/api/node_modules/loopback-testing/lib/helpers.js:243:46)
    at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:217:15)
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:258:10)
    at Immediate.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runner.js:275:5)
    at Immediate._onImmediate (~/api/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31)
    at processImmediate [as _immediateCallback] (timers.js:358:17)

Perhaps this should be tearing down on 'close'?

In any case, should this even be running in Node 0.12? I thought it was just a polyfill for something that was going to ship in core.

hayes commented 9 years ago

Interesting, the one thing I notice is the on('connection') that gets added to _listen2 should probably be changed to a once. That would probably fix this issue, it might be better to just make sure the listener was not already added before doing on. But I can definitely see how shutting down and reconnecting a a server could cause this issue. Its a pretty strange pattern, so not too surprising no one has run into this before

STRML commented 9 years ago

Yeah it is a bit strange. I don't think you'd see it much in production though, just test, so not the highest of priorities but it is a bit annoying.

STRML commented 9 years ago

In any case should this code even be running in Node 0.12?

hayes commented 9 years ago

it should be mostly functional, see #27 and #28

zanemcca commented 9 years ago

I have been having the same issue. It only happens if I use supertest(require('app')) but not when I connect to an already running app on a specified url using supertest('http://localhost:1234').

nicolasgramlich commented 8 years ago

Hey guys, is there any update on this issue? We're having a fairly big test suite and with this leak our tests just die in the middle of execution after a few thousand (≈ 4000) https calls through supertest(require('app').

dh376 commented 8 years ago

I'm running into same issue here. Only when I use supertest(require('app')) as well!

DaGaMs commented 8 years ago

Same here with a loopback app

mitsos1os commented 8 years ago

Same as @DaGaMs running with loopback app produces memory leak... Related: https://github.com/visionmedia/supertest/issues/307

imjordanxd commented 3 years ago

any updates? 👀 still an issue in 2021 👎

pshaddel commented 2 years ago

Any no update or workaround about this issue?

Qard commented 2 years ago

This hasn't been maintained for years. Why are you still using it when the functionality has been in Node.js core for several years now? 🀔

rochdev commented 2 years ago

The feature in Node core mentioned above: https://nodejs.org/api/async_context.html.

pshaddel commented 2 years ago

@Qard I am using supertest, and there were some memory leaks, I was here because there was an open issue on superagent which was related to this one.

Qard commented 2 years ago

Supertest doesn't use async-listener. Somewhere else in your dependency graph something is still using this module when it shouldn't.