Skip to content

Commit 7b6f5c4

Browse files
authored
Merge pull request #1666 from tommarien/fix/emit-end-if-stream-receives-end
fix(connection): ensure pooled connections get released
2 parents cc64575 + 2dfee95 commit 7b6f5c4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/connection.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class Connection extends EventEmitter {
9696
}
9797
this.packetParser.execute(data);
9898
});
99+
this.stream.on('end', () => {
100+
// emit the end event so that the pooled connection can close the connection
101+
this.emit('end');
102+
});
99103
this.stream.on('close', () => {
100104
// we need to set this flag everywhere where we want connection to close
101105
if (this._closing) {

test/integration/test-pool-end.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const { createPool } = require('../common.js');
4+
const assert = require('assert');
5+
6+
const pool = createPool();
7+
8+
pool.getConnection((err, conn) => {
9+
assert.ifError(err);
10+
11+
assert(pool._allConnections.length === 1);
12+
assert(pool._freeConnections.length === 0);
13+
14+
// emit the end event, so the connection gets removed from the pool
15+
conn.stream.emit('end');
16+
17+
assert(pool._allConnections.length === 0);
18+
assert(pool._freeConnections.length === 0);
19+
20+
// As the connection has not really ended we need to do this ourselves
21+
conn.destroy();
22+
});

0 commit comments

Comments
 (0)