@@ -38,6 +38,7 @@ import {
3838} from '../utils' ;
3939import { AggregateOperation } from './aggregate' ;
4040import { AbstractOperation , Aspect } from './operation' ;
41+ import { RunCommandOperation } from './run_command' ;
4142
4243const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES . IllegalOperation ;
4344const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
@@ -265,22 +266,22 @@ async function executeOperationWithRetries<
265266 if ( previousOperationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError ) ) {
266267 systemOverloadRetryAttempt += 1 ;
267268
269+ // if retryable writes or reads are not configured, throw.
270+ const isOperationConfiguredForRetry =
271+ ( hasReadAspect && topology . s . options . retryReads ) ||
272+ ( hasWriteAspect && topology . s . options . retryWrites ) ;
273+ const isRunCommand = operation instanceof RunCommandOperation ;
274+
268275 if (
269276 // if the SystemOverloadError is not retryable, throw.
270277 ! previousOperationError . hasErrorLabel ( MongoErrorLabel . RetryableError ) ||
271- ! (
272- // if retryable writes or reads are not configured, throw.
273- (
274- ( hasReadAspect && topology . s . options . retryReads ) ||
275- ( hasWriteAspect && topology . s . options . retryWrites )
276- )
277- )
278+ ! ( isOperationConfiguredForRetry || isRunCommand )
278279 ) {
279280 throw previousOperationError ;
280281 }
281282
282283 // if we have exhausted overload retry attempts, throw.
283- if ( systemOverloadRetryAttempt >= maxSystemOverloadRetryAttempts ) {
284+ if ( systemOverloadRetryAttempt > maxSystemOverloadRetryAttempts ) {
284285 throw previousOperationError ;
285286 }
286287
@@ -361,7 +362,10 @@ async function executeOperationWithRetries<
361362
362363 try {
363364 // If attempt > 0 and we are command batching we need to reset the batch.
364- if ( nonOverloadRetryAttempt > 0 && operation . hasAspect ( Aspect . COMMAND_BATCHING ) ) {
365+ if (
366+ ( nonOverloadRetryAttempt > 0 || systemOverloadRetryAttempt > 0 ) &&
367+ operation . hasAspect ( Aspect . COMMAND_BATCHING )
368+ ) {
365369 operation . resetBatch ( ) ;
366370 }
367371
0 commit comments