redis/node-redis · error · Error

Socket already opened

Error message

Socket already opened

What it means

Thrown from RedisSocket.connect() (socket.ts:237) when #isOpen is already true. The socket guard prevents a second connect attempt over a live connection. It is a plain Error, thrown synchronously to the caller of client.connect().

Source

Thrown at packages/client/lib/client/socket.ts:237

      return cause;
    } else if (retryIn instanceof Error) {
      this.#isOpen = false;
      publish(CHANNELS.ERROR, () => ({
        error: cause,
        origin: 'client',
        internal: false,
        clientId: this.#clientId
      }));
      this.emit('error', cause);
      return new ReconnectStrategyError(retryIn, cause);
    }

    return retryIn;
  }

  async connect(): Promise<void> {
    if (this.#isOpen) {
      throw new Error('Socket already opened');
    }

    this.#isOpen = true;
    return this.#connect();
  }

  async #connect(): Promise<void> {
    let retries = 0;
    do {
      try {
        const connectStartTime = performance.now();
        const socket = this.#socket = await this.#createSocket();
        this.emit('connect');

        try {
          await this.#initiateWhileSocketAlive(socket);

          // Check if socket was closed/destroyed during initiator execution

View on GitHub (pinned to bb5beb5657)

Solutions

  1. Guard the call: check client.isOpen before calling connect(), or memoize the connect promise.
  2. Use createClient().connect() once at startup and reuse the instance.
  3. If reconnecting after close(), call close()/destroy() first, then connect().

Example fix

// before
await client.connect();
await client.connect(); // throws 'Socket already opened'

// after
if (!client.isOpen) await client.connect();
Defensive patterns

Strategy: validation

Validate before calling

if (client.isOpen) { /* already connected, skip */ } else { await client.connect(); }

Prevention

When it happens

Trigger: Calling client.connect() twice; awaiting connect() in a retry loop that already succeeded; connecting a client that the application auto-connected elsewhere.

Common situations: Double await on the same connect() promise race; reconnect logic that calls connect() without checking isOpen; framework lifecycle hooks firing twice.

Related errors


AI-assisted analysis of redis/node-redis@bb5beb5657 (2026-08-03). Data as JSON: /data/errors/f75c3381fd4193b8.json. Report an issue: GitHub.