Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): drop chat-state completeness marks before a reload read

Yudhi Armyndharisfb45c174b29a
diff --git a/src/engine/adapters/baileys-chat-state-store.service.spec.ts b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
index a4612af3c..e5b1c8d89 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
@@ -285,6 +285,25 @@ describe('ChatStateStoreService', () => {
});
});
+ it('a reload that cannot read the table sends uncached chats back through the table', async () => {
+ const repo = makeRepo([{ sessionId: 's', chatId: 'a', archived: true }]);
+ const svc = svcWith(repo);
+ await svc.refreshSession('s'); // 's' is complete: a miss costs no query
+ svc.get('t', 'gone');
+ await tick(); // 't'/'gone' is now known to have no row
+ // A restore replaces the table, then the post-commit reload fails.
+ await repo.upsert({ sessionId: 's', chatId: 'b', pinned: true } as ChatState);
+ await repo.upsert({ sessionId: 't', chatId: 'gone', archived: true } as ChatState);
+ repo.find.mockRejectedValueOnce(new Error('SQLITE_BUSY'));
+ await expect(svc.reload()).resolves.toBeUndefined();
+ svc.get('s', 'b');
+ svc.get('t', 'gone');
+ await tick();
+ expect(svc.get('s', 'b')).toEqual(expect.objectContaining({ pinned: true }));
+ expect(svc.get('t', 'gone')).toEqual(expect.objectContaining({ archived: true }));
+ expect(svc.get('s', 'a')).toEqual(expect.objectContaining({ archived: true })); // cached rows are kept
+ });
+
describe('a session whose rows all fit in the cache answers a miss without a query', () => {
const seeded = () =>
makeRepo([
diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.ts
index d8d263934..b65e5bfba 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.ts
@@ -174,6 +174,10 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
async reload(): Promise<void> {
const seq = this.writeSeq;
const idle = this.writes.size === 0;
+ // Dropped before the read, as refreshSession does: a restore may have replaced the table, so if
+ // the read fails, every chat not cached must read through rather than trust a stale mark.
+ this.completeSessions.clear();
+ this.absent.clear();
try {
const rows = await this.repo.find({
order: { updatedAt: 'DESC' },