Skip to content

Commit 94d6774

Browse files
fix(history-sync): reset cumulative counters on new sync start and abort
Detect new sync runs by tracking lastProgress — when progress resets or decreases, counters are zeroed before accumulating. This prevents stale counts from aborted syncs leaking into subsequent runs. Addresses Sourcery review feedback on PR EvolutionAPI#2442.
1 parent f6ba879 commit 94d6774

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ export class BaileysStartupService extends ChannelStartupService {
252252
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
253253
private eventProcessingQueue: Promise<void> = Promise.resolve();
254254

255-
// Cumulative history sync counters (reset on sync completion)
255+
// Cumulative history sync counters (reset on new sync or completion)
256256
private historySyncMessageCount = 0;
257257
private historySyncChatCount = 0;
258258
private historySyncContactCount = 0;
259+
private historySyncLastProgress = -1;
259260

260261
// Cache TTL constants (in seconds)
261262
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
@@ -949,6 +950,14 @@ export class BaileysStartupService extends ChannelStartupService {
949950
syncType?: proto.HistorySync.HistorySyncType;
950951
}) => {
951952
try {
953+
// Reset counters when a new sync starts (progress resets or decreases)
954+
if (progress <= this.historySyncLastProgress) {
955+
this.historySyncMessageCount = 0;
956+
this.historySyncChatCount = 0;
957+
this.historySyncContactCount = 0;
958+
}
959+
this.historySyncLastProgress = progress ?? -1;
960+
952961
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
953962
console.log('received on-demand history sync, messages=', messages);
954963
}
@@ -1097,6 +1106,7 @@ export class BaileysStartupService extends ChannelStartupService {
10971106
this.historySyncMessageCount = 0;
10981107
this.historySyncChatCount = 0;
10991108
this.historySyncContactCount = 0;
1109+
this.historySyncLastProgress = -1;
11001110
}
11011111

11021112
contacts = undefined;

0 commit comments

Comments
 (0)