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(cmap): undo flipping of beforeHandshake flag for timeout errors #2813

Merged
merged 3 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 lib/cmap/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Connection extends EventEmitter {
if (issue.isTimeout) {
op.cb(
new MongoNetworkTimeoutError(`connection ${this.id} to ${this.address} timed out`, {
beforeHandshake: !!this[kIsMaster]
beforeHandshake: this[kIsMaster] == null
dariakp marked this conversation as resolved.
Show resolved Hide resolved
})
);
} else if (issue.isClose) {
Expand Down
4 changes: 1 addition & 3 deletions lib/core/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ class MongoNetworkError extends MongoError {
super(message);
this.name = 'MongoNetworkError';

if (options && options.beforeHandshake === true) {
this[kBeforeHandshake] = true;
}
this[kBeforeHandshake] = !!(options && options.beforeHandshake === true);
dariakp marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
59 changes: 59 additions & 0 deletions test/unit/cmap/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,63 @@ describe('Connection', function() {
}
);
});

it('streams that timeout after handshake should have error[kBeforeHandshake] be false', function(done) {
server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(mock.DEFAULT_ISMASTER_36);
}
// respond to no other requests to trigger timeout event
});

const address = server.address();
const options = {
bson: new BSON(),
connectionType: Connection,
host: address.host,
port: address.port
};

connect(options, (err, conn) => {
expect(err).to.be.a('undefined');
expect(conn).to.be.instanceOf(Connection);

conn.command('$admin.cmd', { ping: 1 }, { socketTimeout: 50 }, err => {
const beforeHandshakeSymbol = Object.getOwnPropertySymbols(err)[0];
expect(beforeHandshakeSymbol).to.be.a('symbol');
expect(err)
.to.have.property(beforeHandshakeSymbol)
.equals(false);

done();
});
});
});

it('streams that timeout before handshake should have error[kBeforeHandshake] be true', function(done) {
// respond to no requests to trigger timeout event
server.setMessageHandler(() => {});

const address = server.address();
const options = {
bson: new BSON(),
connectionType: Connection,
host: address.host,
port: address.port,
socketTimeout: 50
};

connect(options, (err, conn) => {
expect(conn).to.be.a('undefined');

const beforeHandshakeSymbol = Object.getOwnPropertySymbols(err)[0];
expect(beforeHandshakeSymbol).to.be.a('symbol');
expect(err)
.to.have.property(beforeHandshakeSymbol)
.equals(true);

done();
});
});
});