From 382c1a7e94a07dd1085ee0ad6661fdf1c14cb4e1 Mon Sep 17 00:00:00 2001 From: devlikepro Date: Fri, 14 Aug 2026 18:40:31 +0700 Subject: [PATCH] fix(WEBJS): phantom AUTHENTICATED/READY on logout and onNewMessageId binding race - fix #2222 --- src/core/engines/webjs/WebjsClientCore.ts | 27 ++++++++++++++------ src/core/engines/webjs/session.webjs.core.ts | 6 ++++- yarn.lock | 4 +-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/core/engines/webjs/WebjsClientCore.ts b/src/core/engines/webjs/WebjsClientCore.ts index 7d7ec61d..c058312f 100644 --- a/src/core/engines/webjs/WebjsClientCore.ts +++ b/src/core/engines/webjs/WebjsClientCore.ts @@ -8,6 +8,7 @@ import { TextStatus } from '@waha/structures/status.dto'; import { sleep } from '@waha/utils/promiseTimeout'; import { EventEmitter } from 'events'; import * as lodash from 'lodash'; +import { Logger } from 'pino'; import { Page } from 'puppeteer'; import { Client, Events, Message as WebjsMessage } from 'whatsapp-web.js'; import { Message } from 'whatsapp-web.js/src/structures'; @@ -93,21 +94,31 @@ function extractReactionsByMessageKey( export class WebjsClientCore extends Client { public events = new EventEmitter(); private wpage: WPage = null; + private injecting: Promise = null; constructor( options, protected tags: boolean, + protected logger: Logger, ) { super(options); // Wait until it's READY and inject more utils - this.on(Events.AUTHENTICATED, async () => { - await this.attachCustomEventListeners(); - await this.injectWaha(); - }); - this.on(Events.READY, async () => { - await this.attachCustomEventListeners(); - await this.injectWaha(); - }); + // AUTHENTICATED and READY fire back to back - run one injection for both + this.on(Events.AUTHENTICATED, () => this.injectUtils()); + this.on(Events.READY, () => this.injectUtils()); + } + + private injectUtils(): Promise { + if (this.injecting) { + return this.injecting; + } + this.injecting = this.attachCustomEventListeners() + .then(() => this.injectWaha()) + .catch((err) => this.logger.error(err, 'Failed to inject utils')) + .finally(() => { + this.injecting = null; + }); + return this.injecting; } async initialize() { diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 71b554e3..1715015a 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -326,7 +326,11 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { const clientOptions = this.getClientOptions(); clientOptions.authStrategy = authStrategy; this.addProxyConfig(clientOptions); - return new WebjsClientCore(clientOptions, this.getWebjsTagsFlag()); + return new WebjsClientCore( + clientOptions, + this.getWebjsTagsFlag(), + this.logger, + ); } protected getWebjsTagsFlag() { diff --git a/yarn.lock b/yarn.lock index 753c463a..c782b783 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14350,7 +14350,7 @@ __metadata: "whatsapp-web.js@github:devlikeapro/whatsapp-web.js#fork-main-2026-06-26": version: 1.34.7 - resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=b53e8e76b571d1987fcc07353de15ad0b45ff4e9" + resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=62174b15ce610ec545855fba8682485568dfb60f" dependencies: archiver: "npm:7.0.1" fluent-ffmpeg: "npm:2.1.3" @@ -14367,7 +14367,7 @@ __metadata: optional: true unzipper: optional: true - checksum: 10/058158e942a61e8feb65a240a472352a930f1998d9a24b8de29889113c95c35f9361d2f2ce3c11806297a67e64c6a45170540164cacb72ce697986d60b40eba1 + checksum: 10/5ad76a46a5897645f1cdba2a89613b8f11302d5c58af6a1a1ca588ece6ec65c98f16fd09506545f4120ed857f520dce056515dab008cf99cbe025d07e2e95515 languageName: node linkType: hard