-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
The log shows - "ClientClosedError: The client is closed" even having explicit connect & disconnect for each call #2607
Comments
Any update on this issue? Still happening...
|
You shouldn't Assuming you are using it with an HTTP server, if you Hope that makes sense.. |
As recommended by leibale, no need to disconnect for every http request, as the program normally is running all along. The reason I executed the "disconnect" previously for each request was trying to prevent the possible more and more open connections, based on the observation, this case wouldn't happen. |
actually that sounds like a great idea, but how I can do this? |
redis.js: import { createClient } from 'redis';
export const client = await createClient()
.on('error', err => console.error('Redis Client Error', err))
.connect(); index.js: import { createServer } from 'node:http';
import { client } from './redis.js';
createServer(async (req, res) => {
try {
res.end(await client.get('key'));
} catch (err) {
console.error(err);
res.end('Internal Server Error');
}
}).listen(3000); |
This is what I have done in my Next.js project, but for some reason, it still gives the same error "ClientClosedError: The client is closed" when fetch onto the server that runs redis commands. I have looked at all my files and there is no |
running into the same exact issue. This is how the client connection is made export async function getRedisCluster(hostEnvName: string, portEnvName: string): Promise<RedisCluster> {
const args = getRedisArgs(hostEnvName, portEnvName);
logger.info(`connecting to Redis cluster at: ${args.redisHost}:${args.redisPort}...`);
const redisCluster = redis.createCluster({
rootNodes: [
{
url: `redis://${args.redisHost}:${+args.redisPort}`,
},
],
// minimizeConnections: true,
});
redisCluster.on('error', (err) => console.log('Redis Cluster Error', err));
await redisCluster.connect();
return redisCluster;
}
export const subscriptionClient = await getRedisCluster('REDIS_SUBSCRIPTION_HOST', 'REDIS_SUBSCRIPTION_PORT'); This is an example of how the unsubscribe happens. export async function maybeRemoveGlobalSubscription(channel: string) {
liveWebSockets.forEach((wsc) => {
if (wsc.subscribedChannels?.includes(channel)) {
return; // early return if any other client is still subscribed
}
});
if (globalSubscriptions.has(channel)) {
globalSubscriptions.delete(channel);
await subscriptionClient.sUnsubscribe(channel);
}
} And this is the error I get Error: The client is closed
at RedisSocket.disconnect (/app/node_modules/@redis/client/dist/lib/client/socket.js:63:19)
at Commander.disconnect (/app/node_modules/@redis/client/dist/lib/client/index.js:343:64)
at RedisClusterSlots.executeShardedUnsubscribeCommand (/app/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:154:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async maybeRemoveGlobalSubscription (file:///app/build/routes/subscribe/relayer.js:16:9)
at async file:///app/build/routes/subscribe/relayer.js:5:9 [/app/build/utils/errors.js]
ErrorMessage: The client is closed
StackTrace: Error: The client is closed
at RedisSocket.disconnect (/app/node_modules/@redis/client/dist/lib/client/socket.js:63:19)
at Commander.disconnect (/app/node_modules/@redis/client/dist/lib/client/index.js:343:64)
at RedisClusterSlots.executeShardedUnsubscribeCommand (/app/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:154:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async maybeRemoveGlobalSubscription (file:///app/build/routes/subscribe/relayer.js:16:9)
at async file:///app/build/routes/subscribe/relayer.js:5:9 |
I encountered an issue with the redis library when using version 4, but switching to version 3.1 resolved the problem. It seems there might be compatibility or changes in behavior between versions 4 and 3.1 that affected my application. I wanted to share this information in case others face a similar issue and might find it helpful to try using version 3.1 instead |
This is what I've done on my Node/Express project: redis.js: import { createClient } from 'redis';
export const client = createClient({
url: process.env.REDIS_URL,
})
if (!client.isOpen) {
await client.connect()
console.log('Connected to Redis')
} index.js: import 'dotenv/config'
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import { router } from './api.js'
const corsConfig = {
origin: process.env.CORS_ORIGIN
}
const app = express()
const PORT = process.env.PORT
app.use(cors(corsConfig))
app.use(bodyParser.json())
app.use('/api', router)
app.listen(PORT, () => console.log(`Listening to port ${PORT}`)) In api.js, I'm simply importing functions from my actions directory such as in my case, updating a note: updateNote.js: import { client } from "../redis.js"
import { noteSchema } from "../schemas/index.js"
import { Repository } from "redis-om"
export async function updateNote(note) {
const repository = new Repository(noteSchema, client)
const result = await repository.fetch(note.noteId)
result.title = note.title
result.body = note.body
result.category = note.category
result.modified = note.modified
await repository.save(result)
} When running |
please use this const redis = require('redis'); client.connect(); |
Description
Sometime the node.js application would throw out the error message - "ClientClosedError: The client is closed" even having explicit connect & disconnect for each call in the code. As redis is used frequenty in the program, the redis is initiated when starting the program(with built-in createClient() method). In my scenario of every express call(get or post), the redis connect will be called if "redisClient.isOpen" is false, after the redis access, the redis disconnect will be excuted explicitly. Don't know why this error was caused.
By the way, if the redis diconnect method is unnecessary(as the front-end users will access the program from time to time, so logically the redis would disconnect and connect alternatively all along)?
Appreciate for the kind assistance.
Node.js Version
v18.14.2
Redis Server Version
7.2.0
Node Redis Version
redis@4.6.7
Platform
Ubuntu 20.04(Linux5.4.0-148-genericx86_64)
Logs
The text was updated successfully, but these errors were encountered: