-
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
SocketClosedUnexpectedlyError: Socket closed unexpectedly #2032
Comments
After disconnect/reconnect it stops receiving messages. With the older version of node-redis it was reconnecting without any problems. I added some code to print the parser error. Code:
Output:
|
We are currently stuck with v3 for months now because this issue seems to be still unresolved with v4. |
@rpong Could you please update the tentative ETA for this issue as it is a blocker for v4? |
Hi @erajanraja24, @leibale would be the best person to ask, I am not part of the team.. |
Hey guys @leibale. Any updates on this fix for this issue? |
This code does not reproduce the issue (I'm running import { createClient } from '@node-redis/client';
async function client() {
const client = createClient();
client.on('error', err => console.error('client error', err));
client.on('connect', () => console.log('client is connect'));
client.on('reconnecting', () => console.log('client is reconnecting'));
client.on('ready', () => console.log('client is ready'));
await client.connect();
return client;
}
const [subscriber, publisher] = await Promise.all([
client(),
client()
]);
let counter = 0;
subscriber.pSubscribe('*', () => counter++);
setInterval(() => console.log(`Got ${counter} messages!`), 1000);
setInterval(async () => {
try {
await publisher.publish('channel', 'message');
} catch (err) {
console.error('publish error', err);
}
}, 5);
|
@leibale i just tested 4.1.0 and it seems this issue still persists, reconnecting still doesn't work like it used to like in v3. after "Socket closed unexpectedly", it just doesn't reconnect without restarting the client application. We simulate this by restarting redis (docker) while the application is running. |
Thanks @leibale , i have discovered that this IS working if we do following before the "await client.connect();"
but without the above, it doesn't reconnect like v3 does. We can upgrade now and just pass debug info for the above, but would like to confirm if this is the intended behavior. |
|
In our case, we did not have this at all for v3 but reconnecting was working. Thank you for the info, we can finally upgrade to v4 after many months. |
I can confirm that the code above that @leibale provided fixes the issue! My issue in particular was that I had deployed my app using Redis to Heroku and upon the socket closing unexpectedly, the Heroku-deployed app would crash until I redeployed it. The problem was driving me nuts and this was the only solution on the web that fixed it. Thank you! |
@leibale , The issue persists in v4 and yes it does reconnect. but is there any particular reason why is it crashing unexpectedly? |
Just had this occur for us with 4.1.0 running... Code:
Our logging:
The ready event didn't ever happen. When I restarted node it did connect/ready. Any other ideas/tips? Something I'm missing? This is for a socket.io adaptor so the redis connection going down really messes with the rest of our app. |
I should add I was getting the ready event in dev testing if I just stopped/started redis. It seems to still be a problem for whatever is happening when the socket closes unexpectedly. (In my case via an AWS Elasticache. |
I believe I'm experiencing this issue on 4.2.0. I set my event listeners like this: client.on('error', err => log.error(`Redis error: ${err}`));
client.on('reconnecting', params => log.info(`Redis reconnecting, attempt ${params.attempt}`));
client.on('connect', () => log.info('Redis connected'));
client.on('ready', () => log.info('Redis ready'));
client.on('end', () => log.info('Redis connection closed')); When it starts up, I see this in the log:
After a while, it disconnects. I'm using Heroku's Redis server, which seems to drop connections after a few minutes, I think in an attempt to clean up stale connections. Before upgrading from 3.x.x, it would reconnect seamlessly. Now, all I see is this:
No reconnect, no ready. I have to restart my Node process to get it working again. Can you offer any advice? |
I think I have discovered the cause of my problem. I was expecting a |
@ryanmeador can you please open a new issue with reproduction? |
Hi, I got this same issue on heroku and I changed to the latest version(4.2.0), and still got the same issue after some minutes. This is my code.
Is there a solution for this? Or maybe I rewrite the code? |
@CaCaBlocker the only known bug that can cause this issue is the one @ryanmeador pointed out in this comment, and I'm already working to fix it (see this branch). Other than that, this error could be caused by some kind of a proxy or a load balancer between the redis-server and the node server. Without a reproduction I won't be able to debug and hopefully solve it. |
same, using redis db on digitalocean |
@fabioselau077 @jorenvandeweyer https://www.digitalocean.com/community/questions/idle-redis-connection-reconnects-every-five-minutes |
@fabioselau077 |
If certain event handlers aren't defined on the client before connecting, the app will crash on disconnect event (e.g., timeout). Issue comment regarding issue details from node-redis repo: redis/node-redis#2032 (comment) Fixes #129, #160
@CaCaBlocker I having the same issue from Redis hosted in Heroku. Did you get any solution or workaround for this? |
@HenonoaH try to add listeners to all available events like ready reconnect error etc. |
@polearnik I have already added all the event listeners. I like to know if that exception is standard. I have configured Rollbar for my app and getting an exception log every five minutes!! This happens after the upgrade. |
If certain event handlers aren't defined on the client before connecting, the app will crash on disconnect event (e.g., timeout). Issue comment regarding issue details from node-redis repo: redis/node-redis#2032 (comment) Fixes #129, #160
Changing to redis-server version 6 on Ubuntu and Nodejs Redis 4.6.6 it works fine. import { Module } from '@nestjs/common'; @module({
], |
Hi @leibale, I'm using ioredis and i don't see pingInterval option. How can i solve it with ioredis? Thanks. |
@hieuhuynhh a simple const timer = setInterval(async () => {
try {
await client.ping();
} catch (err) {
console.error('Ping Interval Error', err);
}
}, 1000 * 60 * 4);
// make sure to `clearInterval` when you close the client
clearInterval(timer);
client.disconnect(); |
Although documentation mentions setting handler for error event, it is not clear that this is actually needed to have this issue fixed. There weren’t any errors of this kind in v3. Is there a reason why this happens in v4? Can we somehow check for that error inside error handler besides it’s message content? There isn’t any error code for that, and This could also be related to comment in #1993 (comment). |
@niksy https://github.com/redis/node-redis#events ":warning: You MUST listen to error events. If a client doesn't have at least one error listener registered and an error occurs, that error will be thrown and the Node.js process will exit. See the EventEmitter docs for more details." |
Yeah, I understand that, sorry I missed that part. It makes sense that this is needed, but it doesn’t answer the question why this type of error doesn’t happen on v3. I will see if it’s related to pinging you mentioned. |
changing from socket connection to url solved the issue on local machine connecting to docker container
|
not sure if related but I'm getting similar errors. All event listeners are configured. Application is a nestjs server dockerized on node:18-alpine, talking to memorystore redis in GCP (idle connections are not automatically closed). client initially connects -> socket gets closed -> client tries reconnecting -> uncaught exception occurs for socket closing
|
@djdabs try this: #2443 (comment) |
Still getting a triggerUncaugthException after client connects -> attempts to reconnect, but I'm now getting a different error. fwiw, i don't think i need a pingInterval with GCP. Their docs say by default no idle timeout is configured. https://cloud.google.com/memorystore/docs/redis/supported-redis-configurations#modifiable_configuration_parameters
|
resolved my issue.. after switching to ioredis the error message got more specific and I was able to track down the underlying problem (redis required TLS). Disabled TLS on memorystore and it was able to work now.
|
Are there any updates on this issue? I'm currently facing the same problem while using a Redis instance served by Digital Ocean. TLS is in place and the job is listening on "error" - each time the issue occurs, I reset the connection. I don't know if it is the best solution but it kind of works even if it throws the error every five minutes. In the previous comments, there is a suggestion that pings the server. Do you think that is the best way to handle it at the moment? Or should we wait the patch? |
@stefanodecillis You can just use |
Getting the same error at v4.6.12 client.on('error', (err) => { client.on('reconnecting', () => console.log('client is reconnecting')); module.exports = client;` This is the error im gettting
It runs well on local but not when deployed on vercel |
We are experiencing the same issue. We're listening error events explicitly...our connection logic is below @leibale :
|
The error message indicates that Redis is running in protected mode due to enabled protected mode and the absence of a password for the default user. In this mode, connections are only accepted from the loopback interface. To enable connections from external computers, you can choose one of the following solutions: Disable protected mode by executing the command: 'CONFIG SET protected-mode no'. |
Good day!
The socket connection closes unexpectedly and doesn't reconnect, my older version of node-redis(2.8.0) was reconnecting fine and still does.
After reconnect it doesn't print ready anymore.
We are using AWS ElastiCache.
I can only reproduce this on production env.
We have a package that wraps node-redis and is used in all our apps, so I don't think is related to version difference.
I tried to comment out next code and it doesn't close the connection at all (from @socket.io/redis-adapter).
Here is where I catch the parser error:
client/socket.ts
List with buffers and offsets:
Code for creating the client
Environment:
The text was updated successfully, but these errors were encountered: