Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): answer a delete normally when its claim release fails

Yudhi Armyndharis27d52a654be5
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index a1fce79ac..16bc7fe47 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -2116,6 +2116,18 @@ describe('SessionService', () => {
expect(ownership.release).not.toHaveBeenCalled();
});
+ // The row is already gone when the claim is released, so a failed release has nothing to lapse on and
+ // must not turn a committed delete into a 500 (the retry answers 404 and no audit row is written).
+ it('delete() keeps its outcome when the claim cannot be handed back', async () => {
+ const ownership = withOwnership();
+ ownership.release.mockRejectedValue(new Error('database unreachable'));
+ jest.spyOn(lifecycle, 'delete').mockResolvedValue(undefined);
+
+ await expect(service.delete('sess-uuid-1')).resolves.toBeUndefined();
+
+ expect(ownership.release).toHaveBeenCalledWith('sess-uuid-1');
+ });
+
it('forceKill() hands the claim back after the kill', async () => {
const ownership = withOwnership();
jest.spyOn(lifecycle, 'forceKill').mockResolvedValue(createMockSession());
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index ad82a3a36..e411d242c 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -607,7 +607,7 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
if (this.ownership) await this.assertNotHeldElsewhere(id);
// A failed row read deleted nothing: the lifecycle drops the mark, and the count goes with it.
await this.engineLifecycle.delete(id, { onReadFailed: () => this.uncountStopRequest(id) });
- await this.ownership?.release(id);
+ await this.releaseClaimBestEffort(id);
this.stopRequests.delete(id);
} catch (error) {
this.discardStopMarkForMissingSession(id, error);
@@ -975,9 +975,17 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
return;
}
this.releaseOwed.delete(id);
- // Best-effort: the outcome of the start or the teardown that asks is already decided, and a claim that
- // cannot be handed back lapses with its lease, which a peer then adopts. A failure here must not
- // replace that outcome (a started session reported as failed, a stopped one as an error).
+ await this.releaseClaimBestEffort(id);
+ }
+
+ /**
+ * Best-effort: the outcome of the start or the teardown that asks is already decided, and a claim that
+ * cannot be handed back lapses with its lease, which a peer then adopts. A failure here must not
+ * replace that outcome (a started session reported as failed, a stopped one as an error, a committed
+ * delete as a 500 whose retry answers 404 and writes no audit row).
+ */
+ private async releaseClaimBestEffort(id: string): Promise<void> {
+ if (!this.ownership) return;
try {
await this.ownership.release(id);
} catch (error) {