Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow client connect after close #2615

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ function processNewChange(
}

function processError(changeStream: ChangeStream, error: AnyError, callback?: Callback) {
const topology = getTopology(changeStream.parent);
const cursor = changeStream.cursor;

// If the change stream has been closed explicitly, do not process error.
Expand Down Expand Up @@ -618,6 +617,7 @@ function processError(changeStream: ChangeStream, error: AnyError, callback?: Ca
// close internal cursor, ignore errors
cursor.close();

const topology = getTopology(changeStream.parent);
waitForTopologyConnected(topology, { readPreference: cursor.readPreference }, err => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing the topology too early in this function causes failing tests when processError is called after client close.

// if the topology can't reconnect, close the stream
if (err) return unresumableError(err);
Expand Down
3 changes: 3 additions & 0 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ export class MongoClient extends EventEmitter implements OperationParent {
return cb();
}

// clear out references to old topology
const topology = this.topology;
this.topology = undefined;

topology.close({ force }, err => {
const autoEncrypter = topology.s.options.autoEncrypter;
if (!autoEncrypter) {
Expand Down
5 changes: 5 additions & 0 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ export function connect(
throw new Error('no callback function provided');
}

// Has a connection already been established?
if (mongoClient.topology && mongoClient.topology.isConnected()) {
throw new MongoError(`'connect' cannot be called when already connected`);
}
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved

let didRequestAuthentication = false;
const logger = new Logger('MongoClient', options);

Expand Down
54 changes: 53 additions & 1 deletion test/functional/connection.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const { withClient, setupDatabase } = require('./shared');
const test = require('./shared').assert,
setupDatabase = require('./shared').setupDatabase,
expect = require('chai').expect;
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved

describe('Connection', function () {
Expand Down Expand Up @@ -274,3 +274,55 @@ describe('Connection', function () {
}
});
});

it(
'should be able to connect again after close',
withClient(function (client, done) {
expect(client.isConnected()).to.be.true;

const collection = client.db('testReconnect').collection('test');
collection.insertOne({ a: 1 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;

client.close(err => {
expect(err).to.not.exist;
expect(client.isConnected()).to.be.false;

client.connect(err => {
expect(err).to.not.exist;
expect(client.isConnected()).to.be.true;

collection.insertOne({ b: 2 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
expect(client.topology.isDestroyed()).to.be.false;
done();
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
});
})
);

it(
'should correctly fail on retry when client has been closed',
withClient(function (client, done) {
expect(client.isConnected()).to.be.true;
const collection = client.db('shouldCorrectlyFailOnRetry').collection('test');
collection.insertOne({ a: 1 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;

client.close(true, function (err) {
expect(err).to.not.exist;
expect(client.isConnected()).to.be.false;

expect(() => {
collection.insertOne({ a: 2 });
}).to.throw(/must be connected/);
done();
});
});
})
);
5 changes: 3 additions & 2 deletions test/functional/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2637,15 +2637,16 @@ describe('Find', function () {
expect(err).to.not.exist;

let selectedServer;
const selectServerStub = sinon.stub(client.topology, 'selectServer').callsFake(function () {
const topology = client.topology;
const selectServerStub = sinon.stub(topology, 'selectServer').callsFake(function () {
const args = Array.prototype.slice.call(arguments);
const originalCallback = args.pop();
args.push((err, server) => {
selectedServer = server;
originalCallback(err, server);
});

return client.topology.selectServer.wrappedMethod.apply(this, args);
return topology.selectServer.wrappedMethod.apply(this, args);
});

const collection = client.db().collection('test_read_preference');
Expand Down
4 changes: 2 additions & 2 deletions test/functional/sessions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('Sessions', function () {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
expect(client.topology).to.not.exist;
});
});
});
Expand All @@ -143,7 +143,7 @@ describe('Sessions', function () {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
expect(client.topology).to.not.exist;
});
});
}
Expand Down