Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): stop the status backfill when its session is deleted

Yudhi Armyndharis1744a98958b2
diff --git a/src/modules/session/session-content-lifetime.spec.ts b/src/modules/session/session-content-lifetime.spec.ts
index 5834c6da9..9a5daa1c7 100644
--- a/src/modules/session/session-content-lifetime.spec.ts
+++ b/src/modules/session/session-content-lifetime.spec.ts
@@ -27,7 +27,7 @@ import { SendPacingService } from '../message/send-pacing.service';
import { StatusStoreService } from '../status-store/status-store.service';
import { StorageService } from '../../common/storage/storage.service';
import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';
-import { captureSessionWrites } from '../../common/utils/session-write-scope';
+import { captureSessionWrites, closeSessionWrites } from '../../common/utils/session-write-scope';
function deferred<T = void>() {
let resolve!: (value: T | PromiseLike<T>) => void;
@@ -276,6 +276,48 @@ describe('Session content lifetime', () => {
await seed;
expect(await db.getRepository(StatusUpdate).count()).toBe(0);
});
+
+ it('stops the status seed at the first item after its session is retired', async () => {
+ const session = await createSession();
+ const id = session.id;
+ const history = Array.from({ length: 200 }, (_, index) => ({
+ id: `story-${index}`,
+ isStatusBroadcast: true,
+ author: '[email protected]',
+ from: 'status@broadcast',
+ fromMe: false,
+ type: 'text',
+ body: `story ${index}`,
+ timestamp: Math.floor(Date.now() / 1000),
+ }));
+ const engine = {
+ getChatHistory: () => Promise.resolve(history),
+ getContactById: jest.fn().mockResolvedValue(null),
+ };
+ const store = new StatusStoreService(db.getRepository(StatusUpdate), {} as StorageService, config);
+ const ingest = store.ingest.bind(store);
+ let deletion: ReturnType<typeof closeSessionWrites> | undefined;
+ const ingested = jest.spyOn(store, 'ingest').mockImplementation(async (...args) => {
+ const stored = await ingest(...args);
+ deletion ??= closeSessionWrites(db, id);
+ return stored;
+ });
+ const warn = jest.fn();
+ const leaf = new SessionEngineLeafEvents({
+ sessionRepository: db.getRepository(Session),
+ statusStore: store,
+ configService: config,
+ logger: { ...logger, warn },
+ } as unknown as ConstructorParameters<typeof SessionEngineLeafEvents>[0]);
+
+ await leaf.seedStatuses(id, engine as unknown as IWhatsAppEngine);
+ deletion?.release();
+
+ expect(ingested).toHaveBeenCalledTimes(1);
+ expect(warn).not.toHaveBeenCalled();
+ expect(await db.getRepository(StatusUpdate).count()).toBe(1);
+ });
+
it('refuses a pre-send continuation after the session id was replaced', async () => {
const session = await createSession();
const id = session.id;
diff --git a/src/modules/session/session-engine-leaf-events.ts b/src/modules/session/session-engine-leaf-events.ts
index 0b20d079b..b33ff0499 100644
--- a/src/modules/session/session-engine-leaf-events.ts
+++ b/src/modules/session/session-engine-leaf-events.ts
@@ -94,6 +94,9 @@ export class SessionEngineLeafEvents {
return resolved;
};
for (const msg of messages) {
+ // Outside the per-item try: a deleted session ends the backfill, where every later item would
+ // only read the store and fail the same way.
+ lifetime.assertCurrent();
try {
// Mirrors the live path's own-send drop: the broadcast chat's history also contains the
// account's OWN active statuses, which must not come back as if a contact had posted them.