Skip to content

Comments

Improve disconnect reason handling for WhatsApp session stability#2367

Open
kobie3717 wants to merge 1 commit intoWhiskeySockets:masterfrom
kobie3717:fix/disconnect-reason-handling
Open

Improve disconnect reason handling for WhatsApp session stability#2367
kobie3717 wants to merge 1 commit intoWhiskeySockets:masterfrom
kobie3717:fix/disconnect-reason-handling

Conversation

@kobie3717
Copy link

@kobie3717 kobie3717 commented Feb 24, 2026

Problem

This PR addresses disconnect reason handling issues that lead to Bad MAC / MessageCounterError after reconnection cycles when session state gets out of sync.

Root Cause

  • The DisconnectReason enum was missing several error codes that WhatsApp sends (e.g., 405)
  • Unknown disconnect reasons fall through to reconnect logic, wasting retries on dead sessions
  • No session cleanup on certain error codes that indicate stale/corrupt sessions

Changes Made

1. Extended DisconnectReason enum

  • Added methodNotAllowed = 405 — indicates stale/corrupt session
  • Added conflict = 409 — session conflict
  • Added preconditionFailed = 412 — session state mismatch

2. Enhanced CB:failure handler in socket.ts

  • Proper error classification based on disconnect reason codes
  • Clear authentication state for terminal errors (405, 409, 412, etc.)
  • Detailed logging for unknown disconnect reasons with warning level
  • Prevent wasteful reconnection attempts on corrupted sessions

3. Session State Management

  • Clear critical session data (creds.me, creds.platform) for terminal errors
  • Better error messages indicating whether auth was cleared

Error Code Classifications

  • Terminal errors (clear auth): 401, 403, 405, 409, 412, 440
  • Retryable errors: 411, 515
  • Unknown codes: Default to badSession with warning logging

Impact

  • Reduces Bad MAC / MessageCounterError occurrences after reconnect cycles
  • Prevents infinite reconnection loops on corrupted sessions
  • Better observability through improved error logging
  • Maintains backward compatibility

- Add missing disconnect reason codes: methodNotAllowed (405), conflict (409), preconditionFailed (412)
- Enhance CB:failure handler with proper error classification
- Clear authentication state for terminal errors that indicate session corruption
- Add detailed logging for unknown disconnect reasons
- Prevent wasteful reconnection attempts on stale/corrupt sessions

Fixes issues with Bad MAC / MessageCounterError after reconnection cycles
by properly handling disconnect codes that indicate session state corruption.

Related issues: WhiskeySockets#2340, WhiskeySockets#2321, WhiskeySockets#2362
@whiskeysockets-bot
Copy link
Contributor

Thanks for opening this pull request and contributing to the project!

The next step is for the maintainers to review your changes. If everything looks good, it will be approved and merged into the main branch.

In the meantime, anyone in the community is encouraged to test this pull request and provide feedback.

✅ How to confirm it works

If you’ve tested this PR, please comment below with:

Tested and working ✅

This helps us speed up the review and merge process.

📦 To test this PR locally:

# NPM
npm install @whiskeysockets/baileys@kobie3717/Baileys#fix/disconnect-reason-handling

# Yarn (v2+)
yarn add @whiskeysockets/baileys@kobie3717/Baileys#fix/disconnect-reason-handling

# PNPM
pnpm add @whiskeysockets/baileys@kobie3717/Baileys#fix/disconnect-reason-handling

If you encounter any issues or have feedback, feel free to comment as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants