Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): stop counting stop requests that took nothing down

Yudhi Armyndharis42b2258848d9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6eaabd5b8..b443eeebc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -57,7 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A session whose automatic reconnect fails to relaunch the engine (a network, DNS or browser launch error) keeps retrying with backoff instead of stopping in `failed` until restarted by hand; an authentication failure or a stale browser profile still ends in `failed` ([#1580](https://github.com/rmyndharis/OpenWA/issues/1580)).
- A session that runs out of reconnect attempts fires the `session:error` plugin hook when it lands in `failed`.
- A session that runs out of reconnect attempts keeps the last attempt's failure reason in `lastError` and in the `session:error` hook, after the attempts message.
-- A stop during the retry delay after a transient start failure is no longer undone by the retry.
+- A stop during the retry delay after a transient start failure is no longer undone by the retry; a stop or delete the ownership fence refused leaves the retry alone.
- In a multi-node deployment, a start cut short by a concurrent stop releases its claim, so a peer no longer adopts and restarts the stopped session.
- A stop or delete that fails on a database error no longer blocks the session's next automatic reconnect or makes a later start answer "already started".
- A logout or force-kill refused as "not started" leaves the session's claim untouched, so a crashed node's session stays visible to the takeover sweep.
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index b89fb8628..95fa3c7a8 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -1389,6 +1389,57 @@ describe('SessionService', () => {
stopping.delete('sess-uuid-1');
});
+ // The per-id request count is set on the same tick as the mark and has the same reclamation
+ // problem: its only reader is the transient start retry, which has nothing to guard once the
+ // request is refused, so a caller hammering ids that 404 or belong to a peer would otherwise
+ // grow the Map one entry per id for the life of the process.
+ it.each([
+ [
+ 'stop',
+ 'no session row',
+ (s: SessionService) => s.stop('sess-uuid-1'),
+ () => (repository.findOne as jest.Mock).mockResolvedValue(null),
+ ],
+ [
+ 'delete',
+ 'no session row',
+ (s: SessionService) => s.delete('sess-uuid-1'),
+ () => (repository.findOne as jest.Mock).mockResolvedValue(null),
+ ],
+ [
+ 'stop',
+ 'a 409 from the ownership fence',
+ (s: SessionService) => s.stop('sess-uuid-1'),
+ () => withOwnership().isHeldByOtherNode.mockResolvedValue(true),
+ ],
+ [
+ 'delete',
+ 'a 409 from the ownership fence',
+ (s: SessionService) => s.delete('sess-uuid-1'),
+ () => withOwnership().isHeldByOtherNode.mockResolvedValue(true),
+ ],
+ [
+ 'stop',
+ 'a failed ownership query',
+ (s: SessionService) => s.stop('sess-uuid-1'),
+ () => withOwnership().isHeldByOtherNode.mockRejectedValue(new Error('Connection terminated unexpectedly')),
+ ],
+ [
+ 'delete',
+ 'a failed ownership query',
+ (s: SessionService) => s.delete('sess-uuid-1'),
+ () => withOwnership().isHeldByOtherNode.mockRejectedValue(new Error('Connection terminated unexpectedly')),
+ ],
+ ])('%s() refused by %s counts no stop request', async (_verb, _refusal, call, arrange) => {
+ arrange();
+ const counts = (service as unknown as { stopRequests: Map<string, number> }).stopRequests;
+
+ await expect(call(service)).rejects.toThrow();
+
+ expect([...counts]).toEqual([]);
+ (lifecycle as unknown as { stoppingSessions: Set<string> }).stoppingSessions.delete('sess-uuid-1');
+ });
+
// A failed read decided nothing. A mark left on a running session made its next disconnect skip
// the reconnect while the dead engine stayed registered, so start() answered "already started".
it.each([
@@ -1528,6 +1579,48 @@ describe('SessionService', () => {
expect(await nodeIdOf(row.id)).toBe('node-a');
});
+ // A stop whose ownership query failed took nothing down, so the retry still has a session to
+ // bring back. Counting that refusal skipped the retry and left the session DISCONNECTED with
+ // its claim released, waiting for an operator.
+ it('start() still retries a transient failure when a stop refused by the fence landed during the delay', async () => {
+ const row = await seed();
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession({ id: row.id }));
+ mockEngine.initialize.mockRejectedValueOnce(new EngineTransportError('Protocol error: Target closed'));
+
+ const starting = service.start(row.id);
+ await until(() => mockEngine.initialize.mock.calls.length === 1 && !lifecycle.isEngineActive(row.id));
+ jest
+ .spyOn(ownership, 'isHeldByOtherNode')
+ .mockRejectedValueOnce(new Error('Connection terminated unexpectedly'));
+ await expect(service.stop(row.id)).rejects.toThrow('Connection terminated unexpectedly');
+
+ await starting;
+
+ expect(mockEngine.initialize).toHaveBeenCalledTimes(2);
+ expect(lifecycle.isEngineActive(row.id)).toBe(true);
+ expect(await nodeIdOf(row.id)).toBe('node-a');
+ });
+
+ // The rollback decrements, it does not clear: a stop that DID take the engine down stays
+ // counted when a later request is refused, so the retry it must cancel stays cancelled.
+ // Clearing instead would relaunch the session the first stop took down and re-claim its row.
+ it('start() does not retry when an effective stop during the delay is followed by a refused one', async () => {
+ const row = await seed();
+ (repository.findOne as jest.Mock).mockResolvedValue(createMockSession({ id: row.id }));
+ mockEngine.initialize.mockRejectedValueOnce(new EngineTransportError('Protocol error: Target closed'));
+
+ const starting = service.start(row.id);
+ const outcome = starting.catch((error: unknown) => error);
+ await until(() => mockEngine.initialize.mock.calls.length === 1 && !lifecycle.isEngineActive(row.id));
+ await service.stop(row.id);
+ jest.spyOn(ownership, 'isHeldByOtherNode').mockResolvedValueOnce(true);
+ await expect(service.stop(row.id)).rejects.toThrow(ConflictException);
+
+ expect(await outcome).toBeInstanceOf(EngineTransportError);
+ expect(mockEngine.initialize).toHaveBeenCalledTimes(1);
+ expect(lifecycle.isEngineActive(row.id)).toBe(false);
+ });
+
// A 400 changes nothing, and release() also clears a lapsed foreign claim: that took a crashed
// node's session out of the lapsed-claim sweep for good.
it.each([
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index f8a698538..7138189a1 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -480,9 +480,15 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
* their own requireSession, so for an id that never had one the entry is unreachable by every
* reclamation path and survives for the life of the process. A 404 also means there is no engine
* and no in-flight start() for the mark to guard, so dropping it is safe as well as necessary.
+ *
+ * The request count set on the same tick is dropped with it, and for the same reason: its only
+ * reader is the transient start retry, which cannot be guarding a session that has no row, so
+ * an id that never had one would otherwise keep a counter entry for the life of the process.
*/
private discardStopMarkForMissingSession(id: string, error: unknown): void {
- if (error instanceof NotFoundException) this.engineLifecycle.clearStopping(id);
+ if (!(error instanceof NotFoundException)) return;
+ this.engineLifecycle.clearStopping(id);
+ this.stopRequests.delete(id);
}
/**
@@ -496,14 +502,19 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
* holder may be gone, and taking over is exactly what the claim rule allows.
*/
private async assertNotHeldElsewhere(id: string): Promise<void> {
- // Both callers set the stop mark before this query. A refusal keeps it (see delete()), but a
- // query that failed decided nothing, and a mark left on a session running here would stop its
- // next disconnect from reconnecting.
+ // Both callers count the request and set the stop mark before this query. The MARK survives a
+ // 409 (harmless, cleared by the next start()) but not a failed query: that decided nothing, and
+ // a mark left on a session running here would stop its next disconnect from reconnecting.
+ // Neither refusal keeps its COUNT. Nothing was taken down, and the count exists only so an
+ // in-flight start()'s transient retry can tell a stop that happened from one that did not;
+ // counting a refusal cancels that retry and leaves the session down with nothing to restart it.
const heldElsewhere = await this.ownership?.isHeldByOtherNode(id).catch((error: unknown) => {
this.engineLifecycle.clearStopping(id);
+ this.uncountStopRequest(id);
throw error;
});
if (heldElsewhere) {
+ this.uncountStopRequest(id);
throw new ConflictException(`Session ${id} is running on another node`);
}
}
@@ -631,6 +642,18 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
this.engineLifecycle.markStopping(id);
}
+ /**
+ * Undo markStopping()'s count for a request that took nothing down, and drop the entry once the
+ * count is back to zero so ids that are only ever refused cannot accumulate. Decrementing rather
+ * than deleting keeps a concurrent stop that DID take effect counted: it leaves the value one
+ * above what an in-flight start captured either way, so the retry it must cancel stays cancelled.
+ */
+ private uncountStopRequest(id: string): void {
+ const remaining = (this.stopRequests.get(id) ?? 0) - 1;
+ if (remaining > 0) this.stopRequests.set(id, remaining);
+ else this.stopRequests.delete(id);
+ }
+
/** Hand the claim back unless something still runs here (engine, in-flight start, pending reconnect). */
private async releaseUnlessEngineActive(id: string): Promise<void> {
if (!this.ownership || this.engineLifecycle.isEngineActive(id)) {