Skip to content

Commit 2de470a

Browse files
authored
fix(parallelCollectionScan): do not use implicit sessions on cursors
When we removed the session use from parallelCollectionScan, we introduced an error where parallelCollectionScan cursors would attempt to use implicit sessions on their cursors.
1 parent 50a9f65 commit 2de470a

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

lib/cursor.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,15 @@ function Cursor(bson, ns, cmd, options, topology, topologyOptions) {
143143
promiseLibrary: promiseLibrary,
144144
// Current doc
145145
currentDoc: null,
146-
// Optional ClientSession
147-
session: options.session
146+
// explicitlyIgnoreSession
147+
explicitlyIgnoreSession: options.explicitlyIgnoreSession
148148
};
149149

150+
// Optional ClientSession
151+
if (!options.explicitlyIgnoreSession && options.session) {
152+
this.s.session = options.session;
153+
}
154+
150155
// Translate correctly
151156
if (this.s.options.noCursorTimeout === true) {
152157
this.addCursorFlag('noCursorTimeout', true);
@@ -211,7 +216,7 @@ for (let name in CoreCursor.prototype) {
211216
}
212217

213218
Cursor.prototype._initImplicitSession = function() {
214-
if (!this.s.session && this.s.topology.hasSessionSupport()) {
219+
if (!this.s.explicitlyIgnoreSession && !this.s.session && this.s.topology.hasSessionSupport()) {
215220
this.s.session = this.s.topology.startSession({ owner: this });
216221
this.cursorState.session = this.s.session;
217222
}

lib/operations/collection_ops.js

+2
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ function parallelCollectionScan(coll, options, callback) {
10211021
null
10221022
);
10231023

1024+
options = Object.assign({ explicitlyIgnoreSession: true }, options);
1025+
10241026
const cursors = [];
10251027
// Add the raw back to the option
10261028
if (raw) options.raw = raw;

test/functional/find_tests.js

+54
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const test = require('./shared').assert;
33
const setupDatabase = require('./shared').setupDatabase;
44
const expect = require('chai').expect;
5+
const MongoClient = require('../../lib/mongo_client');
56

67
describe('Find', function() {
78
before(function() {
@@ -2624,6 +2625,59 @@ describe('Find', function() {
26242625
}
26252626
});
26262627

2628+
it('Should not use a session when using parallelCollectionScan', {
2629+
metadata: {
2630+
requires: {
2631+
mongodb: '>=3.6.0',
2632+
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
2633+
}
2634+
},
2635+
test: function(done) {
2636+
const configuration = this.configuration;
2637+
const client = new MongoClient(configuration.url());
2638+
2639+
client.connect(function(err, client) {
2640+
var db = client.db(configuration.db);
2641+
var docs = [];
2642+
2643+
// Insert some documents
2644+
for (var i = 0; i < 1000; i++) {
2645+
docs.push({ a: i });
2646+
}
2647+
2648+
// Get the collection
2649+
var collection = db.collection('parallelCollectionScan_4');
2650+
// Insert 1000 documents in a batch
2651+
collection.insert(docs, function(err) {
2652+
expect(err).to.be.null;
2653+
var numCursors = 1;
2654+
2655+
// Execute parallelCollectionScan command
2656+
collection.parallelCollectionScan({ numCursors: numCursors }, function(err, cursors) {
2657+
expect(err).to.be.null;
2658+
expect(cursors)
2659+
.to.be.an('array')
2660+
.with.lengthOf(1);
2661+
2662+
const cursor = cursors[0];
2663+
2664+
expect(cursor).to.not.have.nested.property('s.session');
2665+
expect(cursor)
2666+
.to.have.nested.property('s.explicitlyIgnoreSession')
2667+
.that.equals(true);
2668+
2669+
cursor.toArray(err => {
2670+
expect(err).to.be.null;
2671+
expect(cursor).to.not.have.nested.property('s.session');
2672+
2673+
cursor.close().then(() => client.close().then(() => done()));
2674+
});
2675+
});
2676+
});
2677+
});
2678+
}
2679+
});
2680+
26272681
it('Should correctly sort using text search on 2.6 or higher in find', {
26282682
// Add a tag that our runner can trigger on
26292683
// in this case we are setting that node needs to be higher than 0.10.X to run

0 commit comments

Comments
 (0)