Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): give a lid row to the latest session to see its phone

Yudhi Armyndharisf8ca7fd0c310
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.ts
index 26341cb33..fa526ab81 100644
--- a/src/engine/identity/lid-mapping-store.service.ts
+++ b/src/engine/identity/lid-mapping-store.service.ts
@@ -50,8 +50,9 @@ export interface LidMappingStore {
/** The lids the table maps to these phone digits, read from the database. */
findLidsForPhone?(phone: string): Promise<string[]>;
/**
- * Write-through: update the cache + persist. A phone is last-write-wins. A `null` phone records a negative
- * result, and replaces a stored phone only when the same session (or none, or one since deleted) wrote it.
+ * Write-through: update the cache + persist. A phone is last-write-wins, and the same phone from another
+ * session makes that session the row's owner. A `null` phone records a negative result, and replaces a
+ * stored phone only when its owner (or none, or one since deleted) gives it.
*/
remember(lid: string, phone: string | null, sessionId?: string): Promise<void>;
}
@@ -108,6 +109,13 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
* write committed, so whatever it answers predates the mapping this process now holds.
*/
private readonly racedLookups = new Set<string>();
+ /**
+ * Sessions that wrote or reported the phone a lid holds in {@link lidToPhone}. One not in it that
+ * reports the same phone takes the row, in one write, so the row names the latest session to see
+ * that phone and the negative of a session that saw it earlier leaves it; one already in it reports
+ * again for free. Dropped with the cached entry.
+ */
+ private readonly seenBy = new Map<string, Set<string>>();
// 0 = unbounded (legacy behaviour). Every other long-lived map in the engine surface is bounded, so
// the default is finite; the env override exists for operators who explicitly want the old behaviour.
private readonly maxCachedLids: number;
@@ -145,12 +153,13 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
});
this.lidToPhone.clear();
this.phoneToLids.clear();
+ this.seenBy.clear();
// A reload re-reads the table, so every recorded absence is a fresh question again.
this.absentFromTable.clear();
// Oldest first, so the newest row ends at the most-recent end of the LRU rather than the first
// one evicted.
for (const row of rows.reverse()) {
- this.index(row.lid, row.phone);
+ this.index(row.lid, row.phone, row.sessionId);
}
this.logger.log(
`Loaded ${rows.length} lid->phone mappings into cache${this.maxCachedLids ? ` (cap ${this.maxCachedLids})` : ''}`,
@@ -277,11 +286,14 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
}
async remember(lid: string, phone: string | null, sessionId?: string): Promise<void> {
- if (!lid || this.lidToPhone.get(lid) === phone) {
- return; // unseen-or-changed only; a no-op write would just churn updatedAt
+ if (!lid) return;
+ // Unseen-or-changed only, or a phone a session reports here for the first time: a no-op write would
+ // just churn updatedAt.
+ if (this.lidToPhone.get(lid) === phone && (phone === null || !sessionId || this.seenBy.get(lid)?.has(sessionId))) {
+ return;
}
// A negative is not cached until the table has taken it: see clear().
- if (phone !== null) this.index(lid, phone);
+ if (phone !== null) this.index(lid, phone, sessionId ?? null);
this.unsettledWrites.set(lid, (this.unsettledWrites.get(lid) ?? 0) + 1);
if (this.pendingLookups.has(lid)) this.racedLookups.add(lid);
try {
@@ -328,6 +340,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
if (!affected) return;
const cached = this.lidToPhone.get(lid);
this.lidToPhone.delete(lid);
+ this.seenBy.delete(lid);
if (cached) this.unindex(lid, cached);
this.absentFromTable.delete(lid);
}
@@ -372,7 +385,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
if (writeInFlight || this.racedLookups.has(lid)) return;
// Last-write-wins: a mapping indexed while the lookup was in flight is newer.
if (row && !this.lidToPhone.has(row.lid)) {
- this.index(row.lid, row.phone);
+ this.index(row.lid, row.phone, row.sessionId);
return;
}
if (!row && !this.lidToPhone.has(lid)) this.noteAbsent(lid);
@@ -385,11 +398,15 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
}
/** Update both in-memory indexes, dropping any stale reverse entry from a previous phone. */
- private index(lid: string, phone: string | null): void {
+ private index(lid: string, phone: string | null, sessionId: string | null): void {
const prev = this.lidToPhone.get(lid);
if (prev && prev !== phone) {
this.unindex(lid, prev);
}
+ const seen = (prev === phone && this.seenBy.get(lid)) || new Set<string>();
+ if (sessionId) seen.add(sessionId);
+ if (phone) this.seenBy.set(lid, seen);
+ else this.seenBy.delete(lid);
// Re-insert (delete + set) so the entry moves to the most-recent end of the LRU order even on update.
this.lidToPhone.delete(lid);
this.lidToPhone.set(lid, phone);
@@ -427,6 +444,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
if (oldest === undefined) break;
const phone = this.lidToPhone.get(oldest);
this.lidToPhone.delete(oldest);
+ this.seenBy.delete(oldest);
if (phone) this.unindex(oldest, phone);
}
}
diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.ts
index 7555c1930..42eda6167 100644
--- a/src/engine/identity/lid-mapping-store.spec.ts
+++ b/src/engine/identity/lid-mapping-store.spec.ts
@@ -211,6 +211,18 @@ describe('LidMappingStoreService — LRU cap', () => {
expect([...phoneToLids.keys()].sort()).toEqual(['620002', '620004']);
});
+ it('drops who reported a phone along with the evicted entry', async () => {
+ process.env.LID_MAPPING_CACHE_MAX = '2';
+ const store = await newStore(makeFakeRepo());
+ const seenBy = (store as unknown as { seenBy: Map<string, Set<string>> }).seenBy;
+
+ await store.remember('lid-a', '620001', 's1');
+ await store.remember('lid-b', '620002', 's1');
+ await store.remember('lid-c', '620003', 's1'); // evicts lid-a
+
+ expect([...seenBy.keys()].sort()).toEqual(['lid-b', 'lid-c']);
+ });
+
it('LID_MAPPING_CACHE_MAX=0 disables the cap (legacy unbounded behaviour)', async () => {
process.env.LID_MAPPING_CACHE_MAX = '0';
const repo = makeFakeRepo();
@@ -950,6 +962,65 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>
expect(await row('111')).toMatchObject({ phone: '628222', sessionId: s3 });
});
+ it('gives the row to a second session that reports the same phone, so the first one no longer clears it', async () => {
+ await store.remember('111', '628111', s1);
+ await store.remember('111', '628111', s2);
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s2 });
+
+ await store.remember('111', null, s1);
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s2 });
+ expect(store.getCached('111')).toBe('628111');
+
+ await store.remember('111', null, s2);
+ expect(await row('111')).toMatchObject({ phone: null, sessionId: s2 });
+ });
+
+ it('writes a phone each session keeps reporting once, not once per report', async () => {
+ const upsert = jest.spyOn(repo, 'upsert');
+ for (let i = 0; i < 50; i++) {
+ await store.remember('111', '628111', s1);
+ await store.remember('111', '628111', s2);
+ }
+
+ expect(upsert).toHaveBeenCalledTimes(2);
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s2 });
+ });
+
+ it('forgets who reported a phone once the phone changes or is cleared', async () => {
+ const seenBy = (store as unknown as { seenBy: Map<string, Set<string>> }).seenBy;
+ await store.remember('111', '628111', s1);
+ await store.remember('111', '628111', s2);
+ await store.remember('111', '628222', s3);
+ await store.remember('111', '628222', s1); // s1 reported the old phone, not this one
+
+ expect(await row('111')).toMatchObject({ phone: '628222', sessionId: s1 });
+ await store.remember('111', null, s1);
+ expect(seenBy.has('111')).toBe(false);
+ });
+
+ it('knows the owner of a row it loaded, so only another session reporting the phone writes', async () => {
+ await repo.insert({ lid: '111', phone: '628111', sessionId: s1 });
+ await store.reload();
+ const upsert = jest.spyOn(repo, 'upsert');
+
+ await store.remember('111', '628111', s1);
+ expect(upsert).not.toHaveBeenCalled();
+ await store.remember('111', '628111', s2);
+ expect(upsert).toHaveBeenCalledTimes(1);
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s2 });
+ });
+
+ it('knows the owner of a row it warms on a miss as well', async () => {
+ await repo.insert({ lid: '111', phone: '628111', sessionId: s1 }); // stored after this node loaded the table
+ for (let i = 0; i < 100 && store.getCached('111') === undefined; i++) await new Promise(r => setTimeout(r, 5));
+ const upsert = jest.spyOn(repo, 'upsert');
+
+ await store.remember('111', '628111', s1);
+ expect(upsert).not.toHaveBeenCalled();
+ await store.remember('111', '628111', s2);
+ expect(upsert).toHaveBeenCalledTimes(1);
+ });
+
it('keeps a phone stored while the negative of another session is being written', async () => {
const update = hold('update');
const negative = store.remember('111', null, s2);
diff --git a/src/engine/identity/lid-mapping.entity.ts b/src/engine/identity/lid-mapping.entity.ts
index cc1c9c6d1..25fb6d3ae 100644
--- a/src/engine/identity/lid-mapping.entity.ts
+++ b/src/engine/identity/lid-mapping.entity.ts
@@ -9,8 +9,8 @@ import { Column, Entity, Index, PrimaryColumn, UpdateDateColumn } from 'typeorm'
* so it's a cache WhatsApp can correct. `phone` is nullable to record a negative result (a lid we looked
* up and could not resolve) so active lookups stay one-network-call-per-unknown-lid. A negative replaces a
* phone only when the session that wrote it (or none, or one since deleted) answers it, since another
- * account may not see the number. `sessionId` is the session that last wrote the row - intentionally NOT a
- * foreign key, since the row outlives any one session.
+ * account may not see the number. `sessionId` is the session that last wrote the row or reported its phone -
+ * intentionally NOT a foreign key, since the row outlives any one session.
*/
@Entity('lid_mappings')
@Index(['phone']) // reverse lookup: phone -> lids, for the message from-filter
@@ -23,7 +23,7 @@ export class LidMapping {
@Column({ type: 'varchar', nullable: true })
phone!: string | null;
- /** The session that last wrote this row; a negative from another live session leaves its phone. Not a foreign key. */
+ /** The row's owner: the session that last wrote it or reported its phone (see above). Not a foreign key. */
@Column({ type: 'varchar', nullable: true })
sessionId!: string | null;
diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.ts
index 7b6f5d8bc..8b7c89e0f 100644
--- a/src/modules/auth/chat-scope.service.spec.ts
+++ b/src/modules/auth/chat-scope.service.spec.ts
@@ -159,6 +159,14 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>
expect(await admitted()).toEqual([true, true]);
});
+ it('still admits it after the session that stored the phone answers no phone, when another one reported it since', async () => {
+ await store.remember(LID, PHONE, s2);
+
+ await store.remember(LID, null, s1);
+
+ expect(await admitted()).toEqual([true, true]);
+ });
+
it('stops admitting it once the session that stored the phone answers no phone', async () => {
await store.remember(LID, null, s1);