expressjs / expressjs/compression

"drain" event listener leak when unpiping to response

Open
#135 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
2.8k
Forks
255
Avg merge
3d 15h
Merged PRs (30d)
3

Description

This is sort of a weird scenario, but this test case fails:

```js
it('should clean up event listeners when res is unpiped to', function (done) {
var listenerCount
var server = createServer({ threshold: 0 }, function (req, res) {
var times = 0
var int = setInterval(function () {
var rs = fs.createReadStream('does not exist')
rs.on('error', function (e) {
listenerCount = res.listenerCount('drain')
rs.unpipe(res)
})
rs.pipe(res)
if (times++ > 12) {
clearInterval(int)
res.end('hello, world')
}
})
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(function () {
assert.ok(listenerCount < 2)
})
.expect(200, done)
})
```

I hit this in some code that retries creating a read stream until the source exists. We clean up from our side: `rs.on("error", e => { rs.unpipe("res"); })`. Seems like `compression` needs to be cleaning up its listeners when `"unpipe"` happens.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.