Skip to content
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

[BUG] Session Keep Disconected #913

Open
chardea opened this issue Jul 8, 2024 · 1 comment
Open

[BUG] Session Keep Disconected #913

chardea opened this issue Jul 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@chardea
Copy link

chardea commented Jul 8, 2024

Describe the bug
I have issue regarding session. I got mass session loggedOut 1 hour after device Scan. I have more than 10 session in my service but less than 30.

To Reproduce
Steps to reproduce the behavior:

  1. Scan 5-10 devices.
  2. wait for hours doing nothing (usually 1 hours)
  3. most of devices shows need to rescan.
  4. log showing session error code 401

Expected behavior

  1. Scan 5-10 devices.
  2. no need to rescan after 1 hour of idle.

Environment (please complete the following information):

  • My server are on DO using kubernates.
  • All IP is static in SG, my devices are Indonesian phone number.
  • these are my codes related:
public connectionHandler({
   sock,
   store,
   sessionId,
 }: {
   sock: WASocket;
   store: WAStore;
   sessionId: string;
 }): ConnectionFn {
   return async ({
     qr,
     connection,
     lastDisconnect,
   }: Partial<ConnectionState>): Promise<void> => {
     this.logger.info(
       {attrs: {sessionId, connection}},
       'new connection status',
     );

     if (qr) {
       try {
         const qrCode = await toDataURL(qr);

         const evPayload: WhatsappEventPayload = {
           sock,
           store,
           sessionId,
           qrCode,
         };

         this.ev.emit('wa::conn.qr', evPayload);

         return;
       } catch (error) {
         this.logger.error(
           {err: error, attrs: {sessionId}},
           'error generate qrCode',
         );

         // attempt logout when error
       }

       console.info('failed QR, attempt logout :', sessionId);
       try {
         await sock.logout();
       } catch (error) {
         this.logger.error(
           {err: error, attrs: {sessionId}},
           'error when logout',
         );
       }

       this.retries.delete(sessionId);

       const evPayload: WhatsappEventPayload = {
         sock,
         store,
         sessionId,
       };

       this.ev.emit('wa::conn.logout', evPayload);

       return;
     }

     if (connection === WAConnectionState.CONNECTING) {
       this.logger.info({attrs: {sessionId, connection}}, 'connecting socket');
     }

     if (connection === WAConnectionState.CLOSE) {
       const {statusCode} = new Boom(lastDisconnect?.error)?.output;
       const reason = statusCode as DisconnectReason;

       if (
         reason === DisconnectReason.loggedOut ||
         reason === DisconnectReason.badSession ||
         !this.shouldReconnect(sessionId)
       ) {
         this.retries.delete(sessionId);

         const evPayload: WhatsappEventPayload = {
           sock,
           store,
           sessionId,
           reason,
         };

         this.ev.emit('wa::conn.logout', evPayload);

         return;
       }

       const reconnectInterval =
         reason === DisconnectReason.restartRequired ||
         reason === DisconnectReason.timedOut
           ? 0
           : this.clientConf.reconnectInterval;

       const evPayload: WhatsappEventPayload = {
         sock,
         store,
         sessionId,
         reason,
       };

       const undoRetries = this.undoRetries.get(sessionId);
       if (undoRetries) {
         console.info(
           'connection unstable! will perform retries...',
           JSON.stringify({
             reason: DisconnectReason[reason],
             sessionId,
             stableWaitTime: this.clientConf.stableWaitTime,
           }),
         );
         clearTimeout(undoRetries);
       }

       setTimeout(() => {
         this.ev.emit('wa::conn.reconnecting', evPayload);
       }, reconnectInterval);

       this.ev.emit('wa::conn.attempt_reconnect', evPayload);
     }

     if (connection === WAConnectionState.OPEN) {
       // it's possible to reconnecting again after connected
       // so need to delay for deleting retries until it's stable
       this.undoRetries.set(
         sessionId,
         setTimeout(() => {
           console.info(
             'connection stable! undoing retries...',
             JSON.stringify({
               sessionId,
               stableWaitTime: this.clientConf.stableWaitTime,
             }),
           );
           this.retries.delete(sessionId);
         }, this.clientConf.stableWaitTime),
       );

       const evPayload: WhatsappEventPayload = {
         sock,
         store,
         sessionId,
       };

       this.ev.emit('wa::conn.connected', evPayload);
     }
   };
 }

Pls Let me know this disconecting session related to my bad infra or the codes. Thanks

@chardea chardea added the bug Something isn't working label Jul 8, 2024
@chardea chardea changed the title [BUG] [BUG] Session Keep Disconected Jul 8, 2024
@athilasb
Copy link

athilasb commented Jul 8, 2024

#860 I think it would be the same problem, but there is no solution yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants