@@ -240,6 +240,11 @@ class Server extends EventEmitter {
240
240
( callback = options ) , ( options = { } ) , ( options = options || { } ) ;
241
241
}
242
242
243
+ if ( this . s . state === STATE_CLOSING || this . s . state === STATE_CLOSED ) {
244
+ callback ( new MongoError ( 'server is closed' ) ) ;
245
+ return ;
246
+ }
247
+
243
248
const error = basicReadValidations ( this , options ) ;
244
249
if ( error ) {
245
250
return callback ( error , null ) ;
@@ -289,6 +294,11 @@ class Server extends EventEmitter {
289
294
* @param {function } callback
290
295
*/
291
296
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
+
292
302
wireProtocol . query ( this , ns , cmd , cursorState , options , ( err , result ) => {
293
303
if ( err ) {
294
304
if ( options . session && err instanceof MongoNetworkError ) {
@@ -313,6 +323,11 @@ class Server extends EventEmitter {
313
323
* @param {function } callback
314
324
*/
315
325
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
+
316
331
wireProtocol . getMore ( this , ns , cursorState , batchSize , options , ( err , result ) => {
317
332
if ( err ) {
318
333
if ( options . session && err instanceof MongoNetworkError ) {
@@ -336,6 +351,14 @@ class Server extends EventEmitter {
336
351
* @param {function } callback
337
352
*/
338
353
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
+
339
362
wireProtocol . killCursors ( this , ns , cursorState , ( err , result ) => {
340
363
if ( err && isSDAMUnrecoverableError ( err , this ) ) {
341
364
this . emit ( 'error' , err ) ;
@@ -438,6 +461,11 @@ function executeWriteOperation(args, options, callback) {
438
461
const ns = args . ns ;
439
462
const ops = Array . isArray ( args . ops ) ? args . ops : [ args . ops ] ;
440
463
464
+ if ( server . s . state === STATE_CLOSING || server . s . state === STATE_CLOSED ) {
465
+ callback ( new MongoError ( 'server is closed' ) ) ;
466
+ return ;
467
+ }
468
+
441
469
const error = basicWriteValidations ( server , options ) ;
442
470
if ( error ) {
443
471
callback ( error , null ) ;
0 commit comments