Skip to content

Commit 701530a

Browse files
committed
fix: update to simple fix
1 parent 904af17 commit 701530a

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

lib/operations/execute_operation.js

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,25 @@ function executeOperation(topology, operation, callback) {
3030
throw new TypeError('This method requires a valid operation instance');
3131
}
3232

33-
if (isUnifiedTopology(topology) && topology.shouldCheckForSessionSupport()) {
34-
return selectServerForSessionSupport(topology, operation, callback);
33+
const Promise = topology.s.promiseLibrary;
34+
35+
let result;
36+
if (typeof callback !== 'function') {
37+
result = new Promise((resolve, reject) => {
38+
callback = (err, res) => {
39+
if (err) return reject(err);
40+
resolve(res);
41+
};
42+
});
3543
}
3644

37-
const Promise = topology.s.promiseLibrary;
45+
if (isUnifiedTopology(topology) && topology.shouldCheckForSessionSupport()) {
46+
// Recursive call to executeOperation after a server selection
47+
// shouldCheckForSessionSupport should return false after a server selection because
48+
// there will be data bearing servers
49+
selectServerForSessionSupport(topology, operation, callback);
50+
return result;
51+
}
3852

3953
// The driver sessions spec mandates that we implicitly create sessions for operations
4054
// that are not explicitly provided with a session.
@@ -45,20 +59,14 @@ function executeOperation(topology, operation, callback) {
4559
session = topology.startSession({ owner });
4660
operation.session = session;
4761
} else if (operation.session.hasEnded) {
48-
throw new MongoError('Use of expired sessions is not permitted');
62+
callback(new MongoError('Use of expired sessions is not permitted'));
63+
return result;
4964
}
50-
} else if (operation.session && operation.session.explicit) {
51-
throw new MongoError('Current topology does not support sessions');
52-
}
53-
54-
let result;
55-
if (typeof callback !== 'function') {
56-
result = new Promise((resolve, reject) => {
57-
callback = (err, res) => {
58-
if (err) return reject(err);
59-
resolve(res);
60-
};
61-
});
65+
} else if (operation.session && !topology.hasSessionSupport()) {
66+
// If the user passed an explicit session and we are still, after server selection,
67+
// trying to run against a topology that doesn't support sessions we error out.
68+
callback(new MongoError('Current topology does not support sessions'));
69+
return result;
6270
}
6371

6472
function executeCallback(err, result) {
@@ -78,15 +86,15 @@ function executeOperation(topology, operation, callback) {
7886
} else {
7987
operation.execute(executeCallback);
8088
}
81-
} catch (e) {
89+
} catch (error) {
8290
if (session && session.owner === owner) {
8391
session.endSession();
8492
if (operation.session === session) {
8593
operation.clearSession();
8694
}
8795
}
8896

89-
throw e;
97+
callback(error);
9098
}
9199

92100
return result;
@@ -141,11 +149,6 @@ function executeWithServerSelection(topology, operation, callback) {
141149
callback(err, null);
142150
return;
143151
}
144-
145-
if (serverSelectionOptions.session && topology.hasSessionSupport()) {
146-
return callback(new MongoError('Current topology does not support sessions'));
147-
}
148-
149152
const shouldRetryReads =
150153
topology.s.options.retryReads !== false &&
151154
operation.session &&
@@ -165,28 +168,13 @@ function executeWithServerSelection(topology, operation, callback) {
165168
// TODO: This is only supported for unified topology, it should go away once
166169
// we remove support for legacy topology types.
167170
function selectServerForSessionSupport(topology, operation, callback) {
168-
const Promise = topology.s.promiseLibrary;
169-
170-
let result;
171-
if (typeof callback !== 'function') {
172-
result = new Promise((resolve, reject) => {
173-
callback = (err, result) => {
174-
if (err) return reject(err);
175-
resolve(result);
176-
};
177-
});
178-
}
179-
180171
topology.selectServer(ReadPreference.primaryPreferred, err => {
181172
if (err) {
182-
callback(err);
183-
return;
173+
return callback(err);
184174
}
185175

186176
executeOperation(topology, operation, callback);
187177
});
188-
189-
return result;
190178
}
191179

192180
module.exports = executeOperation;

0 commit comments

Comments
 (0)