gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

Fixes for network reconnection

Open SecurityAndStuff opened this issue 2 years ago • 1 comments

This patch prevents race conditions during network failure and fixes the issues mentioned in https://github.com/gram-js/gramjs/issues/375 https://github.com/gram-js/gramjs/issues/303 https://github.com/gram-js/gramjs/issues/372 https://github.com/gram-js/gramjs/issues/338

To reproduce, I used this code and toggled the network connection on and off:

// simulate network failure while receiving data
setInterval(async () => {
      this.client.getDialogs({})
      await this.client.disconnect()
      await this.client.connect()
    }, 10_000)
  }

Before:

[2022-09-22T00:00:20.684] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Disconnect (caused from messages.GetHistory)
Error: Disconnect (caused from messages.GetHistory)
Error: Disconnect (caused from channels.GetParticipants)
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:00:20.689] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:20.690] [INFO] - [connection closed]
[2022-09-22T00:00:20.735] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]
[2022-09-22T00:00:20.735] [INFO] - [Using LAYER 145 for initial connect]
[2022-09-22T00:00:27.680] [INFO] - [connection closed]
[2022-09-22T00:00:27.680] [ERROR] - [Error: Not connected]
[2022-09-22T00:00:27.680] [WARN] - [Connection closed while receiving data]
[2022-09-22T00:00:28.683] [INFO] - [Started reconnecting]
[2022-09-22T00:00:28.683] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
[2022-09-22T00:00:28.684] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:28.684] [ERROR] - [WebSocket connection failed attempt: 1]
[2022-09-22T00:00:29.692] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:29.693] [ERROR] - [WebSocket connection failed attempt: 2]
[2022-09-22T00:00:30.685] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:00:30.685] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:30.686] [ERROR] - [WebSocket connection failed attempt: 1]
[2022-09-22T00:00:30.700] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:30.700] [ERROR] - [WebSocket connection failed attempt: 3]
[2022-09-22T00:00:31.693] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:31.694] [ERROR] - [WebSocket connection failed attempt: 2]
[2022-09-22T00:00:31.709] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:31.710] [ERROR] - [WebSocket connection failed attempt: 4]
[2022-09-22T00:00:32.700] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:32.701] [ERROR] - [WebSocket connection failed attempt: 3]
[2022-09-22T00:00:32.716] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:32.716] [ERROR] - [WebSocket connection failed attempt: 5]
[2022-09-22T00:00:33.700] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:33.701] [ERROR] - [WebSocket connection failed attempt: 4]
[2022-09-22T00:00:33.716] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:33.716] [ERROR] - [WebSocket connection failed attempt: 6]
[2022-09-22T00:00:34.702] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:34.703] [ERROR] - [WebSocket connection failed attempt: 5]
[2022-09-22T00:00:34.718] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:34.719] [ERROR] - [WebSocket connection failed attempt: 7]
[2022-09-22T00:00:35.716] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:35.732] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-22T00:00:35.763] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]
[2022-09-22T00:00:35.763] [INFO] - [Using LAYER 145 for initial connect]
[2022-09-22T00:00:35.778] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]
[2022-09-22T00:00:35.825] [INFO] - [connection closed]
[2022-09-22T00:00:35.826] [ERROR] - [Error: Not connected]
[2022-09-22T00:00:35.826] [WARN] - [Connection closed while receiving data]
[2022-09-22T00:00:35.826] [ERROR] - [Error: Not connected]
[2022-09-22T00:00:35.826] [WARN] - [Connection closed while receiving data]
[2022-09-22T00:00:35.826] [INFO] - [connection closed]
[2022-09-22T00:00:36.829] [INFO] - [Started reconnecting]
[2022-09-22T00:00:36.829] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Disconnect (caused from users.GetUsers)
[2022-09-22T00:00:36.829] [ERROR] - [Error while trying to reconnect]
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:00:40.685] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:00:50.685] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:01:00.692] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
Error: Cannot send requests while disconnected. You need to call .connect()
[2022-09-22T00:01:05.711] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]

After:

[2022-09-21T23:56:04.599] [INFO] - [connection closed]
[2022-09-21T23:56:04.599] [ERROR] - [Error: Not connected]
[2022-09-21T23:56:04.599] [WARN] - [Connection closed while receiving data]
[2022-09-21T23:56:05.610] [INFO] - [Started reconnecting]
[2022-09-21T23:56:05.611] [INFO] - [Disconnecting from 149.154.167.91:443/TCPFull...]
[2022-09-21T23:56:05.612] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:05.612] [ERROR] - [WebSocket connection failed attempt: 1]
[2022-09-21T23:56:06.627] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:06.627] [ERROR] - [WebSocket connection failed attempt: 2]
[2022-09-21T23:56:07.633] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:07.633] [ERROR] - [WebSocket connection failed attempt: 3]
[2022-09-21T23:56:08.636] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:08.637] [ERROR] - [WebSocket connection failed attempt: 4]
[2022-09-21T23:56:09.652] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:09.652] [ERROR] - [WebSocket connection failed attempt: 5]
[2022-09-21T23:56:10.654] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:10.655] [ERROR] - [WebSocket connection failed attempt: 6]
[2022-09-21T23:56:11.667] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:11.668] [ERROR] - [WebSocket connection failed attempt: 7]
[2022-09-21T23:56:12.670] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:12.671] [ERROR] - [WebSocket connection failed attempt: 8]
[2022-09-21T23:56:13.672] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:13.672] [ERROR] - [WebSocket connection failed attempt: 9]
[2022-09-21T23:56:14.689] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:14.690] [ERROR] - [WebSocket connection failed attempt: 10]
[2022-09-21T23:56:15.690] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:15.691] [ERROR] - [WebSocket connection failed attempt: 11]
[2022-09-21T23:56:16.706] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:16.707] [ERROR] - [WebSocket connection failed attempt: 12]
[2022-09-21T23:56:17.714] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:17.715] [ERROR] - [WebSocket connection failed attempt: 13]
[2022-09-21T23:56:18.727] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:18.728] [ERROR] - [WebSocket connection failed attempt: 14]
[2022-09-21T23:56:19.733] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:19.733] [ERROR] - [WebSocket connection failed attempt: 15]
[2022-09-21T23:56:20.745] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:20.745] [ERROR] - [WebSocket connection failed attempt: 16]
[2022-09-21T23:56:21.761] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:21.761] [ERROR] - [WebSocket connection failed attempt: 17]
[2022-09-21T23:56:22.766] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:22.766] [ERROR] - [WebSocket connection failed attempt: 18]
[2022-09-21T23:56:23.778] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:23.778] [ERROR] - [WebSocket connection failed attempt: 19]
[2022-09-21T23:56:24.790] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:24.790] [ERROR] - [WebSocket connection failed attempt: 20]
[2022-09-21T23:56:25.804] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2022-09-21T23:56:25.848] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]

SecurityAndStuff avatar Sep 20 '22 03:09 SecurityAndStuff

Hello! My code is not working for more than 10 minutes. In my code, the connection to different sessions is constantly changing and information about channels is collected from them. After each session, I execute "client.destroy()", after some time of such work, my code freezes forever and prints this to the console. i am using npm package version 2.11.7

https://ibb.co/n1WWMCp https://ibb.co/LdqRDs2

please accept this update quickly, this error is preventing me from living 😢

sanch1z avatar Sep 23 '22 18:09 sanch1z

hope to see this merges soon!

MoKhajavi75 avatar Sep 27 '22 15:09 MoKhajavi75

version 2.12.0 should have a similar fix. It will retry the requests that were lost during the connection drop out.

painor avatar Sep 27 '22 21:09 painor

Fixed in https://github.com/gram-js/gramjs/commit/72c3778171ded55ad749d9d7812ff01f37ecd153. Thanks!

ghost avatar Sep 28 '22 14:09 ghost