Skip to content

fix(whatsapp): add timeout to fetchLatestBaileysVersion to prevent permanent disconnect wedge - #77298

Closed
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/whatsapp-reconnect-hang
Closed

fix(whatsapp): add timeout to fetchLatestBaileysVersion to prevent permanent disconnect wedge#77298
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/whatsapp-reconnect-hang

Conversation

@JonthanaHanh

Copy link
Copy Markdown
Contributor

Summary

Fixes #77268

The WhatsApp bridge can get permanently disconnected after a server-side disconnect (stream:error 503) when the reconnect path calls fetchLatestBaileysVersion() — a plain fetch() to raw.githubusercontent.com with no timeout. If the connection stalls, the fetch hangs forever. The bridge logs "Reconnecting in 3s..." once then goes silent; the Express server keeps serving 503 to the gateway indefinitely (observed for 27+ hours in the field).

Root Cause

Two issues in scripts/whatsapp-bridge/bridge.js:

  1. fetchLatestBaileysVersion() has no timeout — it's a plain fetch() to raw.githubusercontent.com that can hang indefinitely on network stalls. No AbortSignal is passed and no Promise.race wraps it.

  2. setTimeout(startSocket, 3000) creates an unhandled promise rejectionstartSocket() is async, so calling it from setTimeout returns a promise that's never .catch()-ed. On modern Node.js, unhandled rejections are fatal.

Changes

  • Add fetchWithTimeout() helper that races an async function against a configurable timeout (default 15s, overridable via WHATSAPP_VERSION_FETCH_TIMEOUT_MS env var)
  • Wrap fetchLatestBaileysVersion() in fetchWithTimeout() so stalled version fetches fail fast instead of hanging forever
  • Wrap all startSocket() call sites with .catch() to prevent unhandled promise rejections (fatal on modern Node.js)

Test Plan

  • node --check scripts/whatsapp-bridge/bridge.js passes
  • Verified reconnect path handles timeout gracefully (manual test with simulated network stall)

…rmanent disconnect wedge

The WhatsApp bridge can get permanently disconnected after a server-side
disconnect (stream:error 503) when the reconnect path calls
`fetchLatestBaileysVersion()` — a plain fetch() to raw.githubusercontent.com
with no timeout. If the connection stalls, the fetch hangs forever. The bridge
logs "Reconnecting in 3s..." once then goes silent; the Express server keeps
serving 503 to the gateway indefinitely.

Fixes NousResearch#77268

Changes:
- Add `fetchWithTimeout()` helper that races an async function against a
  configurable timeout (default 15s, overridable via
  WHATSAPP_VERSION_FETCH_TIMEOUT_MS env var)
- Wrap `fetchLatestBaileysVersion()` in `fetchWithTimeout()` so stalled
  version fetches fail fast instead of hanging forever
- Wrap all `startSocket()` call sites with `.catch()` to prevent unhandled
  promise rejections (fatal on modern Node.js)
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as duplicate of #77337 (salvage of #77270 by the same author @ATran28). The salvage PR is the more complete version — includes cached version fallback, reconnect error handling with retry, and 6 regression tests. Your fetchWithTimeout approach was simpler but added a WHATSAPP_VERSION_FETCH_TIMEOUT_MS env var which conflicts with the project's .env-is-for-secrets-only policy. Thanks for the contribution!

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/whatsapp WhatsApp Business adapter labels Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/whatsapp WhatsApp Business adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WhatsApp bridge wedges permanently disconnected if reconnect's version fetch hangs

3 participants