redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.
MIT License
14.31k stars 1.19k forks source link

Getting frequesnt connection closed errors #1848

Open geo-github opened 8 months ago

geo-github commented 8 months ago

I have implemented Pub/Sub pattern using ioredis. For past few days i get below error frequently and pod where the app is deployed frequently restarts . Wanted to know if node app restarts due to this error?Or this error is due to node app crashing

/usr/src/app/node_modules/ioredis/built/redis/event_handler.js:189

self.flushQueue(new Error(utils_1.CONNECTION_CLOSED_ERROR_MSG)) ^ Error: Connection is closed. at close (/usr/src/app/node_modules/ioredis/built/redis/event_handler.js:189:25) at TLSSocket. (/usr/src/app/node_modules/ioredis/built/redis/event_handler.js:156:20) at Object.onceWrapper (node:events:628:26) at TLSSocket.emit (node:events:525:35) at node:net:301:12 at TCP.done (node:_tls_wrap:588:7) at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

Sample code below: async subscribeToChannel(channelName) { this.logger.debug('cannot aquire lock - redis is locked'); const redisSub = this.getRedisInstance(); //Subscribe to channel redisSub.subscribe(channelName, (err, data) => { this.logger.debug('client subscribed'); }); return await new Promise((resolve, reject) => {//Wait for message event redisSub.on('message', (channel, message) => { this.logger.debug(Received the following message from ${channel}); resolve(JSON.parse(message));//Return data redisSub.unsubscribe();//Unsubscribe redisSub.quit();//Close connection }); }) }

publishToChannel(channelName, data) {
    const pub = this.getRedisInstance();
    pub.publish(channelName, JSON.stringify(data));//Publish data
    pub.quit();//Close connection
}
roggervalf commented 8 months ago

I'm facing same issue too when using quit method, looks like it's coming from this line

roggervalf commented 8 months ago

found the issue in my side, there was a pending command that was not resolved so when closing, it throws that error