Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): gate chat-state completeness on the session's own writes

Yudhi Armyndharis693fc4112c62
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 be46609bc..ebc37ebdc 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts
@@ -379,6 +379,49 @@ describe('ChatStateStoreService', () => {
expect(svc.get('s', 'late')).toEqual(expect.objectContaining({ pinned: true }));
});
+ it('is not complete when a write in flight as the read began settled before the read did', async () => {
+ const repo = seeded();
+ let releaseUpsert!: () => void;
+ repo.upsert.mockImplementationOnce(
+ (v: ChatState) =>
+ new Promise(resolve => {
+ releaseUpsert = () => {
+ repo.rows.set(KEY(v.sessionId, v.chatId), { ...v });
+ resolve(undefined);
+ };
+ }),
+ );
+ const svc = svcWith(repo);
+ const pending = svc.remember('s', 'late', { pinned: true });
+ await tick(); // the write is indexed and waiting on its upsert
+ // The read's snapshot predates the upsert, but the read resolves after it (pooled connections).
+ let releaseFind!: () => void;
+ repo.find.mockImplementationOnce(() => {
+ const snapshot = [...repo.rows.values()].filter(r => r.sessionId === 's');
+ return new Promise(resolve => (releaseFind = () => resolve(snapshot)));
+ });
+ const refresh = svc.refreshSession('s');
+ releaseUpsert();
+ await pending;
+ await tick();
+ releaseFind();
+ await refresh;
+ expect(svc.get('s', 'late')).toBeUndefined();
+ await tick();
+ expect(svc.get('s', 'late')).toEqual(expect.objectContaining({ pinned: true }));
+ });
+
+ it("is complete when only another session's write overlapped the refresh read", async () => {
+ const repo = seeded();
+ repo.upsert.mockImplementationOnce(() => new Promise(() => undefined)); // never settles
+ const svc = svcWith(repo);
+ void svc.remember('other', 'x', { pinned: true });
+ await tick();
+ await svc.refreshSession('s');
+ svc.get('s', 'unknown');
+ expect(repo.findOne).not.toHaveBeenCalledWith({ where: { sessionId: 's', chatId: 'unknown' } });
+ });
+
it('a write whose read-through failed leaves the session incomplete, so the row is read back', async () => {
const repo = seeded();
const svc = svcWith(repo);
diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.ts
index 271efc4c7..147f770e1 100644
--- a/src/engine/adapters/baileys-chat-state-store.service.ts
+++ b/src/engine/adapters/baileys-chat-state-store.service.ts
@@ -106,6 +106,8 @@ export function mergeTwinStates(rows: ChatStateValue[]): ChatStateValue | undefi
return out;
}
+type SessionWrites = { pending: Set<Promise<void>>; seq: number };
+
const SEP = '\u0000'; // a null byte never appears in a session name or JID, so the join cannot collide
// One global LRU across all sessions, default 5000, matching the other engine maps. A
@@ -147,6 +149,11 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
private readonly completeSessions = new Set<string>();
/** Bumped by every write that indexes or persists, so a load can tell a write overlapped its read. */
private writeSeq = 0;
+ /**
+ * Per session: its writes queued or running, and a count bumped like {@link writeSeq} by its own
+ * writes only, so a refresh is not held up by another session's traffic.
+ */
+ private readonly sessionWrites = new Map<string, SessionWrites>();
private warnedEviction = false;
private readonly maxEntries: number;
/** One write chain per chat, so each remember() merges onto the state the previous one left. */
@@ -165,6 +172,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
async reload(): Promise<void> {
const seq = this.writeSeq;
+ const idle = this.writes.size === 0;
try {
const rows = await this.repo.find({
order: { updatedAt: 'DESC' },
@@ -178,7 +186,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
for (const row of [...rows].reverse()) {
this.index(this.key(row.sessionId, row.chatId), fromRow(row));
}
- if (this.loadWasWhole(rows.length, seq)) {
+ if (this.loadWasWhole(rows.length, idle && this.writeSeq === seq && this.writes.size === 0)) {
for (const row of rows) this.completeSessions.add(row.sessionId);
}
this.logger.log(
@@ -212,9 +220,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
*/
remember(sessionId: string, chatId: string, patch: Partial<ChatStateValue>, create = true): Promise<void> {
const k = this.key(sessionId, chatId);
- return new Promise<void>((resolve, reject) =>
- this.writes.enqueue(k, () => this.applyPatch(k, sessionId, chatId, patch, create).then(() => resolve(), reject)),
- );
+ return this.enqueue(sessionId, k, async () => {
+ await this.applyPatch(k, sessionId, chatId, patch, create);
+ });
}
fold(
@@ -225,9 +233,31 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
create = true,
): Promise<void> {
const k = this.key(sessionId, chatId);
- return new Promise<void>((resolve, reject) =>
- this.writes.enqueue(k, () => this.applyFold(k, sessionId, chatId, twins, patch, create).then(resolve, reject)),
- );
+ return this.enqueue(sessionId, k, () => this.applyFold(k, sessionId, chatId, twins, patch, create));
+ }
+
+ /** Queues work on the chat's write chain and counts it under the session until it settles. */
+ private enqueue(sessionId: string, k: string, work: () => Promise<void>): Promise<void> {
+ const { pending } = this.writesOf(sessionId);
+ const done = new Promise<void>((resolve, reject) => this.writes.enqueue(k, () => work().then(resolve, reject)));
+ pending.add(done);
+ const settle = () => pending.delete(done);
+ done.then(settle, settle);
+ return done;
+ }
+
+ private writesOf(sessionId: string): SessionWrites {
+ let writes = this.sessionWrites.get(sessionId);
+ if (!writes) {
+ writes = { pending: new Set(), seq: 0 };
+ this.sessionWrites.set(sessionId, writes);
+ }
+ return writes;
+ }
+
+ private bumpWriteSeq(sessionId: string): void {
+ this.writeSeq++;
+ this.writesOf(sessionId).seq++;
}
/**
@@ -308,7 +338,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
row = await this.repo.findOne({ where: { sessionId, chatId } });
} catch {
// Persisted but not indexed, so the session's cache no longer holds all of its rows.
- this.writeSeq++;
+ this.bumpWriteSeq(sessionId);
this.completeSessions.delete(sessionId);
const ok = await this.persistBlind(sessionId, chatId, patch, !restatesDefaults);
this.absent.delete(k);
@@ -336,7 +366,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
}
const at = new Date();
next.updatedAt = at.getTime();
- this.writeSeq++;
+ this.bumpWriteSeq(sessionId);
this.index(k, next);
return this.persist(sessionId, chatId, next, at);
}
@@ -413,20 +443,17 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
const k = this.key(sessionId, chatId);
this.states.delete(k);
this.markAbsent(k);
- return new Promise<void>(resolve =>
- this.writes.enqueue(k, async () => {
- try {
- await this.repo.delete({ sessionId, chatId });
- this.markAbsent(k);
- } catch (err) {
- this.logger.warn(
- `Failed to forget chat state for ${chatId}: ${err instanceof Error ? err.message : String(err)}`,
- );
- }
- this.states.delete(k);
- resolve();
- }),
- );
+ return this.enqueue(sessionId, k, async () => {
+ try {
+ await this.repo.delete({ sessionId, chatId });
+ this.markAbsent(k);
+ } catch (err) {
+ this.logger.warn(
+ `Failed to forget chat state for ${chatId}: ${err instanceof Error ? err.message : String(err)}`,
+ );
+ }
+ this.states.delete(k);
+ });
}),
);
}
@@ -441,7 +468,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
*/
async refreshSession(sessionId: string): Promise<void> {
const prefix = `${sessionId}${SEP}`;
- const seq = this.writeSeq;
+ const writes = this.writesOf(sessionId);
+ const seq = writes.seq;
+ const idle = writes.pending.size === 0;
this.completeSessions.delete(sessionId);
let rows: ChatState[] | undefined;
try {
@@ -467,16 +496,20 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {
this.index(this.key(sessionId, row.chatId), fromRow(row));
}
// Marked after the loop: indexing these rows can evict other sessions' keys, never this one's.
- if (this.loadWasWhole(rows.length, seq)) this.completeSessions.add(sessionId);
+ // Only this session's writes can leave its keys out of the load, so another session's do not count.
+ if (this.loadWasWhole(rows.length, idle && writes.seq === seq && writes.pending.size === 0)) {
+ this.completeSessions.add(sessionId);
+ }
}
/**
- * True when a load holds every row it asked for and no write ran meanwhile. A write in flight during
- * the read may be missing from `rows` while its key was just dropped from the cache, so the session
- * cannot be vouched for; skipping the mark only keeps the read-through path.
+ * True when a load holds every row it asked for and no write was in flight from the start of its read
+ * to the end (`quiet`). A write in flight during the read may be missing from `rows` while its key was
+ * just dropped from the cache, even one that finished before the read did, so the session cannot be
+ * vouched for; skipping the mark only keeps the read-through path.
*/
- private loadWasWhole(count: number, seq: number): boolean {
- return (this.maxEntries === 0 || count < this.maxEntries) && this.writeSeq === seq && this.writes.size === 0;
+ private loadWasWhole(count: number, quiet: boolean): boolean {
+ return quiet && (this.maxEntries === 0 || count < this.maxEntries);
}
/** Warm a cache miss from the table. This lookup still returns undefined (the read cannot await); the next hits. */