Skip to content

Commit 797e122

Browse files
committed
refactor(server): do not permit commands when server is closed
1 parent 32316e4 commit 797e122

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/core/sdam/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ class Server extends EventEmitter {
240240
(callback = options), (options = {}), (options = options || {});
241241
}
242242

243+
if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
244+
callback(new MongoError('server is closed'));
245+
return;
246+
}
247+
243248
const error = basicReadValidations(this, options);
244249
if (error) {
245250
return callback(error, null);
@@ -289,6 +294,11 @@ class Server extends EventEmitter {
289294
* @param {function} callback
290295
*/
291296
query(ns, cmd, cursorState, options, callback) {
297+
if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
298+
callback(new MongoError('server is closed'));
299+
return;
300+
}
301+
292302
wireProtocol.query(this, ns, cmd, cursorState, options, (err, result) => {
293303
if (err) {
294304
if (options.session && err instanceof MongoNetworkError) {
@@ -313,6 +323,11 @@ class Server extends EventEmitter {
313323
* @param {function} callback
314324
*/
315325
getMore(ns, cursorState, batchSize, options, callback) {
326+
if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
327+
callback(new MongoError('server is closed'));
328+
return;
329+
}
330+
316331
wireProtocol.getMore(this, ns, cursorState, batchSize, options, (err, result) => {
317332
if (err) {
318333
if (options.session && err instanceof MongoNetworkError) {
@@ -336,6 +351,14 @@ class Server extends EventEmitter {
336351
* @param {function} callback
337352
*/
338353
killCursors(ns, cursorState, callback) {
354+
if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) {
355+
if (typeof callback === 'function') {
356+
callback(new MongoError('server is closed'));
357+
}
358+
359+
return;
360+
}
361+
339362
wireProtocol.killCursors(this, ns, cursorState, (err, result) => {
340363
if (err && isSDAMUnrecoverableError(err, this)) {
341364
this.emit('error', err);
@@ -438,6 +461,11 @@ function executeWriteOperation(args, options, callback) {
438461
const ns = args.ns;
439462
const ops = Array.isArray(args.ops) ? args.ops : [args.ops];
440463

464+
if (server.s.state === STATE_CLOSING || server.s.state === STATE_CLOSED) {
465+
callback(new MongoError('server is closed'));
466+
return;
467+
}
468+
441469
const error = basicWriteValidations(server, options);
442470
if (error) {
443471
callback(error, null);

0 commit comments

Comments
 (0)