Skip to content

Bug report & fix for Evolution API WAMonitoringService — stream:error 515 kills healthy instances #2498

@OmarEltak

Description

@OmarEltak

In v2.2.3, when a user scans a QR code to connect WhatsApp, the connection is immediately killed ~7
seconds later by WAMonitoringService even though WhatsApp connected successfully.

Exact sequence (from Docker logs):
stream:error code=515 ← WhatsApp sends "Connection Replaced" (normal multi-device
protocol)
Baileys reconnects ← Baileys handles it correctly, reconnects in ~5s
CONNECTED TO WHATSAPP ✓ ← Session is open, webhook fires state='open'
WAMonitoringService: REMOVED ← 7 seconds later, Evolution API kills it
WAMonitoringService: LOGOUT ← Instance gone

Root cause (in src/whatsapp/services/whatsapp.baileys.service.ts):

When stream:error 515 fires, WAMonitoringService starts a removal timer. Baileys recovers and fires
connection.update state='open' — but the timer is never cancelled. After Baileys reconnects,
WhatsApp sends 401 loggedOut to clean up the old session slot. Evolution API's close handler sees
401 → triggers logout.instance → REMOVED.

The 401 after a 515 is normal WhatsApp multi-device behavior — it's not a real logout. But Evolution
API treats it as one.

The fix:

In the connection close handler (whatsapp.baileys.service.ts), track when a 515 just occurred and
treat any 401 that fires within a 30-second window as a reconnect, not a logout:

// Somewhere on the class:
private _lastStream515At = 0;

// In the stream error handler:
if (node.tag === 'stream:error' && node.attrs.code === '515') {
this._lastStream515At = Date.now();
}

// In the connection.update close handler (the existing condition):
// BEFORE:
const shouldReconnect = ![
DisconnectReason.loggedOut,
DisconnectReason.forbidden,
402,
406,
].includes(statusCode);

// AFTER:
const recentStream515 = Date.now() - this._lastStream515At < 30_000;
const shouldReconnect = ![
DisconnectReason.loggedOut,
DisconnectReason.forbidden,
402,
406,
].includes(statusCode) || (statusCode === DisconnectReason.loggedOut && recentStream515);

Environment:

  • Evolution API v2.2.3 (latest as of 2026-04-05)
  • Baileys version auto-fetched: 2.3000.1035194821
  • WhatsApp multi-device (linked device QR flow)
  • Reproducible 100% of the time on fresh QR scans

Thanks For Your Efforts❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions