Skip to content

Commit

Permalink
fix(NODE-3688): check handshake error when applying label
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Apr 20, 2022
1 parent 5b28e91 commit fecb104
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
MongoNetworkError,
MongoNetworkTimeoutError,
MongoRuntimeError,
MongoServerError
MongoServerError,
needsRetryableWriteLabel
} from '../error';
import { Callback, ClientMetadata, HostAddress, makeClientMetadata, ns } from '../utils';
import { AuthContext, AuthProvider } from './auth/auth_provider';
Expand Down Expand Up @@ -187,6 +188,9 @@ function performInitialHandshake(
if (err) {
if (err instanceof MongoError) {
err.addErrorLabel(MongoErrorLabel.HandshakeError);
if (needsRetryableWriteLabel(err, response.maxWireVersion)) {
err.addErrorLabel(MongoErrorLabel.RetryableWriteError);
}
}
return callback(err);
}
Expand Down
21 changes: 11 additions & 10 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,16 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
return true;
}

if (
maxWireVersion >= 9 ||
(error instanceof MongoError && error.hasErrorLabel(MongoErrorLabel.RetryableWriteError))
) {
// If we already have the error label no need to add it again. 4.4+ servers add the label.
return false;
if (error instanceof MongoError) {
if (
(maxWireVersion >= 9 || error.hasErrorLabel(MongoErrorLabel.RetryableWriteError)) &&
!error.hasErrorLabel(MongoErrorLabel.HandshakeError)
) {
// If we already have the error label no need to add it again. 4.4+ servers add the label.
// In the case where we have a handshake error, need to fall down to the logic checking
// the codes.
return false;
}
}

if (error instanceof MongoWriteConcernError) {
Expand All @@ -781,10 +785,7 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
}

export function isRetryableWriteError(error: MongoError): boolean {
return (
error.hasErrorLabel(MongoErrorLabel.RetryableWriteError) ||
error.hasErrorLabel(MongoErrorLabel.HandshakeError)
);
return error.hasErrorLabel(MongoErrorLabel.RetryableWriteError);
}

/** Determines whether an error is something the driver should attempt to retry */
Expand Down

0 comments on commit fecb104

Please sign in to comment.