Skip to content

sendTextMessage and getChats throw 500 on ready session (whatsapp-web.js) — send succeeds despite the error #757

Description

@Hossamudin

Pre-flight

OpenWA version

v0.8.17 (also reproduced on the version before this, prior to upgrading via docker compose pull && docker compose up -d)

Deployment

Docker Compose (standalone), Postgres database, ENGINE_TYPE=whatsapp-web.js (whatsapp-web.js v1.34.7, unchanged across the 0.8.17 bump)

WhatsApp engine

whatsapp-web.js

What happened?

Two related endpoints throw 500s on an authenticated, ready session, even on a completely fresh session (deleted data/sessions/session-<name> and re-scanned the QR code — this is not the stale-Chromium-profile issue from #708, which I hit and fixed separately during the same upgrade).

1. GET /api/sessions/:id/chats always 500s:

ERROR [ExceptionsHandler] r: r
    at #evaluate (puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:391:56)
    at async ExecutionContext.evaluate (.../ExecutionContext.js:277:16)
    at async IsolatedWorld.evaluate (.../IsolatedWorld.js:100:16)
    at async CdpFrame.evaluate (.../Frame.js:362:20)
    at async CdpPage.evaluate (.../Page.js:830:20)
    at async Client.getChats (whatsapp-web.js/src/Client.js:1669:23)
    at async WhatsAppWebJsAdapter.getChats (dist/engine/adapters/whatsapp-web-js.adapter.js:1372:23)
    at async SessionService.getChats (dist/modules/session/session.service.js:1012:28)

The dashboard's Chats page is unusable as a result (same 500).

2. POST /api/sessions/:id/messages/send-text 500s — but the message is actually delivered:

ERROR [ExceptionsHandler] TypeError: Cannot read properties of undefined (reading 'id')
    at WhatsAppWebJsAdapter.sendTextMessage (dist/engine/adapters/whatsapp-web-js.adapter.js:680:21)
    at async MessageService.sendText (dist/modules/message/message.service.js:62:19)

Looking at the adapter source:

async sendTextMessage(chatId, text, mentions) {
    this.ensureReady();
    const msg = await this.sendResolved(chatId, to => mentions?.length ? this.client.sendMessage(to, text, { mentions }) : this.client.sendMessage(to, text));
    return {
        id: msg.id._serialized,   // <-- throws when msg is undefined
        timestamp: msg.timestamp,
    };
}

this.client.sendMessage(...) (whatsapp-web.js) is resolving with undefined (or something missing .id) instead of a Message instance — but the underlying WhatsApp send already completes. I confirmed this by directly checking the recipient's phone across multiple sends in this session: every one that I was able to confirm the recipient's side of arrived on WhatsApp, even though the API returned {"statusCode":500,"message":"Internal server error"} for every one.

This makes the send-text success/failure response completely unreliable — callers building any kind of send-tracking on top of this API cannot tell a real failure (message never sent) from this false negative (message sent, wrapper crashed after the fact). For anything that logs delivery state to avoid duplicate sends, that ambiguity is the actually dangerous part.

Steps to reproduce

  1. Have a ready session on whatsapp-web.js engine.
  2. GET /api/sessions/:id/chats → 500, Client.getChats trace as above.
  3. POST /api/sessions/:id/messages/send-text with any valid chatId → 500, TypeError trace as above, but message arrives on the recipient's device.
  4. Deleting the session profile dir and re-scanning the QR code (full fresh session) does not change the behavior.

Root cause hypothesis

Something in WhatsApp Web's current internal Store surface has drifted from what whatsapp-web.js@1.34.7 expects — both crashes bottom out in Page.evaluate() calls into injected Store-dependent code (Client.getChats, and whatever Client.sendMessage reads internally to build its return value). This looks like the same class of problem as the pinned WhatsApp Web version (2.3000.1043220887-alpha, logged as Pinning WhatsApp Web version ... on session init) no longer matching what whatsapp-web.js@1.34.7's injected scripts assume, similar in spirit to the media-storage change referenced in #747.

Suggested fixes

  • Bump the bundled whatsapp-web.js past 1.34.7 if a newer release addresses the Store surface change (this OpenWA release didn't change that dependency version, package.json still shows 1.34.7).
  • Regardless of the underlying fix, sendTextMessage should not let a malformed/undefined return value from client.sendMessage() produce a 500 when the send call itself resolved without throwing — right now a resolved-but-shape-mismatched response is indistinguishable from a genuine failure, which is worse than just returning a best-effort result (e.g. omit id/timestamp rather than throwing) since it makes the API actively lie about delivery.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions