Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(storage): bound S3 requests with connect and idle timeouts

Yudhi Armyndharisf8944bdfe309
diff --git a/src/common/storage/storage.service.s3.spec.ts b/src/common/storage/storage.service.s3.spec.ts
index 04603c355..92ed17f20 100644
--- a/src/common/storage/storage.service.s3.spec.ts
+++ b/src/common/storage/storage.service.s3.spec.ts
@@ -26,7 +26,7 @@ jest.mock('@aws-sdk/client-s3', () => {
import { S3Client } from '@aws-sdk/client-s3';
import { LoggerService } from '../services/logger.service';
-import { StorageService } from './storage.service';
+import { S3_CONNECT_TIMEOUT_MS, S3_SOCKET_TIMEOUT_MS, StorageService } from './storage.service';
// The subset of the AWS SDK's S3Client config this service sets — used purely to type the mock's call
// args so the assertions below are type-checked instead of `any`.
@@ -35,6 +35,7 @@ type S3ClientConfig = {
region: string;
forcePathStyle?: boolean;
credentials: { accessKeyId: string; secretAccessKey: string };
+ requestHandler?: { connectionTimeout?: number; socketTimeout?: number; requestTimeout?: number };
};
const mockedS3Client = S3Client as unknown as jest.Mock<unknown, [S3ClientConfig]>;
@@ -118,6 +119,18 @@ describe('StorageService (s3) client init', () => {
expect(svc.isS3Available()).toBe(true);
});
+ // A store that accepts the connection and never answers would otherwise leave every request (the
+ // availability probe, media reads and archive writes) pending forever.
+ it('bounds every request with a connect and an idle-socket timeout', async () => {
+ new StorageService(makeConfig({ accessKeyId: 'minio', secretAccessKey: 'minio123', region: 'us-east-1' }));
+ await flush();
+
+ expect(lastConfig().requestHandler).toEqual({
+ connectionTimeout: S3_CONNECT_TIMEOUT_MS,
+ socketTimeout: S3_SOCKET_TIMEOUT_MS,
+ });
+ });
+
it('falls back to local (no client) when credentials are missing, and says so', async () => {
// Without a client none of the S3 logging further down ever runs, so this fallback used to be
// the one that produced no output at all: the bucket simply stayed empty. Name the two vars
diff --git a/src/common/storage/storage.service.ts b/src/common/storage/storage.service.ts
index 059d6e868..28a4f8f75 100644
--- a/src/common/storage/storage.service.ts
+++ b/src/common/storage/storage.service.ts
@@ -46,6 +46,16 @@ export const DEFAULT_S3_REPROBE_INTERVAL_MS = 60_000;
*/
export const S3_DELETE_TIMEOUT_MS = 30_000;
+/**
+ * Connect and idle-socket bounds for every S3 request. Without them a store that accepts the
+ * connection and never answers (a paused container, a stuck proxy) leaves the request pending
+ * forever, holding its media buffer and the caller. The socket bound is an idle timeout, so a long
+ * upload or download that keeps moving bytes is not cut off. A bare requestTimeout would not do:
+ * the HTTP handler only logs a warning when it expires.
+ */
+export const S3_CONNECT_TIMEOUT_MS = 5_000;
+export const S3_SOCKET_TIMEOUT_MS = 30_000;
+
function positiveIntFromEnv(name: string, fallback: number): number {
const parsed = Number.parseInt(process.env[name] ?? '', 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
@@ -91,6 +101,7 @@ export class StorageService implements OnModuleDestroy {
secretAccessKey,
},
...(endpoint ? { forcePathStyle: true } : {}), // Required for path-style stores (MinIO)
+ requestHandler: { connectionTimeout: S3_CONNECT_TIMEOUT_MS, socketTimeout: S3_SOCKET_TIMEOUT_MS },
});
this.s3Bucket = process.env.S3_BUCKET || s3Config.bucket || 'openwa';
const keyRoot = normalizeS3KeyPrefix(process.env.S3_KEY_PREFIX || s3Config.keyPrefix);