-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Labels
Description
Description
Exactly 1 minute after a connection has successfully been established to Redis, the client is failing with a "Socket Closed Unexpectedly" error. Following other issues raised on this, have tried changing the tcp-keepalive and timeout values in Redis to match with the defaults for this library (5000ms), with no improvement. This has only started happening following a recent update to Redis 5.1.0 from the previous version of 3.1.2. We are also using RedisStore from the connect-redis library alongside this one
Minimal code example:
import session from 'express-session';
import { RedisStore } from 'connect-redis';
import { createClient } from 'redis';
import { logger } from './logger';
import { getConfig } from './config/ValidateConfig';
import { BaseHttpsConfig, RedisConfig } from './config';
export const configureSession = (): session.SessionOptions => {
let isConnected = false;
const redisConfig = getConfig(RedisConfig);
const httpsConfig = getConfig(BaseHttpsConfig);
const redisClient = createClient({
url: `redis://${redisConfig.REDIS_USERNAME}:${redisConfig.REDIS_PASSWORD}@${redisConfig.REDIS_HOSTNAME}:${redisConfig.REDIS_PORT}`,
});
redisClient.on('connect', () => {
// This ensure that the log message is only called once
// Connect event can happen many times due to timeouts on the redis instance being configured to a small value
if (!isConnected) {
logger.info(
`Successfully connected to Redis on host: ${redisConfig.REDIS_HOSTNAME}:${redisConfig.REDIS_PORT}`
);
}
isConnected = true;
});
redisClient.on('reconnecting', () => {
logger.warn('Lost connection Redis, attempting reconnect...');
if (isConnected) {
isConnected = false;
}
});
redisClient.on('error', (err) => {
logger.error(`Error on Redis Connection: ${err.message}`);
logger.error('Process will now exit.');
throw err;
});
redisClient.on('end', () => {
logger.warn('Redis Client Connection terminated');
if (isConnected) {
isConnected = false;
}
});
redisClient.connect();
return {
store: new RedisStore({ client: redisClient }),
name: redisConfig.REDIS_COOKIE_NAME,
secret: redisConfig.REDIS_SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: httpsConfig.HTTPS_ENABLED,
httpOnly: true,
maxAge: redisConfig.REDIS_SESSION_EXPIRY_MILLIS,
},
};
};Node.js Version
20.10.0
Redis Server Version
6.2.1
Node Redis Version
5.1.0
Platform
Linux
Logs
13:58:50 info: Successfully connected to Redis on host: [redacted]
...
13:59:50 error: Error on Redis Connection: Socket closed unexpectedly
13:59:50 error: Process will now exit
SocketClosedUnexpectedlyError: Socket closed unexpectedly
at Socket.<anonymous> (/node_module/@redis/client/dist/lib/client/socket.js:232:33)
at Object.onceWrapper (node:events:639:26)
at Socket.emit (node:events:524:28)
at TCP.<anonymous> (node:net:343:12)Reactions are currently unavailable