Skip to content
Open

Pr 2365 #2379

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,24 @@ export class BaileysStartupService extends ChannelStartupService {
if (connection === 'close') {
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];

// FIX: Do not reconnect if it's the initial connection (waiting for QR code)
// This prevents infinite loop that blocks QR code generation
const isInitialConnection = !this.instance.wuid && (this.instance.qrcode?.count ?? 0) === 0;
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition (this.instance.qrcode?.count ?? 0) === 0 only prevents reconnection when the connection closes before the first QR code is generated. However, based on the comment "Do not reconnect if it's the initial connection (waiting for QR code)", it appears the intent is to prevent reconnection during the entire QR code phase (count can be 1 to LIMIT while waiting for the user to scan). After the first QR code is generated (count becomes 1), if the connection closes, this check will fail and reconnection will still trigger, potentially causing the infinite loop mentioned in the comment. Consider using !this.instance.wuid alone (which indicates no authenticated user) or checking if count is below the LIMIT to cover all initial connection scenarios.

Suggested change
const isInitialConnection = !this.instance.wuid && (this.instance.qrcode?.count ?? 0) === 0;
const isInitialConnection = !this.instance.wuid;

Copilot uses AI. Check for mistakes.

if (isInitialConnection) {
this.logger.info('Initial connection closed, waiting for QR code generation...');
return;
Comment on lines +432 to +436
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Early return on initial connection may skip other important close-handling logic (e.g., status notifications).

Because this returns early when isInitialConnection is true, none of the later close-handling logic runs (including the STATUS_INSTANCE webhook in the non-reconnect path). If callers expect that status even on initial connection close, this can break monitoring/state tracking. You might instead still emit the appropriate status event here, or refactor so only the reconnect loop is skipped while other close-handling remains consistent.

}

const shouldReconnect = !codesToNotReconnect.includes(statusCode);
if (shouldReconnect) {
this.logger.warn(`Connection lost (status: ${statusCode}), reconnecting...`);
await this.connectToWhatsapp(this.phoneNumber);
} else {
this.logger.info(
`Skipping reconnection for status code ${statusCode} (code is in codesToNotReconnect list)`,
);
this.sendDataWebhook(Events.STATUS_INSTANCE, {
Comment on lines 426 to 447
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description is incomplete - it only contains the template without filling in the actual details. Please provide a description of what issue this PR is addressing, what the root cause was, and how this fix resolves it. This will help reviewers understand the context and validate that the solution is correct.

Copilot uses AI. Check for mistakes.
instance: this.instance.name,
status: 'closed',
Expand Down
Loading