@@ -249,6 +249,7 @@ export class BaileysStartupService extends ChannelStartupService {
249249 private readonly msgRetryCounterCache : CacheStore = new NodeCache ( ) ;
250250 private readonly userDevicesCache : CacheStore = new NodeCache ( { stdTTL : 300000 , useClones : false } ) ;
251251 private endSession = false ;
252+ private isDeleting = false ; // Flag to prevent reconnection during deletion
252253 private logBaileys = this . configService . get < Log > ( 'LOG' ) . BAILEYS ;
253254 private eventProcessingQueue : Promise < void > = Promise . resolve ( ) ;
254255
@@ -265,10 +266,27 @@ export class BaileysStartupService extends ChannelStartupService {
265266 }
266267
267268 public async logoutInstance ( ) {
269+ // Mark instance as deleting to prevent reconnection attempts
270+ this . isDeleting = true ;
271+ this . endSession = true ;
272+
268273 this . messageProcessor . onDestroy ( ) ;
269- await this . client ?. logout ( 'Log out instance: ' + this . instanceName ) ;
270274
271- this . client ?. ws ?. close ( ) ;
275+ if ( this . client ) {
276+ try {
277+ await this . client . logout ( 'Log out instance: ' + this . instanceName ) ;
278+ } catch ( error ) {
279+ this . logger . error ( { message : 'Error during logout' , error } ) ;
280+ }
281+
282+ // Improved socket cleanup
283+ try {
284+ this . client . ws ?. close ( ) ;
285+ this . client . end ( new Error ( 'Instance logout' ) ) ;
286+ } catch ( error ) {
287+ this . logger . error ( { message : 'Error during socket cleanup' , error } ) ;
288+ }
289+ }
272290
273291 const db = this . configService . get < Database > ( 'DATABASE' ) ;
274292 const cache = this . configService . get < CacheConf > ( 'CACHE' ) ;
@@ -332,6 +350,18 @@ export class BaileysStartupService extends ChannelStartupService {
332350 }
333351
334352 private async connectionUpdate ( { qr, connection, lastDisconnect } : Partial < ConnectionState > ) {
353+ // Enhanced logging for connection updates
354+ const statusCode = ( lastDisconnect ?. error as Boom ) ?. output ?. statusCode ;
355+ this . logger . info ( {
356+ message : 'Connection update received' ,
357+ connection,
358+ hasQr : ! ! qr ,
359+ statusCode,
360+ instanceName : this . instance . name ,
361+ isDeleting : this . isDeleting ,
362+ endSession : this . endSession ,
363+ } ) ;
364+
335365 if ( qr ) {
336366 if ( this . instance . qrcode . count === this . configService . get < QrCode > ( 'QRCODE' ) . LIMIT ) {
337367 this . sendDataWebhook ( Events . QRCODE_UPDATED , {
@@ -424,11 +454,29 @@ export class BaileysStartupService extends ChannelStartupService {
424454 }
425455
426456 if ( connection === 'close' ) {
457+ // Check if instance is being deleted or session is ending
458+ if ( this . isDeleting || this . endSession ) {
459+ this . logger . info ( 'Instance is being deleted/ended, skipping reconnection attempt' ) ;
460+ return ;
461+ }
462+
427463 const statusCode = ( lastDisconnect ?. error as Boom ) ?. output ?. statusCode ;
428464 const codesToNotReconnect = [ DisconnectReason . loggedOut , DisconnectReason . forbidden , 402 , 406 ] ;
429465 const shouldReconnect = ! codesToNotReconnect . includes ( statusCode ) ;
466+
467+ this . logger . info ( {
468+ message : 'Connection closed, evaluating reconnection' ,
469+ statusCode,
470+ shouldReconnect,
471+ instanceName : this . instance . name ,
472+ } ) ;
473+
430474 if ( shouldReconnect ) {
431- await this . connectToWhatsapp ( this . phoneNumber ) ;
475+ // Add 3 second delay before reconnection to prevent rapid reconnection loops
476+ this . logger . info ( 'Reconnecting in 3 seconds...' ) ;
477+ setTimeout ( async ( ) => {
478+ await this . connectToWhatsapp ( this . phoneNumber ) ;
479+ } , 3000 ) ;
432480 } else {
433481 this . sendDataWebhook ( Events . STATUS_INSTANCE , {
434482 instance : this . instance . name ,
@@ -591,18 +639,19 @@ export class BaileysStartupService extends ChannelStartupService {
591639 this . logger . info ( `Browser: ${ browser } ` ) ;
592640 }
593641
642+ // Fetch latest WhatsApp Web version automatically
594643 const baileysVersion = await fetchLatestWaWebVersion ( { } ) ;
595644 const version = baileysVersion . version ;
596- const log = `Baileys version: ${ version . join ( '.' ) } ` ;
597645
646+ const log = `Baileys version: ${ version . join ( '.' ) } ` ;
598647 this . logger . info ( log ) ;
599648
600649 this . logger . info ( `Group Ignore: ${ this . localSettings . groupsIgnore } ` ) ;
601650
602651 let options ;
603652
604653 if ( this . localProxy ?. enabled ) {
605- this . logger . info ( 'Proxy enabled: ' + this . localProxy ?. host ) ;
654+ this . logger . verbose ( 'Proxy enabled' ) ;
606655
607656 if ( this . localProxy ?. host ?. includes ( 'proxyscrape' ) ) {
608657 try {
@@ -611,9 +660,10 @@ export class BaileysStartupService extends ChannelStartupService {
611660 const proxyUrls = text . split ( '\r\n' ) ;
612661 const rand = Math . floor ( Math . random ( ) * Math . floor ( proxyUrls . length ) ) ;
613662 const proxyUrl = 'http://' + proxyUrls [ rand ] ;
663+ this . logger . info ( 'Proxy url: ' + proxyUrl ) ;
614664 options = { agent : makeProxyAgent ( proxyUrl ) , fetchAgent : makeProxyAgentUndici ( proxyUrl ) } ;
615- } catch {
616- this . localProxy . enabled = false ;
665+ } catch ( error ) {
666+ this . logger . error ( error ) ;
617667 }
618668 } else {
619669 options = {
0 commit comments