Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): report a phone-sent message once, and never drop one

Yudhi Armyndharisd092c8017b0e
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index b83b21701..a9abd1b10 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -100,6 +100,7 @@ function makeHost(overrides: Partial<BaileysEventsHost> = {}): BaileysEventsHost
recordMessage: noop,
recordMessageEdit: noop,
consumeOwnSend: () => false,
+ rememberOwnSend: noop,
getStoredMessage: () => undefined,
putStoredMessage: () => undefined,
getOnMessage: () => undefined,
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index 83a14f15c..71b1e2838 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -119,6 +119,8 @@ export interface BaileysEventsHost {
* is arriving; false for anything the session did not send (see OwnSendRegistry).
*/
consumeOwnSend(id: string | null | undefined): boolean;
+ /** Claim an id for {@link consumeOwnSend}, so the next delivery carrying it is taken as a repeat. */
+ rememberOwnSend(id: string | null | undefined): void;
/** A message this session already delivered or sent, from the persistent store; undefined without a store. */
getStoredMessage(messageId: string): Promise<WAMessage | null> | undefined;
/** The currently-registered onMessage callback, if any (assigned at initialize()). */
@@ -176,12 +178,23 @@ export class BaileysEvents {
// a stored message again. That oracle does NOT gate dispatch on the own-send path, which is
// why the echo has to be caught here, and why a fromMe message the store already holds is
// dropped in processInboundMessage before it can be reported a second time.
- if (msg.key.fromMe === true && this.host.consumeOwnSend(msg.key.id)) {
- this.host.logger.debug('Skipping the echo of a message this session sent', {
- msgId: msg.key.id ?? 'unknown',
- type: event.type,
- });
- continue;
+ //
+ // One gate catches both repeats. `consume` is true for the echo of a send this session made
+ // through the API, whose id the send path recorded, and for a second delivery of a fromMe
+ // message this handler has already taken, because it claims the id below. Claiming is what
+ // makes the second case work: the store check in processInboundMessage sits behind an await
+ // and can only see ids the store has committed, so two deliveries landing inside that window
+ // would both be reported. This runs before any await, so they cannot.
+ if (msg.key.fromMe === true) {
+ const alreadyTaken = this.host.consumeOwnSend(msg.key.id);
+ this.host.rememberOwnSend(msg.key.id);
+ if (alreadyTaken) {
+ this.host.logger.debug('Skipping a message this session has already reported', {
+ msgId: msg.key.id ?? 'unknown',
+ type: event.type,
+ });
+ continue;
+ }
}
// Throttle through the limiter so a burst of media messages can't run unbounded parallel
// downloads (each a full decrypted buffer in heap). Ordering stays correct — the message store
@@ -356,10 +369,11 @@ export class BaileysEvents {
// a node whose ack was lost on a drop, and the own-send path downstream dispatches message.sent
// whatever its insert did, so the second copy has to stop here. The store is written by both
// the inbound path below and the send path, and it survives a restart, which the registry
- // consulted in handleMessagesUpsert does not.
- if (msg.key.fromMe === true && msg.key.id && (await this.host.getStoredMessage(msg.key.id))) {
+ // consulted in handleMessagesUpsert does not. The read fails open (see readStoredMessage).
+ const ownMessageId = msg.key.fromMe === true ? (msg.key.id ?? null) : null;
+ if (ownMessageId !== null && (await this.readStoredMessage(ownMessageId))) {
this.host.logger.debug('Skipping a re-delivered message this session already recorded', {
- msgId: msg.key.id,
+ msgId: ownMessageId,
});
return;
}
@@ -383,6 +397,28 @@ export class BaileysEvents {
}
}
+ /**
+ * The stored copy of a message id, or null when the store cannot answer.
+ *
+ * Deliberately fail-open: a locked database or a row whose JSON no longer parses must not be read
+ * as "this was never sent". The caller's only other outcome is the catch above, which drops the
+ * message outright, and Baileys acks the node before it emits the upsert, so WhatsApp does not
+ * send it again. A repeat is recoverable downstream (same idempotency key on the webhook, and the
+ * insert oracle holds the row to one); a message nobody ever hears about is not. The persist side
+ * of the same store is already written this way.
+ */
+ private async readStoredMessage(messageId: string): Promise<WAMessage | null> {
+ try {
+ return (await this.host.getStoredMessage(messageId)) ?? null;
+ } catch (err) {
+ this.host.logger.warn('Could not read the message store while checking for a repeat delivery', {
+ msgId: messageId,
+ error: err instanceof Error ? err.message : String(err),
+ });
+ return null;
+ }
+ }
+
handleMessagesUpdate(updates: Array<{ key?: { id?: string | null }; update?: { status?: number | null } }>): void {
for (const u of updates) {
const status = mapBaileysStatus(u.update?.status);
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 2026896b9..1d066f419 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -2419,6 +2419,53 @@ describe('BaileysAdapter inbound fan-out', () => {
expect(onMessageCreate).toHaveBeenCalledTimes(1);
});
+ // The store write is fire-and-forget and only issued after the dispatch, so two deliveries of one
+ // id landing before it commits both read an empty store. The id is claimed synchronously when the
+ // batch is taken, which is the only point early enough to catch them.
+ it('does not dispatch a phone-sent message twice when both deliveries land before the store commits', async () => {
+ const replayed = {
+ key: { remoteJid: '[email protected]', fromMe: true, id: 'TYPED_ON_PHONE' },
+ message: { conversation: 'replied from the phone during the outage' },
+ messageTimestamp: Math.floor(Date.now() / 1000) - 3600,
+ };
+ const onMessageCreate = jest.fn();
+ const adapter = newAdapter();
+ await adapter.initialize({ onMessageCreate });
+ fakeSock.fire('connection.update', { connection: 'open' });
+
+ // Both in the same tick: nothing has been written to the store in between.
+ fakeSock.fire('messages.upsert', { type: 'append', messages: [replayed] });
+ fakeSock.fire('messages.upsert', { type: 'append', messages: [replayed] });
+ await new Promise(r => setImmediate(r));
+
+ expect(onMessageCreate).toHaveBeenCalledTimes(1);
+ });
+
+ // A store that cannot answer must not be read as "this was never sent": the only other outcome is
+ // the handler's catch, which drops the message, and Baileys acks the node before emitting it, so
+ // WhatsApp never sends it again. Fail open, and take the duplicate risk instead of the loss.
+ it('still delivers a phone-sent message when the message store read fails', async () => {
+ fakeStore.getMessage.mockRejectedValueOnce(new Error('SQLITE_BUSY: database is locked'));
+ const onMessageCreate = jest.fn();
+ const adapter = newAdapter();
+ await adapter.initialize({ onMessageCreate });
+ fakeSock.fire('connection.update', { connection: 'open' });
+
+ fakeSock.fire('messages.upsert', {
+ type: 'append',
+ messages: [
+ {
+ key: { remoteJid: '[email protected]', fromMe: true, id: 'TYPED_WHILE_DB_BUSY' },
+ message: { conversation: 'sent from the phone while the database was locked' },
+ messageTimestamp: Math.floor(Date.now() / 1000) - 60,
+ },
+ ],
+ });
+ await new Promise(r => setImmediate(r));
+
+ expect(onMessageCreate).toHaveBeenCalledTimes(1);
+ });
+
it('emits onMessageAck from messages.update with a neutral status', async () => {
const onMessageAck = jest.fn();
const adapter = newAdapter();