Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): remove a deleted session's send idempotency claims

Yudhi Armyndharis0f74926dfa2c
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index e8e7f8958..b7c620751 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -1278,7 +1278,7 @@ Delete a session.
**Response** `204` — empty body (`@HttpCode(204)`, returns void). A `findOne` lookup runs first, so a missing id yields `404`.
-The delete removes, in one transaction, the session's messages, message batches, chat states, received statuses, webhooks, templates, stored Baileys messages, automation rules, webhook outbox rows, webhook delivery-failure records and integration dead-letter rows, then purges its on-disk auth data. It keeps the audit log, the LID mappings, and the integration conversation mappings (so a re-paired session keeps its provider conversations); ingress dedup rows age out on their own after `INGRESS_DEDUP_RETENTION_DAYS` (default 7).
+The delete removes, in one transaction, the session's messages, message batches, chat states, received statuses, webhooks, templates, stored Baileys messages, automation rules, webhook outbox rows, webhook delivery-failure records, integration dead-letter rows and send idempotency claims, then purges its on-disk auth data. It keeps the audit log, the LID mappings, and the integration conversation mappings (so a re-paired session keeps its provider conversations); ingress dedup rows age out on their own after `INGRESS_DEDUP_RETENTION_DAYS` (default 7).
**Errors:** `401` missing/invalid key · `403` key not scoped to this session, or key role below OPERATOR · `404` session not found · `409` credential teardown for the same session name still in flight (retryable; body carries `code: 'SESSION_NAME_TEARDOWN_PENDING'`; on a `409` the row is **not** deleted and no hook/auth-purge runs — retry after cleanup settles), or, on a multi-node deployment, another node currently holds this session's live engine (no `code`; nothing is deleted; send the delete to the owning node)
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 88aac9036..f16f1bd48 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -11,6 +11,7 @@ import { Repository, DataSource } from 'typeorm';
import { Session, SessionStatus } from './entities/session.entity';
import { Message } from '../message/entities/message.entity';
import { MessageBatch } from '../message/entities/message-batch.entity';
+import { SendIdempotencyKey } from '../message/entities/send-idempotency-key.entity';
import { Webhook } from '../webhook/entities/webhook.entity';
import { WebhookOutboxEvent } from '../webhook/entities/webhook-outbox-event.entity';
import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';
@@ -748,9 +749,11 @@ export class SessionEngineControls {
// because depending on a pragma neither this file nor a test pins is a thinner guarantee than
// an explicit delete, and the ordering mirrors the restore path's explicit-clear. chat_states and
// status_updates are in the no-FK group too, and so are the session's webhook outbox rows,
- // webhook delivery-failure records and integration dead-letter rows (which keep full payloads).
- // Those three are point-in-time: a delivery still in flight can write one after the commit, and
- // their retention windows cover it.
+ // webhook delivery-failure records, integration dead-letter rows (which keep full payloads) and
+ // send idempotency claims (which keep the stored response of a send for 24 h, so a session
+ // re-created under the same id could not replay one). The webhook and dead-letter records are
+ // point-in-time: a delivery still in flight can write one after the commit, and their retention
+ // windows cover it.
//
// Deliberately kept: conversation_mappings (a re-paired session is rebound to its provider
// conversations, see ConversationMappingService.rebindSession), ingress_events (the
@@ -767,6 +770,7 @@ export class SessionEngineControls {
await manager.delete(WebhookOutboxEvent, { sessionId: id });
await manager.delete(WebhookDeliveryFailure, { sessionId: id });
await manager.delete(IntegrationDeliveryFailure, { sessionId: id });
+ await manager.delete(SendIdempotencyKey, { sessionId: id });
await manager.remove(session);
});
parentDeleted = true;
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index c53c6bb2d..6a72408b0 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -43,6 +43,7 @@ import { StatusUpdate } from '../status-store/entities/status-update.entity';
import { WebhookOutboxEvent } from '../webhook/entities/webhook-outbox-event.entity';
import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';
import { IntegrationDeliveryFailure } from '../integration/entities/integration-delivery-failure.entity';
+import { SendIdempotencyKey } from '../message/entities/send-idempotency-key.entity';
import { ConversationMapping } from '../integration/entities/conversation-mapping.entity';
import { IngressEvent } from '../integration/entities/ingress-event.entity';
import { LidMapping } from '../../engine/identity/lid-mapping.entity';
@@ -1341,6 +1342,9 @@ describe('SessionService', () => {
expect(managerDelete).toHaveBeenCalledWith(WebhookOutboxEvent, { sessionId: 'sess-uuid-1' });
expect(managerDelete).toHaveBeenCalledWith(WebhookDeliveryFailure, { sessionId: 'sess-uuid-1' });
expect(managerDelete).toHaveBeenCalledWith(IntegrationDeliveryFailure, { sessionId: 'sess-uuid-1' });
+ // Send idempotency claims hold the stored response of a send for 24 h: a session re-created under
+ // the same id must not replay one of the deleted session's.
+ expect(managerDelete).toHaveBeenCalledWith(SendIdempotencyKey, { sessionId: 'sess-uuid-1' });
// Kept on purpose: a re-paired session is rebound to its conversations, ingress rows are the
// instance-keyed dedup record, and LID mappings are a global cache.
const deleted = managerDelete.mock.calls.map(([entity]) => entity as unknown);