Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): answer 409 when a stop cancels a start's retry

Yudhi Armyndharis278e02682751
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index fdd5d6981..664e1c4d0 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -1476,7 +1476,7 @@ describe('SessionService', () => {
await service.stop('sess-uuid-1');
finishClaim();
- expect(await outcome).toBeInstanceOf(EngineTransportError);
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
expect(mockEngine.initialize).toHaveBeenCalledTimes(1);
expect(lifecycle.isEngineActive('sess-uuid-1')).toBe(false);
expect(desired()).toBe('stopped');
@@ -1998,7 +1998,7 @@ describe('SessionService', () => {
await until(() => mockEngine.initialize.mock.calls.length === 1 && !lifecycle.isEngineActive(row.id));
await service.stop(row.id);
- expect(await outcome).toBeInstanceOf(EngineTransportError);
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
expect(mockEngine.initialize).toHaveBeenCalledTimes(1);
expect(lifecycle.isEngineActive(row.id)).toBe(false);
expect(await nodeIdOf(row.id)).toBeNull();
@@ -2054,7 +2054,7 @@ describe('SessionService', () => {
jest.spyOn(ownership, 'isHeldByOtherNode').mockResolvedValueOnce(true);
await expect(service.stop(row.id)).rejects.toThrow(ConflictException);
- expect(await outcome).toBeInstanceOf(EngineTransportError);
+ expect(await outcome).toBeInstanceOf(SessionStoppedException);
expect(mockEngine.initialize).toHaveBeenCalledTimes(1);
expect(lifecycle.isEngineActive(row.id)).toBe(false);
});
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index 7b31c0363..f452228ed 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -693,8 +693,12 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
await setTimeout(SESSION_START_RETRY_DELAY_MS);
// Retrying would bring back a session that a stop() issued since this start began just took
// down. A mark alone does not prove that: an earlier stop's mark survives until start() clears
- // it, and a first attempt failing before that point would otherwise lose its retry.
- if (this.stopRequests.get(id) !== stopRequestsBefore) throw error;
+ // it, and a first attempt failing before that point would otherwise lose its retry. Answered
+ // as the stop it yielded to, like the check above: a 503 reads as retryable, and a client that
+ // replays the start would bring the session back.
+ if (this.stopRequests.get(id) !== stopRequestsBefore) {
+ throw new SessionStoppedException(`Session ${id} was stopped`);
+ }
// The lease may have lapsed while the first attempt ran; the retry must keep holding the
// claim, never 409 on the session it already owns.
if (this.ownership && !(await this.ownership.claim(id))) {
@@ -702,7 +706,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
throw new ConflictException(`Session ${id} is running on another node`);
}
// Checked again: a stop that landed during the re-claim has already released it.
- if (this.stopRequests.get(id) !== stopRequestsBefore) throw error;
+ if (this.stopRequests.get(id) !== stopRequestsBefore) {
+ throw new SessionStoppedException(`Session ${id} was stopped`);
+ }
return this.engineLifecycle.start(id, { explicit });
}
}