Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure other command wait for auth response. #1414

Open
wants to merge 3 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 27 additions & 16 deletions lib/redis/event_handler.ts
Expand Up @@ -19,9 +19,14 @@ export function connectHandler(self) {
// AUTH command should be processed before any other commands
let flushed = false;
const { connectionEpoch } = self;
let authResolve;
const authPromise = new Promise((resolve) => {
authResolve = resolve;
});
if (self.condition.auth) {
self.auth(self.condition.auth, function (err) {
if (connectionEpoch !== self.connectionEpoch) {
authResolve();
return;
}
if (err) {
Expand Down Expand Up @@ -50,7 +55,11 @@ export function connectHandler(self) {
self.recoverFromFatalError(err, err);
}
}

authResolve();
});
} else {
authResolve();
}

if (self.condition.select) {
Expand All @@ -76,25 +85,27 @@ export function connectHandler(self) {
});

if (self.options.enableReadyCheck) {
self._readyCheck(function (err, info) {
if (connectionEpoch !== self.connectionEpoch) {
return;
}
if (err) {
if (!flushed) {
self.recoverFromFatalError(
new Error("Ready check failed: " + err.message),
err
);
authPromise.then(() => {
self._readyCheck(function (err, info) {
if (connectionEpoch !== self.connectionEpoch) {
return;
}
} else {
self.serverInfo = info;
if (self.connector.check(info)) {
exports.readyHandler(self)();
if (err) {
if (!flushed) {
self.recoverFromFatalError(
new Error("Ready check failed: " + err.message),
err
);
}
} else {
self.disconnect(true);
self.serverInfo = info;
if (self.connector.check(info)) {
exports.readyHandler(self)();
} else {
self.disconnect(true);
}
}
}
});
});
}
};
Expand Down
13 changes: 7 additions & 6 deletions test/functional/connection.ts
Expand Up @@ -32,6 +32,7 @@ describe("connection", function () {
times += 1;
if (times === 1) {
expect(command.name).to.eql("auth");
return command.resolve("OK");
} else if (times === 2) {
expect(command.name).to.eql("info");
redis.disconnect();
Expand Down Expand Up @@ -549,15 +550,15 @@ describe("disconnection", function () {
}
});
});

it("should clear the added script hashes interval even when no connection succeeded", function (done) {
let attempt = 0;
const redis = new Redis(0, 'localhost');
const redis = new Redis(0, "localhost");

redis.on("error", function () {
if(attempt < 5) {
attempt ++;
return
if (attempt < 5) {
attempt++;
return;
}

redis.quit();
Expand Down