Nerixyz / instagram_mqtt

Realtime and Push Notification (FBNS) support for the instagram-private-api
MIT License
244 stars 49 forks source link

Proper way to keep subscriptions working over extended periods of time #19

Open Ektaros opened 4 years ago

Ektaros commented 4 years ago

During some initial testing (using 0.1.17 version) a noticed that after some time (10-12 hours) realtime subscription (Im using only one) started working slowly, and fbns stopped working at all. So the idea was to reconnect them after some time. I settle upon around 4-5 hours. It worked fine but after a day or so of heavy use i got this error:

"message": "Got warning in fbns sub",
  "warning": {
    "code": "ERR_STREAM_DESTROYED",
    "name": "Error",
    "message": "Cannot call write after a stream was destroyed",
    "stack": "Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed\n    at doWrite (_stream_writable.js:431:19)\n    at writeOrBuffer (_stream_writable.js:419:5)\n    at TLSSocket.Writable.write (_stream_writable.js:309:11)\n    at MQTToTClient.writePacketToSocket (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:237:21)\n    at MQTToTClient.handleSendingFlows (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:217:22)\n    at MQTToTClient.startFlow (/usr/backen [ ...... ] /mqtt/mqtt.client.ts:238:27\n    at processTicksAndRejections (internal/process/task_queues.js:80:21) [ long string, total length = 552638 ]"
  },

I consider that as a dead subscription so right after that I call

await this.ig.fbns.disconnect()
await this.ig.fbns.connect();

I already saw this portion of code work as intended but this time it followed with these bad boys:

"error": {
    "name": "RangeError",
    "message": "Maximum call stack size exceeded",
    "stack": "RangeError: Maximum call stack size exceeded\n    at MQTToTClient.<anonymous> (/usr/backend/node_modules/instagram_mqtt/src/mqttot/mqttot.client.ts:27:71)\n    at MQTToTClient.emit (events.js:228:7)\n    at MQTToTClient.EventEmitter.emit (domain.js:475:20)\n    at MQTToTClient.MqttClient.emitWarning (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:88:52)\n    at MQTToTClient.startFlow (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:170:18)\n    at MQTToTClient.discon [ ...... ] /mqtt/mqtt.client.ts:238:27\n    at processTicksAndRejections (internal/process/task_queues.js:80:21) [ long string, total length = 561814 ]"
  },

"error": {
    "name": "RangeError",
    "message": "Maximum call stack size exceeded",
    "stack": "RangeError: Maximum call stack size exceeded\n    at new PacketFlow (/usr/backend/node_modules/instagram_mqtt/src/mqtt/flow/packet-flow.ts:33:5)\n    at new OutgoingDisconnectFlow (/usr/backend/node_modules/instagram_mqtt/src/mqtt/flow/outgoing.disconnect.flow.ts:10:9)\n    at MQTToTClient.disconnect (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:132:31)\n    at FbnsClient.disconnect (/usr/backend/node_modules/instagram_mqtt/src/fbns/fbns.client.ts:317:28)\n    at Instagram._callee [ ...... ] al/streams/destroy.js:60:3)\n    at processTicksAndRejections (internal/process/task_queues.js:81:21) [ long string, total length = 550309 ]"
  },

"error": {
    "name": "RangeError",
    "message": "Maximum call stack size exceeded",
    "stack": "RangeError: Maximum call stack size exceeded\n    at new Context (/usr/backend/node_modules/regenerator-runtime/runtime.js:427:5)\n    at Object.wrap (/usr/backend/node_modules/regenerator-runtime/runtime.js:23:19)\n    at Instagram._callee19 (/usr/backend/src/instagramAPI/instagram.js:1072:35)\n    at /usr/backend/src/instagramAPI/instagram.js:32:151\n    at new Promise (<anonymous>)\n    at Instagram.<anonymous> (/usr/backend/src/instagramAPI/instagram.js:32:97)\n    at Instagram.stopFbns (/usr/backe [ ...... ] /mqtt/mqtt.client.ts:238:27\n    at processTicksAndRejections (internal/process/task_queues.js:80:21) [ long string, total length = 560905 ]"
  },

stopFbns() - function that just calls await this.ig.fbns.disconnect() after thar i got flooded with these errors:

"error": {
    "code": "EPIPE",
    "name": "Error",
    "message": "This socket has been ended by the other party",
    "stack": "Error: This socket has been ended by the other party\n    at TLSSocket.writeAfterFIN [as write] (net.js:447:14)\n    at MQTToTClient.writePacketToSocket (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:237:21)\n    at MQTToTClient.handleSendingFlows (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:217:22)\n    at MQTToTClient.startFlow (/usr/backend/node_modules/instagram_mqtt/src/mqtt/mqtt.client.ts:155:22)\n    at MQTToTClient.registerClient (/usr/backend/node_modules/instagram_mqtt/src/mqttot/mqttot.client.ts:38:28)\n    at Timeout._onTimeout (/usr/backend/node_modules/instagram_mqtt/src/mqttot/mqttot.client.ts:46:18)\n    at listOnTimeout (internal/timers.js:531:17)\n    at processTimers (internal/timers.js:475:7)"
  },

 "error": {
    "name": "IgResponseError",
    "message": "POST /api/v1/push/register/ - 400 Bad Request; failed to acquire pushlock",
    "stack": "IgResponseError: POST /api/v1/push/register/ - 400 Bad Request; failed to acquire pushlock\n    at Request.handleResponseError (/usr/backend/node_modules/instagram-private-api/src/core/request.ts:161:12)\n    at Request.send (/usr/backend/node_modules/instagram-private-api/src/core/request.ts:78:24)\n    at FbnsClient.sendPushRegister (/usr/backend/node_modules/instagram_mqtt/src/fbns/fbns.client.ts:374:26)\n    at FbnsClient.handleMessage (/usr/backend/node_modules/instagram_mqtt/src/fbns/fbns.client.ts:334:21)"
  },

Unfortunatly, i did not have debug running, so this is the info i can provide This happend in between my reconnect period so after 3 hours all came back to normal.

That could be the cause of such behavior? Is this even the right way to keep subscriptions working?