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

Commit 7633f10

Browse files
authored
fix(connection): fixing leaky connection (#234)
1 parent de6d220 commit 7633f10

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/connection/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ Connection.prototype.connect = function(_options) {
464464
});
465465
self.connection.setTimeout(self.connectionTimeout);
466466
} else {
467-
self.connection.on('connect', function() {
467+
self.connection.once('connect', function() {
468468
// Set socket timeout instead of connection timeout
469469
self.connection.setTimeout(self.socketTimeout);
470470
// Emit connect event

test/tests/functional/pool_tests.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ exports['Should correctly connect pool to single server'] = {
3737
}
3838
}
3939

40+
exports['Should only listen on connect once'] = {
41+
metadata: { requires: { topology: "single" } },
42+
43+
test: function(configuration, test) {
44+
var Pool = require('../../../lib/connection/pool')
45+
, Connection = require('../../../lib/connection/connection')
46+
, bson = require('bson');
47+
48+
// Enable connections accounting
49+
Connection.enableConnectionAccounting();
50+
51+
// Attempt to connect
52+
var pool = new Pool({
53+
host: configuration.host
54+
, port: configuration.port
55+
, bson: new bson()
56+
, messageHandler: function() {}
57+
});
58+
59+
var connection;
60+
61+
// Add event listeners
62+
pool.on('connect', function(_pool) {
63+
process.nextTick(() => {
64+
// Now that we are in next tick, connection should still exist, but there
65+
// should be no connect listeners
66+
test.equal(0, connection.connection.listenerCount('connect'));
67+
test.equal(1, pool.allConnections().length);
68+
69+
_pool.destroy();
70+
71+
// Connection should be gone after destroy
72+
test.equal(0, pool.allConnections().length);
73+
Connection.disableConnectionAccounting();
74+
test.done();
75+
});
76+
});
77+
78+
test.equal(0, pool.allConnections().length);
79+
80+
// Start connection
81+
pool.connect();
82+
83+
test.equal(1, pool.allConnections().length);
84+
connection = pool.allConnections()[0];
85+
test.equal(1, connection.connection.listenerCount('connect'));
86+
}
87+
}
88+
4089
exports['Should properly emit errors on forced destroy'] = {
4190
metadata: { requires: { topology: "single" } },
4291

0 commit comments

Comments
 (0)