Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit fc669c0

Browse files
authored
fix(connection): fixing leak in 3.0.0 (#235)
1 parent 7b67c00 commit fc669c0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/connection/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ Connection.prototype.connect = function(_options) {
600600
});
601601
self.connection.setTimeout(self.connectionTimeout);
602602
} else {
603-
self.connection.on('connect', function() {
603+
self.connection.once('connect', function() {
604604
// Set socket timeout instead of connection timeout
605605
self.connection.setTimeout(self.socketTimeout);
606606
// Emit connect event

test/tests/functional/pool_tests.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,53 @@ describe('Pool tests', function() {
3939
}
4040
});
4141

42+
it.only('Should only listen on connect once', {
43+
metadata: { requires: { topology: 'single' } },
44+
45+
test: function(done) {
46+
// Enable connections accounting
47+
Connection.enableConnectionAccounting();
48+
49+
// Attempt to connect
50+
var pool = new Pool(null, {
51+
host: this.configuration.host,
52+
port: this.configuration.port,
53+
bson: new Bson(),
54+
messageHandler: function() {}
55+
});
56+
57+
let connection;
58+
59+
// Add event listeners
60+
pool.on('connect', function(_pool) {
61+
var connections = _pool.allConnections();
62+
63+
process.nextTick(() => {
64+
// Now that we are in next tick, connection should still exist, but there
65+
// should be no connect listeners
66+
expect(connection.connection.listenerCount('connect')).to.equal(0);
67+
expect(connections).to.have.lengthOf(1);
68+
69+
_pool.destroy();
70+
71+
// Connection should be gone after destroy
72+
expect(_pool.allConnections()).to.have.lengthOf(0);
73+
Connection.disableConnectionAccounting();
74+
done();
75+
});
76+
});
77+
78+
expect(pool.allConnections()).to.have.lengthOf(0);
79+
80+
// Start connection
81+
pool.connect();
82+
83+
expect(pool.allConnections()).to.have.lengthOf(1);
84+
connection = pool.allConnections()[0];
85+
expect(connection.connection.listenerCount('connect')).to.equal(1);
86+
}
87+
});
88+
4289
it('should correctly write ismaster operation to the server', {
4390
metadata: { requires: { topology: 'single' } },
4491

0 commit comments

Comments
 (0)