Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): stop a stop/start mid-heartbeat reading as a lease loss

Yudhi Armyndharisa46ad805a2c8
diff --git a/src/modules/session/session-ownership.service.spec.ts b/src/modules/session/session-ownership.service.spec.ts
index 40ab6d5b2..72763a415 100644
--- a/src/modules/session/session-ownership.service.spec.ts
+++ b/src/modules/session/session-ownership.service.spec.ts
@@ -428,6 +428,49 @@ describe('SessionOwnershipService', () => {
await expect(nodeA.renew()).resolves.toBeUndefined();
expect(nodeA.ownedIds()).toEqual([]);
});
+
+ // A stop releases the claim while a tick is in flight: the row no longer names this node, but
+ // nothing was lost to a peer, and the lease-loss teardown would leave a stale stop mark.
+ it('does not report a session this process released during the tick', async () => {
+ const session = await seed();
+ const nodeA = service('node-a');
+ await nodeA.claim(session.id);
+ const lost: string[][] = [];
+ nodeA.onLeaseLoss(ids => void lost.push(ids));
+ const realFind = sessions.find.bind(sessions);
+ jest.spyOn(sessions, 'find').mockImplementation(async (...args: Parameters<typeof realFind>) => {
+ await nodeA.release(session.id);
+ return realFind(...args);
+ });
+
+ await nodeA.renew();
+ jest.restoreAllMocks();
+
+ expect(lost).toEqual([]);
+ });
+
+ // A stop then a start: the tick read the row between the release and the re-claim. Reporting it
+ // lost would drop the fresh claim from `owned` and tear down the engine the start is launching.
+ it('does not report a session released and re-claimed during the tick', async () => {
+ const session = await seed();
+ const nodeA = service('node-a');
+ await nodeA.claim(session.id);
+ const lost: string[][] = [];
+ nodeA.onLeaseLoss(ids => void lost.push(ids));
+ const realFind = sessions.find.bind(sessions);
+ jest.spyOn(sessions, 'find').mockImplementation(async (...args: Parameters<typeof realFind>) => {
+ await nodeA.release(session.id);
+ const rows = await realFind(...args);
+ await nodeA.claim(session.id);
+ return rows;
+ });
+
+ await nodeA.renew();
+ jest.restoreAllMocks();
+
+ expect(lost).toEqual([]);
+ expect(nodeA.ownedIds()).toEqual([session.id]);
+ });
});
describe('what a booting process may reset', () => {
diff --git a/src/modules/session/session-ownership.service.ts b/src/modules/session/session-ownership.service.ts
index f9e70e372..aac450d81 100644
--- a/src/modules/session/session-ownership.service.ts
+++ b/src/modules/session/session-ownership.service.ts
@@ -41,6 +41,8 @@ export class SessionOwnershipService {
private heartbeat?: ReturnType<typeof setInterval>;
/** Sessions this process believes it owns, so the heartbeat knows what to renew. */
private readonly owned = new Set<string>();
+ /** Bumped on every claim and release, so renew() can tell a claim it read from one replaced since. */
+ private readonly claimGen = new Map<string, number>();
/** Notified when a renewal proves this process no longer holds sessions it thought it did. */
private onLeaseLost?: (sessionIds: string[]) => Promise<void> | void;
@@ -146,6 +148,7 @@ export class SessionOwnershipService {
const claimed = (result.affected ?? 0) > 0;
if (claimed) {
this.owned.add(sessionId);
+ this.bumpClaimGen(sessionId);
this.lastSeenLease.set(sessionId, leaseExpiresAt.getTime());
} else this.logger.warn('Session is held by another node', { sessionId, nodeId: this.nodeId });
return claimed;
@@ -163,6 +166,7 @@ export class SessionOwnershipService {
async release(sessionId: string): Promise<void> {
const now = new Date();
this.owned.delete(sessionId);
+ this.bumpClaimGen(sessionId);
await this.sessions
.createQueryBuilder()
.update(Session)
@@ -172,6 +176,10 @@ export class SessionOwnershipService {
.execute();
}
+ private bumpClaimGen(sessionId: string): void {
+ this.claimGen.set(sessionId, (this.claimGen.get(sessionId) ?? 0) + 1);
+ }
+
/** Release everything this process holds, on the way down. */
async releaseAll(): Promise<void> {
const ids = [...this.owned];
@@ -253,6 +261,7 @@ export class SessionOwnershipService {
*/
async renew(): Promise<void> {
const held = [...this.owned];
+ const heldGen = new Map(held.map(id => [id, this.claimGen.get(id)]));
// Only claims that still cover something alive on this process are pushed out. A claim whose
// engine is gone (a failed start, an exhausted reconnect) must be allowed to lapse — renewing
@@ -301,7 +310,10 @@ export class SessionOwnershipService {
// entry-only check would let it through. Renewing was harmless; concluding loss is not.
if (this.lossDetectionSuspended > 0) return;
- const lost = held.filter(id => !kept.has(id));
+ // Only a claim this tick actually read can be lost. One released during the queries (a stop) is
+ // gone on purpose, and one released and claimed again (a stop, then a start) may have been read
+ // in between: neither was taken by a peer.
+ const lost = held.filter(id => !kept.has(id) && this.owned.has(id) && this.claimGen.get(id) === heldGen.get(id));
if (lost.length === 0) return;
for (const id of lost) this.owned.delete(id);
this.logger.warn(`Lost the claim on ${lost.length} session(s); another node now holds them`, {