Skip to content

Commit

Permalink
Merge pull request #1666 from tommarien/fix/emit-end-if-stream-receiv…
Browse files Browse the repository at this point in the history
…es-end

fix(connection): ensure pooled connections get released
  • Loading branch information
sidorares authored Dec 4, 2022
2 parents cc64575 + 2dfee95 commit 7b6f5c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class Connection extends EventEmitter {
}
this.packetParser.execute(data);
});
this.stream.on('end', () => {
// emit the end event so that the pooled connection can close the connection
this.emit('end');
});
this.stream.on('close', () => {
// we need to set this flag everywhere where we want connection to close
if (this._closing) {
Expand Down
22 changes: 22 additions & 0 deletions test/integration/test-pool-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const { createPool } = require('../common.js');
const assert = require('assert');

const pool = createPool();

pool.getConnection((err, conn) => {
assert.ifError(err);

assert(pool._allConnections.length === 1);
assert(pool._freeConnections.length === 0);

// emit the end event, so the connection gets removed from the pool
conn.stream.emit('end');

assert(pool._allConnections.length === 0);
assert(pool._freeConnections.length === 0);

// As the connection has not really ended we need to do this ourselves
conn.destroy();
});

0 comments on commit 7b6f5c4

Please sign in to comment.