-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Bug Fix: QR Code Infinite Reconnection Loop #2365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts Baileys WhatsApp connection handling to avoid reconnecting during the very first, unauthenticated connection so QR codes can be generated, while preserving existing reconnection behavior for authenticated/normal sessions. Sequence diagram for updated Baileys connection close handlingsequenceDiagram
participant BaileysSocket as BaileysSocket
participant BaileysStartupService as BaileysStartupService
participant WhatsappAPI as WhatsappAPI
BaileysSocket->>BaileysStartupService: connectionUpdate(connection close, lastDisconnect)
activate BaileysStartupService
BaileysStartupService->>BaileysStartupService: read lastDisconnect.error.statusCode
BaileysStartupService->>BaileysStartupService: compute isInitialConnection = !instance.wuid && instance.qrcode.count == 0
alt initial_unauthenticated_connection
BaileysStartupService->>BaileysStartupService: logger.info(Initial connection closed, waiting for QR code generation...)
BaileysStartupService-->>BaileysSocket: return (no reconnect)
else authenticated_or_qr_generated
BaileysStartupService->>BaileysStartupService: compute shouldReconnect based on statusCode
alt shouldReconnect
BaileysStartupService->>BaileysStartupService: logger.warn(Connection lost, reconnecting...)
BaileysStartupService->>WhatsappAPI: connectToWhatsapp(phoneNumber)
WhatsappAPI-->>BaileysStartupService: connection established or closed
else shouldNotReconnect
BaileysStartupService->>BaileysStartupService: sendDataWebhook(STATUS_INSTANCE, disconnected)
end
end
deactivate BaileysStartupService
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 1 issue, and left some high level feedback:
- The
isInitialConnectioncheck assumesthis.instance.qrcodeis always defined; consider using optional chaining or a default (this.instance.qrcode?.count ?? 0) to avoid potential runtime errors ifqrcodeis undefined on some paths. - The new inline comments are in Portuguese while surrounding code and logs are in English; consider standardizing the comment language to match the rest of the file for consistency.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `isInitialConnection` check assumes `this.instance.qrcode` is always defined; consider using optional chaining or a default (`this.instance.qrcode?.count ?? 0`) to avoid potential runtime errors if `qrcode` is undefined on some paths.
- The new inline comments are in Portuguese while surrounding code and logs are in English; consider standardizing the comment language to match the rest of the file for consistency.
## Individual Comments
### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:441` </location>
<code_context>
+
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
if (shouldReconnect) {
+ this.logger.warn(`Connection lost (status: ${statusCode}), reconnecting...`);
await this.connectToWhatsapp(this.phoneNumber);
} else {
</code_context>
<issue_to_address>
**suggestion:** Consider symmetric logging when opting not to reconnect
When `shouldReconnect` is false there’s no corresponding log explaining that reconnection is being skipped and why. Please add a log in the `else` branch (e.g. info/warn) that includes the `statusCode` and mentions that reconnect is skipped because it’s in `codesToNotReconnect`, to improve debugging and correlation with webhook events.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
|
||
| const shouldReconnect = !codesToNotReconnect.includes(statusCode); | ||
| if (shouldReconnect) { | ||
| this.logger.warn(`Connection lost (status: ${statusCode}), reconnecting...`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider symmetric logging when opting not to reconnect
When shouldReconnect is false there’s no corresponding log explaining that reconnection is being skipped and why. Please add a log in the else branch (e.g. info/warn) that includes the statusCode and mentions that reconnect is skipped because it’s in codesToNotReconnect, to improve debugging and correlation with webhook events.
📋 Description
This PR fixes the infinite reconnection loop that prevents QR code generation in Evolution API versions v2.1.0, v2.1.1, and v2.2.0.
Problem:
When attempting to generate a QR code for WhatsApp connection, the Baileys integration enters an infinite reconnection loop. The
connectionUpdateevent handler triggers reconnection immediately when the connection closes (which is normal during initial QR code generation), preventing the QR code from ever being generated and displayed.Root Cause:
The issue occurs in
whatsapp.baileys.service.tswhere the connection close event triggersshouldReconnectlogic before the initial QR code is generated. SincestatusCodeisundefinedduring the first connection attempt, the code incorrectly attempts to reconnect, creating an endless loop.Solution:
Added an
isInitialConnectioncheck that prevents reconnection attempts when:!this.instance.wuid)this.instance.qrcode.count === 0)This allows the first connection to close gracefully so the QR code can be generated and sent to the client.
Changes:
connectionUpdatemethod insrc/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts(lines 421-440)🔗 Related Issue
Closes #2341
🧪 Type of Change
🧪 Testing
Test Results:
Testing Environment:
📸 Screenshots (if applicable)
Before Fix:
After Fix:
✅ Checklist
📝 Additional Notes
Impact: This is a critical bug fix that restores QR code generation functionality for new WhatsApp connections. Without this fix, users cannot establish new WhatsApp connections in affected versions.
Backward Compatibility: This fix is fully backward compatible. It only affects the initial connection flow and does not change behavior for already authenticated instances.
Code Changes Summary:
Versions Affected: v2.1.0, v2.1.1, v2.2.0
Fix Verified In: v2.3.7
Summary by Sourcery
Prevent infinite reconnection loops during initial WhatsApp QR code setup by adjusting Baileys connection handling.
Bug Fixes:
Enhancements: