Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(storage): give up on an S3 bucket probe after 10 seconds
Yudhi Armyndharis1a56a9c411d2
diff --git a/src/common/storage/storage.service.s3-reprobe.spec.ts b/src/common/storage/storage.service.s3-reprobe.spec.tsindex 645702216..504f876da 100644--- a/src/common/storage/storage.service.s3-reprobe.spec.ts+++ b/src/common/storage/storage.service.s3-reprobe.spec.ts@@ -32,7 +32,12 @@ import {HeadBucketCommand,ListObjectsV2Command,} from '@aws-sdk/client-s3';-import { DEFAULT_S3_REPROBE_INTERVAL_MS, S3_DELETE_TIMEOUT_MS, StorageService } from './storage.service';+import {+ DEFAULT_S3_REPROBE_INTERVAL_MS,+ S3_DELETE_TIMEOUT_MS,+ S3_PROBE_TIMEOUT_MS,+ StorageService,+} from './storage.service';const ENV_KEYS = ['S3_ENDPOINT',@@ -392,6 +397,49 @@ describe('StorageService S3 re-probe and recovery', () => {}});+ it('settles a bucket probe that never answers, so a later re-probe can still recover', async () => {+ // A probe that never settled stayed parked in the in-flight slot: every later re-probe and the+ // status and migration endpoints awaited it, and S3 never came back without a restart.+ let stalled: 'head' | 'create' | 'none' = 'head';+ mockSend.mockImplementation((cmd: unknown, options?: { abortSignal?: AbortSignal }) => {+ if (stalled === 'create' && cmd instanceof HeadBucketCommand) return Promise.reject(s3Error('NotFound'));+ const stalls =+ (stalled === 'head' && cmd instanceof HeadBucketCommand) ||+ (stalled === 'create' && cmd instanceof CreateBucketCommand);+ if (!stalls) return Promise.resolve({});+ return new Promise((_resolve, reject) => {+ options?.abortSignal?.addEventListener('abort', () => reject(s3Error('AbortError')));+ });+ });+ // AbortSignal.timeout runs on Node's internal timers, which fake timers do not reach.+ const timeout = jest.spyOn(AbortSignal, 'timeout').mockImplementation((ms: number) => {+ const controller = new AbortController();+ setTimeout(() => controller.abort(), ms);+ return controller.signal;+ });+ const settleWithin = async (probe: Promise<boolean>): Promise<boolean | 'pending'> => {+ await jest.advanceTimersByTimeAsync(S3_PROBE_TIMEOUT_MS);+ return Promise.race([probe, Promise.resolve('pending' as const)]);+ };+ try {+ const svc = new StorageService(makeConfig());+ warnSpyOf(svc);+ await flush();+ await jest.advanceTimersByTimeAsync(S3_PROBE_TIMEOUT_MS); // the boot probe gives up too+ expect(svc.isS3Available()).toBe(false);++ await expect(settleWithin(svc.refreshS3Availability())).resolves.toBe(false);++ stalled = 'create';+ await expect(settleWithin(svc.refreshS3Availability())).resolves.toBe(false);++ stalled = 'none';+ await expect(settleWithin(svc.refreshS3Availability())).resolves.toBe(true);+ } finally {+ timeout.mockRestore();+ }+ });+it('listFiles unions S3 objects with the local fallback dir (each key once)', async () => {mockSend.mockImplementation((cmd: unknown) => {if (cmd instanceof ListObjectsV2Command) {diff --git a/src/common/storage/storage.service.ts b/src/common/storage/storage.service.tsindex 28a4f8f75..9fe07348d 100644--- a/src/common/storage/storage.service.ts+++ b/src/common/storage/storage.service.ts@@ -46,6 +46,13 @@ export const DEFAULT_S3_REPROBE_INTERVAL_MS = 60_000;*/export const S3_DELETE_TIMEOUT_MS = 30_000;+/**+ * Cap on one bucket probe request. The status endpoint and the storage migration routes await the+ * probe, and every later re-probe waits on one already in flight, so it gets a tighter bound than+ * the idle-socket timeout below, and one that also stops the SDK's retries.+ */+export const S3_PROBE_TIMEOUT_MS = 10_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@@ -162,14 +169,18 @@ export class StorageService implements OnModuleDestroy {*/private async ensureS3Bucket(): Promise<void> {try {- await this.s3Client!.send(new HeadBucketCommand({ Bucket: this.s3Bucket }));+ await this.s3Client!.send(new HeadBucketCommand({ Bucket: this.s3Bucket }), {+ abortSignal: AbortSignal.timeout(S3_PROBE_TIMEOUT_MS),+ });return;} catch (error: unknown) {const name = (error as { name?: string }).name;if (name !== 'NotFound' && name !== 'NoSuchBucket') throw error;}try {- await this.s3Client!.send(new CreateBucketCommand({ Bucket: this.s3Bucket }));+ await this.s3Client!.send(new CreateBucketCommand({ Bucket: this.s3Bucket }), {+ abortSignal: AbortSignal.timeout(S3_PROBE_TIMEOUT_MS),+ });} catch (error: unknown) {// Another replica (or an overlapping probe) created it first, and this deployment owns it.// BucketAlreadyExists means another account owns the name, so that one still throws.