Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): map outer init-hang timeout to 504 too (#733) (#743)
Yudhi Armyndharis08d215d96efc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1eb5b4fc2..d3f54fcab 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -15,14 +15,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed-- **Engine auth-timeout now returns a diagnostic 504 instead of a bare 500.** When the WhatsApp Web- engine's internal authentication poll exhausts its timeout (default 30s) — e.g. the session proxy- is unreachable, so the browser launches but no QR is ever delivered — `POST /api/sessions/:id/start`- previously let the primitive `'auth timeout'` string escape to NestJS's default handler as a- meaningless `500 Internal Server Error`. It now maps to `504 Gateway Timeout` with a message- pointing at the proxy / network-egress / firewall causes and the `WWEBJS_AUTH_TIMEOUT_MS` knob for- legitimately slow first boots. The outer engine-init hang deadline (`EngineInitTimeoutError`) still- surfaces as 500 and is tracked separately.+- **Engine start timeouts now return a diagnostic 504 instead of a bare 500.** Two+ `POST /api/sessions/:id/start` failure modes previously escaped to NestJS's default handler as a+ meaningless `500 Internal Server Error`: (1) the **auth-timeout** — whatsapp-web.js throws the+ primitive string `'auth timeout'` when its login poll exhausts `authTimeoutMs` (default 30s), e.g.+ an unreachable session proxy means the browser launches but no QR is ever delivered; and (2) the+ **outer init-hang deadline** (`EngineInitTimeoutError`) — a wedged `initialize()` that never settles+ within `max(60s, WWEBJS_AUTH_TIMEOUT_MS+30s)`, usually a container memory/resource limit or a stalled+ Chromium. Both now map to `504 Gateway Timeout` with a diagnostic message (proxy/network vs resource+ limits respectively) and the `WWEBJS_AUTH_TIMEOUT_MS` knob for slow first boots. Engine cleanup+ (force-destroy + evict + status) still runs before mapping; generic non-timeout init rejections+ (e.g. "chromium launch failed") still propagate untouched.- **S3 storage no longer falls back to local without an `endpoint`.** The S3 client init required an`endpoint`, which only S3-compatible stores (MinIO, R2) need — standard AWS S3 (whose endpoint isdiff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 3dd28e85c..6655a739d 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -3,7 +3,7 @@ import { getRepositoryToken, getDataSourceToken } from '@nestjs/typeorm';import { Repository, DataSource, In } from 'typeorm';import { NotFoundException, ConflictException, BadRequestException, HttpException, HttpStatus } from '@nestjs/common';import { ConfigService } from '@nestjs/config';-import { SessionService, ACK_RECONCILE_DELAY_MS, EngineInitTimeoutError } from './session.service';+import { SessionService, ACK_RECONCILE_DELAY_MS } from './session.service';import { Session, SessionStatus } from './entities/session.entity';import { Message, MessageStatus } from '../message/entities/message.entity';import { MessageBatch } from '../message/entities/message-batch.entity';@@ -726,8 +726,12 @@ describe('SessionService', () => {await jest.advanceTimersByTimeAsync(60_000);await settled;- expect(caught).toBeInstanceOf(EngineInitTimeoutError);- expect(String(caught)).toMatch(/timed out after 60000ms/i);+ // The outer init-hang deadline now maps to a diagnostic 504 (like the auth-timeout) instead of+ // escaping as a bare 500 (#733 follow-up). Cleanup (force-destroy + evict + DISCONNECTED) still+ // runs inside initializeEngine before the mapped error is thrown — asserted below.+ expect(caught).toBeInstanceOf(HttpException);+ expect((caught as HttpException).getStatus()).toBe(HttpStatus.GATEWAY_TIMEOUT);+ expect((caught as HttpException).getResponse() as string).toMatch(/timed out after 60000ms/i);expect(mockEngine.forceDestroy).toHaveBeenCalled(); // wedged browser reapedexpect(intern().engines.has('sess-uuid-1')).toBe(false); // slot freed for retryexpect(repository.update).toHaveBeenCalledWith('sess-uuid-1', { status: SessionStatus.DISCONNECTED });@@ -763,8 +767,9 @@ describe('SessionService', () => {// Past the derived 150s deadline the race finally fires.await jest.advanceTimersByTimeAsync(90_000); // 60s + 90s = 150sawait settled;- expect(caught).toBeInstanceOf(EngineInitTimeoutError);- expect(String(caught)).toMatch(/timed out after 150000ms/i);+ expect(caught).toBeInstanceOf(HttpException);+ expect((caught as HttpException).getStatus()).toBe(HttpStatus.GATEWAY_TIMEOUT);+ expect((caught as HttpException).getResponse() as string).toMatch(/timed out after 150000ms/i);} finally {jest.useRealTimers();delete process.env.WWEBJS_AUTH_TIMEOUT_MS;diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex e2827136b..308484deb 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -1224,6 +1224,15 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat// teardownEngineSafely is itself time-bound, so this can't wedge a second time.await this.teardownEngineSafely(id, engine, e => e.forceDestroy(), 'force-destroy');await this.updateStatus(id, SessionStatus.DISCONNECTED);+ // Map to a diagnostic 504 like the auth-timeout branch below, so a wedged init doesn't escape as a+ // bare 500 (#733 follow-up). The browser stalled mid-startup — usually a container memory/resource+ // limit or a wedged Chromium, not a network/proxy issue (that's the auth-timeout's signature).+ throw new HttpException(+ `Engine initialization timed out after ${err.timeoutMs}ms — the browser process did not complete ` ++ 'startup in time (often a container memory/resource limit or a stalled Chromium, not a network ' ++ 'issue). Retry the session; for chronically slow first boots, raise WWEBJS_AUTH_TIMEOUT_MS.',+ HttpStatus.GATEWAY_TIMEOUT,+ );} else if (isAuthTimeoutRejection(err)) {// The engine's INTERNAL auth-timeout: whatsapp-web.js throws the primitive string 'auth timeout'// (see ENGINE_AUTH_TIMEOUT) when its inject poll exhausts authTimeoutMs (default 30s) — the common