async-listener icon indicating copy to clipboard operation
async-listener copied to clipboard

When required in combination with supertest, causes eventemitter memory leak

Open STRML opened this issue 10 years ago • 15 comments

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.

STRML avatar Feb 07 '15 11:02 STRML

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

hayes avatar Feb 07 '15 19:02 hayes

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 avatar Feb 08 '15 06:02 STRML

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

STRML avatar Feb 08 '15 07:02 STRML

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

hayes avatar Feb 10 '15 02:02 hayes

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').

zanemcca avatar Feb 12 '15 17:02 zanemcca

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').

nicolasgramlich avatar Dec 08 '15 19:12 nicolasgramlich

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

dh376 avatar Jan 22 '16 22:01 dh376

Same here with a loopback app

DaGaMs avatar May 01 '16 13:05 DaGaMs

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

mitsos1os avatar Jun 06 '16 08:06 mitsos1os

any updates? 👀 still an issue in 2021 👎

imjordanxd avatar Feb 04 '21 05:02 imjordanxd

Any no update or workaround about this issue?

pshaddel avatar Aug 26 '22 19:08 pshaddel

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? 🤔

Qard avatar Aug 27 '22 06:08 Qard

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

rochdev avatar Aug 27 '22 14:08 rochdev

@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.

pshaddel avatar Aug 27 '22 14:08 pshaddel

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

Qard avatar Aug 27 '22 15:08 Qard