Skip to content

Commit

Permalink
fix: ETIMEDOUT error with Bluebird when connecting. (#925)
Browse files Browse the repository at this point in the history
There's a race condition when making connections in 4.11.x,
  which will happen easier with Bluebird & Unix socks.

Thank you to @jeremytm & @p3x-robot.

Close #918
  • Loading branch information
luin committed Jul 13, 2019
1 parent a90f0de commit 4bce38b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
4 changes: 1 addition & 3 deletions lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import SentinelIterator from "./SentinelIterator";
import { ISentinelAddress } from "./types";
import AbstractConnector, { ErrorEmitter } from "../AbstractConnector";
import { NetStream, CallbackFunction } from "../../types";
import * as PromiseContainer from "../../promiseContainer";
import Redis from "../../redis";

const debug = Debug("SentinelConnector");
Expand Down Expand Up @@ -87,10 +86,9 @@ export default class SentinelConnector extends AbstractConnector {
this.retryAttempts = 0;

let lastError;
const _Promise = PromiseContainer.get();

const connectToNext = () =>
new _Promise<NetStream>((resolve, reject) => {
new Promise<NetStream>((resolve, reject) => {
const endpoint = this.sentinelIterator.next();

if (endpoint.done) {
Expand Down
41 changes: 22 additions & 19 deletions lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createConnection, TcpNetConnectOpts, IpcNetConnectOpts } from "net";
import { connect as createTLSConnection, SecureContextOptions } from "tls";
import { CONNECTION_CLOSED_ERROR_MSG } from "../utils";
import AbstractConnector, { ErrorEmitter } from "./AbstractConnector";
import * as PromiseContainer from "../promiseContainer";
import { NetStream } from "../types";

export function isIIpcConnectionOptions(
Expand Down Expand Up @@ -52,27 +51,31 @@ export default class StandaloneConnector extends AbstractConnector {
Object.assign(connectionOptions, options.tls);
}

const _Promise = PromiseContainer.get();
return new _Promise<NetStream>((resolve, reject) => {
process.nextTick(() => {
if (!this.connecting) {
reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}
// TODO:
// We use native Promise here since other Promise
// implementation may use different schedulers that
// cause issue when the stream is resolved in the
// next tick.
// Should use the provided promise in the next major
// version and do not connect before resolved.
return new Promise<NetStream>((resolve, reject) => {
if (!this.connecting) {
reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}

try {
if (options.tls) {
this.stream = createTLSConnection(connectionOptions);
} else {
this.stream = createConnection(connectionOptions);
}
} catch (err) {
reject(err);
return;
try {
if (options.tls) {
this.stream = createTLSConnection(connectionOptions);
} else {
this.stream = createConnection(connectionOptions);
}
} catch (err) {
reject(err);
return;
}

resolve(this.stream);
});
resolve(this.stream);
});
}
}
25 changes: 20 additions & 5 deletions test/functional/connection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Socket } from "net";
import * as net from "net";
import Redis from "../../lib/redis";
import * as sinon from "sinon";
import { expect } from "chai";
import MockServer from "../helpers/mock_server";
import * as Bluebird from "bluebird";

describe("connection", function() {
it('should emit "connect" when connected', function(done) {
Expand Down Expand Up @@ -77,9 +78,9 @@ describe("connection", function() {
var set = false;

// TODO: use spy
// @ts-ignore
const stub = sinon
.stub(Socket.prototype, "setTimeout")
.stub(net.Socket.prototype, "setTimeout")
// @ts-ignore
.callsFake(timeout => {
if (timeout === connectTimeout) {
set = true;
Expand All @@ -103,9 +104,9 @@ describe("connection", function() {
let timedoutCalled = false;

// TODO: use spy
// @ts-ignore
sinon
.stub(Socket.prototype, "setTimeout")
.stub(net.Socket.prototype, "setTimeout")
// @ts-ignore
.callsFake((timeout, callback) => {
if (timeout === 0) {
if (!isReady) {
Expand Down Expand Up @@ -383,4 +384,18 @@ describe("connection", function() {
});
});
});

describe("sync connection", () => {
it("works with synchronous connection", done => {
// @ts-ignore
Redis.Promise = Bluebird;
const redis = new Redis("/data");
redis.on("error", () => {
// @ts-ignore
Redis.Promise = Promise;
redis.disconnect();
done();
});
});
});
});

0 comments on commit 4bce38b

Please sign in to comment.