Skip to content
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

Unhandled error event: Error: read ECONNRESET at TCP.onStreamRead #1203

Closed
sysopsnw18 opened this issue Oct 8, 2020 · 31 comments
Closed

Unhandled error event: Error: read ECONNRESET at TCP.onStreamRead #1203

sysopsnw18 opened this issue Oct 8, 2020 · 31 comments

Comments

@sysopsnw18
Copy link

Hi,
I am getting below error sometimes. Redis connection gets disconnected sometime. Out of 10 I am facing this issue 2 time. It takes time to reconnect the server and api response time increases from 50-80 msec to 1-2 mins.

Error : Unhandled error event: Error: read ECONNRESET at TCP.onStreamRead

Ioredis client configuration as below:
var redisMasterClient = new IORedis({
host: host ,
connectTimeout: 1000,
password: "password,
keepAlive : 1000,
retryStrategy: function(times) {
var delay = Math.min(times * 10, 2000);
return delay;
},
maxRetriesPerRequest: 1
});

Please help. This is urgent issue on production.

@mrosendin
Copy link

I am also having this issue

@wshino
Copy link

wshino commented Nov 30, 2020

Me too. I'll watch this issue.

@slaveofcode
Copy link

Got the same problem

@ctrlaltdylan
Copy link

same issue, tried reseting the worker, deleting and recreating the redis instance but no luck

@ctrlaltdylan
Copy link

Turns out Heroku's Redis v6 instances are self-signing certificates. Passing ignoreUnauthorized to the tls options fixed the issue for me. Filing a bug report with Heroku too.

@kapalkat
Copy link

Hey, I have the same. Seems like following PR might resolve that: #1236.
The question is: when we can expect a merge?

@luin
Copy link
Collaborator

luin commented Mar 18, 2021

Sorry for the late response. This issue should be fixed in the latest version published in the last week. Closing this. Feel free to reopen if there are any issues.

@luin luin closed this as completed Mar 18, 2021
@burnthoney
Copy link

Im still getting the problem and im on the latest version.

@burnthoney
Copy link

image

@yovanoc
Copy link

yovanoc commented Jul 11, 2021

Getting the problem too

@astrolemonade
Copy link

I am getting this too

@andredezzy
Copy link

Any news for this issue, I am having the same problem.

@flyingcrp
Copy link

im getting this too
has any progress for this issue?

@luin
Copy link
Collaborator

luin commented Sep 2, 2021

Network issues can be the cause of ECONNRESET errors. It's in all probability not related to ioredis and the Redis server itself. I'd check the network first by using telnet or something similar in the same network condition of the client to see whether it works.

@flyingcrp
Copy link

flyingcrp commented Sep 6, 2021

i guess,ioredis's connection was broken
because i add a setInterval task that send the ping to the redis server at every 15s,now its work fine.

Network issues can be the cause of ECONNRESET errors. It's in all probability not related to ioredis and the Redis server itself. I'd check the network first by using telnet or something similar in the same network condition of the client to see whether it works.

@HowellRen
Copy link

i am getting this too,
at 6am and 6:15am every day.
微信截图_20211012115011

@doptster
Copy link

I am getting the same error, any workaround for this?

@doptster
Copy link

I resolved my issue by using the following option

const client = new Redis(process.env.REDIS_URL, {
  tls: {
    rejectUnauthorized: false
  }
});

Can try if it helps at your side

@ntedgi
Copy link

ntedgi commented Dec 7, 2021

hi @dopt do you use AWS elasticCache or self hosted redis instance?
did the problem eventually solved

@flyingcrp
Copy link

I resolved my issue by using the following option

const client = new Redis(process.env.REDIS_URL, {
  tls: {
    rejectUnauthorized: false
  }
});

Can try if it helps at your side

i dont understand whats the relationship with this attribute.
maybe use setInterval send ping to redisserver is temp solution.

finding the root cause and fixing it is the best solution

@jayantasamaddar
Copy link

jayantasamaddar commented Sep 19, 2022

The right way to do this is to handle the error gracefully. The problem seems to be the IORedis client's behaviour of timed disconnect when inactive but it has an auto reconnect mechanism that will keep trying to reconnect.

Use Node.js Event Emitter to handle it. View all events with client.eventNames() or see the references below. One can simply log out the error, reconnecting and connect events like this to understand the behaviour described above that IORedis is doing in the background. Consequently, this also handles the irritating error message. Hope this helps!

import Redis from 'ioredis';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../.env') });

const client = new Redis({
  port: Number(process.env.SESSION_STORE_PORT) || 6379,
  host: process.env.SESSION_STORE_HOSTNAME,
});

// Listen to 'error' events to the Redis connection
client.on('error', error => {
  if (error.code === 'ECONNRESET') {
    console.log('Connection to Redis Session Store timed out.');
  } else if (error.code === 'ECONNREFUSED') {
    console.log('Connection to Redis Session Store refused!');
  } else console.log(error);
});

// Listen to 'reconnecting' event to Redis
client.on('reconnecting', err => {
  if (client.status === 'reconnecting')
    console.log('Reconnecting to Redis Session Store...');
  else console.log('Error reconnecting to Redis Session Store.');
});

// Listen to the 'connect' event to Redis
client.on('connect', err => {
  if (!err) console.log('Connected to Redis Session Store!');
});

export default client;

References:

@Felipeex
Copy link

Felipeex commented Sep 4, 2023

Hello, you don't instance radis, when your application starts. you'll instance when you use the radis.

Exemple here:

try {
 await redis.set("test", "test")
} catch (err) {
 console.log("error", err)
} finally {
 redis.quit() // stop connection here, and init other when you need
}

@jayantasamaddar
Copy link

jayantasamaddar commented Sep 4, 2023

Hello, you don't instance radis, when your application starts. you'll instance when you use the radis.

Exemple here:

try {
 await redis.set("test", "test")
} catch (err) {
 console.log("error", err)
} finally {
 redis.quit() // stop connection here, and init other when you need
}

This is okay only when your application is having low throughput into redis. For a medium to higher throughput application, why would you want to open and close connection for every R/W operation and increase your latency per operation? In most cases, you want to keep it open and gracefully handle the error then, unless there's a security reason why you should not or if it's infrequent use.

@ShivaBhattacharjee
Copy link

Getting the same error

Screenshot from 2024-02-24 13-04-40

@PuffMeow
Copy link

There is still this error in 2024...

@thepranaygupta
Copy link

same issue, tried reseting the worker, deleting and recreating the redis instance but no luck

@babeingineer
Copy link

Untitled
Tried all above solutions but not working....

@jayantasamaddar
Copy link

The right way to do this is to handle the error gracefully. The problem seems to be the IORedis client's behaviour of timed disconnect when inactive but it has an auto reconnect mechanism that will keep trying to reconnect.

Use Node.js Event Emitter to handle it. View all events with client.eventNames() or see the references below. One can simply log out the error, reconnecting and connect events like this to understand the behaviour described above that IORedis is doing in the background. Consequently, this also handles the irritating error message. Hope this helps!

import Redis from 'ioredis';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../.env') });

const client = new Redis({
  port: Number(process.env.SESSION_STORE_PORT) || 6379,
  host: process.env.SESSION_STORE_HOSTNAME,
});

// Listen to 'error' events to the Redis connection
client.on('error', error => {
  if (error.code === 'ECONNRESET') {
    console.log('Connection to Redis Session Store timed out.');
  } else if (error.code === 'ECONNREFUSED') {
    console.log('Connection to Redis Session Store refused!');
  } else console.log(error);
});

// Listen to 'reconnecting' event to Redis
client.on('reconnecting', err => {
  if (client.status === 'reconnecting')
    console.log('Reconnecting to Redis Session Store...');
  else console.log('Error reconnecting to Redis Session Store.');
});

// Listen to the 'connect' event to Redis
client.on('connect', err => {
  if (!err) console.log('Connected to Redis Session Store!');
});

export default client;

References:

@babeingineer try this

@OmGarg8700
Copy link

In my case : it is not working : in development mode (http)

so using this helped,
const client = new Redis(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
}
});

but in production : remove this

@abdulsamadayoade
Copy link

None of the solutions is working for me

@KingPuiWong
Copy link

None of the solutions is working for me

me too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests