Skip to content

Commit 1aecf96

Browse files
authored
fix: allow client connect after close (#2581)
NODE-2544
1 parent d7e505a commit 1aecf96

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/mongo_client.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,29 @@ export class MongoClient extends EventEmitter implements OperationParent {
349349
const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false;
350350

351351
return maybePromise(callback, cb => {
352+
const completeClose = (err?: AnyError) => {
353+
// clear out references to old topology
354+
this.topology = undefined;
355+
this.s.dbCache = new Map();
356+
this.s.sessions = new Set();
357+
358+
cb(err);
359+
};
360+
352361
if (this.topology == null) {
353-
return cb();
362+
completeClose();
363+
return;
354364
}
355365

356366
const topology = this.topology;
357367
topology.close({ force }, err => {
358368
const autoEncrypter = topology.s.options.autoEncrypter;
359369
if (!autoEncrypter) {
360-
cb(err);
370+
completeClose(err);
361371
return;
362372
}
363373

364-
autoEncrypter.teardown(force, err2 => cb(err || err2));
374+
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
365375
});
366376
});
367377
}

src/operations/connect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ export function connect(
197197
throw new Error('no callback function provided');
198198
}
199199

200+
// Has a connection already been established?
201+
if (mongoClient.topology && mongoClient.topology.isConnected()) {
202+
throw new MongoError(`'connect' cannot be called when already connected`);
203+
}
204+
200205
let didRequestAuthentication = false;
201206
const logger = new Logger('MongoClient', options);
202207

test/functional/connection.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const test = require('./shared').assert,
33
setupDatabase = require('./shared').setupDatabase,
44
expect = require('chai').expect;
5+
const withClient = require('./shared').withClient;
56

67
describe('Connection', function () {
78
before(function () {
@@ -273,4 +274,34 @@ describe('Connection', function () {
273274
done();
274275
}
275276
});
277+
278+
it(
279+
'should be able to connect again after close',
280+
withClient(function (client, done) {
281+
expect(client.isConnected()).to.be.true;
282+
283+
const collection = () => client.db('testReconnect').collection('test');
284+
collection().insertOne({ a: 1 }, (err, result) => {
285+
expect(err).to.not.exist;
286+
expect(result).to.exist;
287+
288+
client.close(err => {
289+
expect(err).to.not.exist;
290+
expect(client.isConnected()).to.be.false;
291+
292+
client.connect(err => {
293+
expect(err).to.not.exist;
294+
expect(client.isConnected()).to.be.true;
295+
296+
collection().insertOne({ b: 2 }, (err, result) => {
297+
expect(err).to.not.exist;
298+
expect(result).to.exist;
299+
expect(client.topology.isDestroyed()).to.be.false;
300+
done();
301+
});
302+
});
303+
});
304+
});
305+
})
306+
);
276307
});

test/functional/sessions.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('Sessions', function () {
123123
// verify that the `endSessions` command was sent
124124
const lastCommand = test.commands.started[test.commands.started.length - 1];
125125
expect(lastCommand.commandName).to.equal('endSessions');
126-
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
126+
expect(client.topology).to.not.exist;
127127
});
128128
});
129129
});
@@ -143,7 +143,7 @@ describe('Sessions', function () {
143143
// verify that the `endSessions` command was sent
144144
const lastCommand = test.commands.started[test.commands.started.length - 1];
145145
expect(lastCommand.commandName).to.equal('endSessions');
146-
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
146+
expect(client.topology).to.not.exist;
147147
});
148148
});
149149
}

0 commit comments

Comments
 (0)