Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 206022e08a fix: handle Redis session storage connection failure gracefully with retry logic
https://sonarly.com/issue/19636?type=bug

The session storage Redis client throws inside a `.catch()` callback on connection timeout, creating an unhandled promise rejection that crashes the Node.js process.

Fix: **Three changes in `session-storage.module-factory.ts`:**

1. **Added `reconnectStrategy` to the Redis client config** — on connection failure, the client will retry with exponential backoff (500ms, 1000ms, ... capped at 5000ms) instead of giving up immediately. This matches the resilience behavior of the `ioredis` client used elsewhere in the codebase (`RedisClientService` with `maxRetriesPerRequest: null`).

2. **Replaced `throw new Error(...)` inside `.catch()` with `logger.error()`** — the original pattern created an unhandled promise rejection (throwing inside `.catch()` produces a new rejected promise with no handler), which crashed the Node.js process. Now the error is logged properly without crashing the server.

3. **Added `redisClient.on('error', ...)` handler** — the node-redis v4 client emits `error` events on connection issues. Without a handler, these become unhandled error events. The handler logs errors at the appropriate level using NestJS Logger.

These changes allow the server to survive transient Redis connectivity issues during startup (e.g., pod restarts where Redis isn't immediately available) while providing clear log output for debugging.
2026-03-30 14:29:09 +00:00
@@ -1,5 +1,7 @@
import { createHash } from 'crypto';
import { Logger } from '@nestjs/common';
import RedisStore from 'connect-redis';
import { createClient } from 'redis';
@@ -8,6 +10,8 @@ import type session from 'express-session';
import { CacheStorageType } from 'src/engine/core-modules/cache-storage/types/cache-storage-type.enum';
import { type TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
const logger = new Logger('SessionStorage');
export const getSessionStorageOptions = (
twentyConfigService: TwentyConfigService,
): session.SessionOptions => {
@@ -57,10 +61,27 @@ export const getSessionStorageOptions = (
const redisClient = createClient({
url: connectionString,
socket: {
reconnectStrategy: (retries: number) => {
const delay = Math.min(retries * 500, 5000);
logger.warn(
`Redis session storage reconnecting (attempt ${retries}, next retry in ${delay}ms)`,
);
return delay;
},
},
});
redisClient.on('error', (err) => {
logger.error(`Redis session storage error: ${err.message}`);
});
redisClient.connect().catch((err) => {
throw new Error(`Redis connection failed: ${err}`);
logger.error(
`Redis session storage initial connection failed: ${err.message}`,
);
});
return {