# Fix ClientClosedError: The client is closed in a Node.js Redis App

Yes. An agent reproduces the crash in its sandbox, finds the command sent before connect(), rewrites the callback calls for node-redis 4 and later and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-the-client-is-closed-error/node-redis/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads the Redis code first. In a small Express service like guillaumejacquart/node-docker-example, `src/index.js` creates the client with `redis.createClient({ host: config.get('redis.host') })`, attaches `client.on("error", ...)`, writes a key at startup with `client.set("foo", "bar", redis.print)`, and serves `GET /redis` by calling `client.get('foo', (err, reply) => ...)` and answering `{ key, value }`. `src/config.js` is a `convict` schema whose `redis.host` defaults to `redis` and reads `REDIS_HOST`. `package.json` has `redis` at `^2.8.0` beside `express`, `mysql` and `convict`, builds with Babel 6 (`babel -d ./build ./src -s`) and starts with `node ./build/index.js`; the `docker-compose.yml` and the README describe running it behind traefik with a `redis` service on the same network.

Then it reproduces both states. With the lock file's `redis` 2.8.0, the app starts, prints its `Example app listening` line, and the error handler logs `Redis connection to redis:6379 failed - getaddrinfo ENOTFOUND redis` when no Redis is reachable: the host from `config` is honoured and the process keeps serving. After `npm install redis@latest` (6.3.1 today) and the same build, the process prints the same listening line and exits with `ClientClosedError: The client is closed`, thrown from `client.set` through `@redis/client/dist/lib/client/index.js`. The reason is in the error, not the network: from version 4 the client sends nothing until `await client.connect()` resolves, and `set` returns a Promise that rejects, which nothing in this file catches.

The same probe shows two more things the upgrade changed. `redis.print` no longer exists (`typeof redis.print` is `undefined`), so the callback argument the code passes is silently treated as options, and `client.get('foo', cb)` returns a Promise whose rejection is unhandled rather than calling `cb`. And `createClient({ host })` ignores `host`: a client made that way tries the default address, which the agent sees as an `ECONNREFUSED` with no host in it, while `createClient({ socket: { host: 'redis' } })` reports `getaddrinfo ENOTFOUND redis` as the old client did.

The fix is small and is checked the same way. The agent moves the host under `socket`, calls `await client.connect()` before `app.listen`, so the service never accepts a request it cannot serve, rewrites the startup `set` and the route's `get` as `await client.set('foo', 'bar')` and `const value = await client.get('foo')` with the route's existing error response on a thrown error, and keeps the `error` listener, which the new client still requires so a dropped connection does not become an unhandled event. It does not reach for `legacyMode`, which 6.3.1 still accepts, because the file has two calls and both read better as promises. The pull request quotes the `ClientClosedError` stack from the old code and the clean startup of the new one against an unreachable host.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This Node.js service crashes at startup with \"ClientClosedError: The client is closed\" after the redis package was upgraded past version 3. Read package.json, the lock file and every file that calls redis.createClient or a redis client method first, then reproduce the crash by building and starting the app with no Redis reachable, and quote the stack. Fix it for node-redis 4 and later: put the host under socket, await client.connect() before the HTTP server listens, convert each callback-style command (including any use of redis.print) to await on the returned promise with the route's existing error handling, and keep the error listener. Do not use legacyMode unless the file has too many callback calls to convert in one change; say so if you do. Start the app once more against an unreachable host to show it no longer throws, run any tests, and open a pull request that quotes the before and after startup output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request touching `src/index.js` and nothing else in the app code: the client is created with `socket.host`, the server starts inside an async function after `await client.connect()`, and the two commands are awaited. The description quotes the `ClientClosedError` stack from the old build, the `typeof redis.print` and `createClient({ host })` findings that explain why the old calls fail silently, and the new startup output, where an unreachable host now shows as `getaddrinfo ENOTFOUND redis` from the error listener instead of a crash.

A reviewer checks that the listen call really waits for the connection, that the `/redis` route still answers `{ key, value }` and returns the same `500` body on an error, and that the MySQL part of the file was not touched. The run's record has every command the agent ran, so the before and after outputs in the description can be opened as the real terminal sessions.

## Run it on every Sentry issue alert

Sentry signs its own webhook requests and cannot add the `Authorization: Bearer` header a Rush `webhook` trigger requires, so a small handler on your server receives Sentry's alert and forwards it: it sends this body to `POST /o/{org}/sessions` with the issue title and the first stack frame appended to `input`, and a `request_id` that is a UUID v5 of the Sentry issue id, so the same issue re-firing returns the run it already started rather than opening a second pull request. The key's `webhook_url` receives `run.completed` or `run.failed` with the session URL when the run ends.

The same prompt works by hand: paste it into the Project's composer in the console when the alert lands, or send the body with `curl` from your terminal and follow the run on `GET /o/{org}/sessions/{id}/events`.

## Why run it on Rush?

This crash only shows against a real client library at a real version, and the diagnosis needs three facts that are easy to assume wrong: that `connect()` is now required, that `redis.print` is gone, and that `host` at the top level is ignored. The agent establishes each by running the code in its sandbox, with the old and the new package installed in turn, and every command and its output stays in the run's record, so the pull request argues from observed behaviour rather than from memory of a changelog.

It is also the kind of fix that should happen minutes after the alert, not whenever someone can pull the branch, install two versions of a package and reproduce a startup crash on a laptop that has other things running on port 3000. On Rush the alert starts one run in its own sandbox, the sandbox has nothing else on it, and what comes back is a pull request with the startup logs attached, ready to review.

## Frequently asked questions

### Why does the client start closed now?

node-redis 4 split creating a client from connecting it. `createClient` returns a client that sends nothing until `await client.connect()` resolves, and any command sent before that rejects with `ClientClosedError`. The agent moves the connect call before the server listens, so a request can never arrive while the client is still closed.

### Can I keep my callbacks with legacyMode?

Yes, and the current 6.x client still accepts the option; the agent checks that before deciding. With `legacyMode: true` a callback-style `set` works again, but only after `connect()`, so that call is still needed. For a file with a couple of commands the promise form is shorter and the agent prefers it; for a large codebase the prompt lets it choose `legacyMode` and say why.

### Will it change where the app connects?

No. The old code read the host from `config`, and the new client needs that value under `socket.host` or it silently ignores it and tries the default address. The agent keeps the same `config` key and shows, in the pull request, that an unreachable host is reported by name in the error listener after the change.

### What about the error listener, do I still need it?

Yes. The new client emits `error` on a dropped or refused connection, and an emitter with no `error` listener throws, which would turn a Redis outage into a process crash. The agent keeps the existing listener and leaves its logging as it was.

## More jobs like this: Fix a bug from an alert

- [Fix "DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead" in a Python Library](https://getrush.ai/run/fix-the-warn-method-is-deprecated-use-warning-instead/logging/)
- [Fix "AttributeError: 'Thread' object has no attribute 'isAlive'" on Python 3.9+](https://getrush.ai/run/fix-thread-isalive-attribute-error/threading/)
- [Fix "AttributeError: module 'time' has no attribute 'clock'" on Python 3.8+](https://getrush.ai/run/fix-time-clock-attribute-error/time/)
- [Fix "_pickle.UnpicklingError: Weights only load failed" from torch.load in PyTorch 2.6 and Later](https://getrush.ai/run/fix-torch-load-weights-only-unpickling-error/pytorch/)
- [Fix "AttributeError: module 'tornado.web' has no attribute 'asynchronous'" after a Tornado upgrade](https://getrush.ai/run/fix-tornado-web-asynchronous-attribute-error/tornado/)
- [Fix CryptographyDeprecationWarning: TripleDES Has Been Moved in a Paramiko Tool](https://getrush.ai/run/fix-tripledes-cryptography-deprecation-warning/paramiko/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
