Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): skip a lid table read that raced a newer write
Yudhi Armyndharis1180f3acc932
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex 09f9264a4..9bdab8b72 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -97,8 +97,14 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {* maps synchronously and writes the table afterwards, so between the two a table read answers "no* row" about a mapping that is about to exist. The forward map cannot stand in for this: the LRU* can evict the entry inside that same window, leaving it indistinguishable from never-learned.+ * Counted per lid, so the first of two overlapping writes settling does not clear the second.*/- private readonly unsettledWrites = new Set<string>();+ private readonly unsettledWrites = new Map<string, number>();+ /**+ * Lids a write started for while their table read was in flight. That read may have run before the+ * write committed, so whatever it answers predates the mapping this process now holds.+ */+ private readonly racedLookups = new 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;@@ -267,7 +273,8 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {return; // unseen-or-changed only; a no-op write would just churn updatedAt}this.index(lid, phone);- this.unsettledWrites.add(lid);+ this.unsettledWrites.set(lid, (this.unsettledWrites.get(lid) ?? 0) + 1);+ if (this.pendingLookups.has(lid)) this.racedLookups.add(lid);try {await this.repo.upsert({ lid, phone, sessionId: sessionId ?? null, updatedAt: new Date() }, ['lid']);} catch (err) {@@ -275,7 +282,9 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {`Failed to persist lid->phone mapping for ${lid}: ${err instanceof Error ? err.message : String(err)}`,);} finally {- this.unsettledWrites.delete(lid);+ const left = (this.unsettledWrites.get(lid) ?? 1) - 1;+ if (left > 0) this.unsettledWrites.set(lid, left);+ else this.unsettledWrites.delete(lid);}}@@ -298,18 +307,23 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {void this.repo.findOne({ where: { lid } }).then(row => {- // Last-write-wins: a remember() that landed while the lookup was in flight is newer.+ // A write that was unsettled when the query ran, or started while it was in flight, is newer+ // than either answer: a row would be the phone it replaces, served as a hit after the write+ // commits, and an absence would block the warm-back once the LRU evicts the forward entry.+ // Neither is recorded, so the next miss reads the table again.+ 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);return;}- // Same rule for the negative: an absence recorded over a mapping this process already holds,- // or is still writing, would block the warm-back once the LRU evicts the forward entry, and- // the row would then be unreachable until something taught the same lid again.- if (!row && !this.lidToPhone.has(lid) && !writeInFlight) this.noteAbsent(lid);+ if (!row && !this.lidToPhone.has(lid)) this.noteAbsent(lid);}).catch(() => undefined)- .finally(() => this.pendingLookups.delete(lid));+ .finally(() => {+ this.pendingLookups.delete(lid);+ this.racedLookups.delete(lid);+ });}/** Update both in-memory indexes, dropping any stale reverse entry from a previous phone. */diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 5345b8d18..3764e79a0 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -422,6 +422,79 @@ describe('LidMappingStoreService — deterministic preload + repository fallbackexpect(store.getCached('lid-writing')).toBe('620001'); // warmed back rather than blocked});+ /** Hold the next upsert open until the returned commit() applies it, as the fake repo would. */+ function holdNextUpsert(repo: ReturnType<typeof makeFakeRepo>): () => void {+ const apply = repo.upsert.getMockImplementation() as (values: Partial<LidMapping>) => Promise<object>;+ let commit: () => void = () => undefined;+ repo.upsert.mockImplementationOnce(+ (values: Partial<LidMapping>) => new Promise(resolve => (commit = () => resolve(apply(values)))),+ );+ return () => commit();+ }++ // The same window from the other side: the table still holds the OLD phone, so the read returns a+ // row, and indexing it would leave the stale phone served as a cache hit after the write commits.+ it('does not index a table row read while a newer write for that lid is in flight', async () => {+ process.env.LID_MAPPING_CACHE_MAX = '1';+ const repo = makeFakeRepo([{ lid: 'lid-moved', phone: '620001' }]);+ const commit = holdNextUpsert(repo);+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);++ const writing = store.remember('lid-moved', '620009'); // indexed; its upsert is held open+ await store.remember('lid-other', '620002'); // cap 1: evicts lid-moved from the forward map++ expect(store.getCached('lid-moved')).toBeUndefined(); // the query reads the uncommitted table+ await new Promise(resolve => setImmediate(resolve));+ commit();+ await writing;++ expect(store.getCached('lid-moved')).not.toBe('620001');+ await new Promise(resolve => setImmediate(resolve));+ expect(store.getCached('lid-moved')).toBe('620009');+ });++ it('does not index a table row read before a write that settled while the read was in flight', async () => {+ process.env.LID_MAPPING_CACHE_MAX = '1';+ const repo = makeFakeRepo([{ lid: 'lid-moved', phone: '620001' }]);+ let answer: (row: LidMapping | null) => void = () => undefined;+ repo.findOne.mockImplementationOnce(() => new Promise(resolve => (answer = resolve)));+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);++ expect(store.getCached('lid-moved')).toBeUndefined(); // the query is now in flight+ const before = { ...repo.rows[0] };+ await store.remember('lid-moved', '620009'); // re-mapped and committed inside that window+ await store.remember('lid-other', '620002'); // cap 1: evicts lid-moved from the forward map+ answer(before); // the query had already run, so it answers with the old row+ await new Promise(resolve => setImmediate(resolve));++ expect(store.getCached('lid-moved')).not.toBe('620001');+ await new Promise(resolve => setImmediate(resolve));+ expect(store.getCached('lid-moved')).toBe('620009');+ });++ it('keeps a lid unsettled until the last of two overlapping writes commits', async () => {+ process.env.LID_MAPPING_CACHE_MAX = '1';+ const repo = makeFakeRepo();+ const commitFirst = holdNextUpsert(repo);+ const commitSecond = holdNextUpsert(repo);+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);++ const first = store.remember('lid-busy', '620001');+ const second = store.remember('lid-busy', '620009');+ commitFirst();+ await first; // the second write is still open+ await store.remember('lid-other', '620002'); // cap 1: evicts lid-busy from the forward map++ expect(store.getCached('lid-busy')).toBeUndefined(); // reads the first write's row+ await new Promise(resolve => setImmediate(resolve));+ commitSecond();+ await second;++ expect(store.getCached('lid-busy')).not.toBe('620001');+ await new Promise(resolve => setImmediate(resolve));+ expect(store.getCached('lid-busy')).toBe('620009');+ });+it('records no absence at all when the cap is disabled', async () => {// LID_MAPPING_CACHE_MAX=0 is documented as the legacy unbounded cache. An absence set that grows// on every lookup would be a new unbounded map the operator never asked for, and unlike the