0x5e / homebridge-tuya-platform

Make homebridge-tuya-platform great again.
MIT License
206 stars 58 forks source link

Better Error Handling When Encountering ECONNRESET #392

Closed lesterchan closed 8 months ago

lesterchan commented 8 months ago

Prerequisite

Cache

Version

1.7.0-beta.51

Device Infomation JSON File

No response

Device Control Mode

None

Logs

[10/18/2023, 2:06:22 PM] [Tuya] [TuyaOpenAPI] Request:
method = post
endpoint = https://openapi.tuyaeu.com
path = /v1.0/devices/<REMOVED>/commands
query = null
headers = {
  "t": "1697609182180",
  "client_id": "<REMOVED>",
  "nonce": "<REMOVED>",
  "Signature-Headers": "client_id",
  "sign": "<REMOVED>",
  "sign_method": "HMAC-SHA256",
  "access_token": "<REMOVED>",
  "lang": "en",
  "dev_lang": "javascript",
  "dev_channel": "homebridge",
  "devVersion": "1.7.0-beta.51"
}
body = {
  "commands": [
    {
      "code": "switch_led",
      "value": true
    }
  ]
}
[10/18/2023, 2:06:22 PM] Error: read ECONNRESET
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)

Other Infomations

Sometimes, when calling Tuya API, I got a ECONNRESET, and that causes Homebridge to restart. I am not sure why when an API call fail, it will cause Homebridge to restart.

If you are unlucky, it will be an infinite loop because when it restarts, it will call Tuya again to fetch the devices and if that API encounter ECONNRESET, it will cause it to restart again.

lesterchan commented 8 months ago

Not sure on this line https://github.com/0x5e/homebridge-tuya-platform/blob/develop_1.7.0/src/core/TuyaOpenAPI.ts#L313, whether we should just log

this.log.error(e.message);
0x5e commented 8 months ago

Thanks @lesterchan for the feedback, I've noticed this issue and didn't dig into the real reason before. It looks like a network issue, or tuya server side issue maybe? Maybe we can add some retry when api call failed.

I think we can't really solve this issue by modify the plugin code, if you are really unlucky, the plugin would be infinite retry instead of homebridge restart, that would be better but problem still there...

lesterchan commented 8 months ago

I agree an infinite retry would be better than a Homebridge restart. The reason is that when Homebridge keeps restarting, the plugins will have to keep logging in to a service (example, potmat/homebridge-google-nest-sdm), and it will get rate limited by Google.

0x5e commented 8 months ago

Understand. I'm busy and busy this year so... hope I could find a way of minimun change to fix this :)

lesterchan commented 8 months ago

https://www.npmjs.com/package/async-await-retry might be a drop-in replacement that we can use in TuyaOpenAPI.ts. Not tested, so on.error, we don't reject, we just log and let retry() to retry it.

    const res: TuyaOpenAPIResponse = await retry(async () => {
      return new Promise((resolve, reject) => {
        const req = https.request({
          host: new URL(this.endpoint).host,
          method,
          headers,
          path,
        }, res => {
          if (res.statusCode !== 200) {
            this.log.warn('Status: %d %s', res.statusCode, res.statusMessage);
            return;
          }
          res.setEncoding('utf8');
          let rawData = '';
          res.on('data', (chunk) => {
            rawData += chunk;
          });
          res.on('end', () => {
            resolve(JSON.parse(rawData));
          });
        });

        if (body) {
          req.write(JSON.stringify(body));
        }

        req.on('error', e => this.log.error(e.message));
        req.end();
      });

      this.log.debug('Response:\npath = %s\ndata = %s', path, JSON.stringify(res, null, 2));
      if (res && res.success !== true && API_ERROR_MESSAGES[res.code]) {
        this.log.error(API_ERROR_MESSAGES[res.code]);
      }

      return res;
    }, null, {retriesMax: 4, interval: 100, exponential: true, factor: 3, jitter: 100})
  }
0x5e commented 8 months ago

@lesterchan thanks! I tried this, reject should not removed or there's no retry... I add logs before reject. this package is cool.

0x5e commented 8 months ago

updated in 1.7.0-beta.52. 0c1d40b91440d0926f0d8a855fcdc3a57c332b54. please try. I can't reproduce ECONNRESET, I just test normal network and network closed, two situations.

randymassey commented 7 months ago

Hi there, this is Randy Massey checking in…It’s been a little while since I’ve needed to toss a message in, I apologize I’m just jumping onto a recent email versus opening a ticket, it’s close to midnight, and I’m heading to bed..But in short, the Driveway motion sensor light, & we did run into this 2 times before as I recall, once, when the adaptive lighting was activated, and then I think randomly at a certain plug-in beta update,some a few months after the adaptive lighting scenario where the light would come on, and then just stay on… That’s been happening again, unfortunately, I didn’t know this was taking place until the neighbor informed me recently. I did some brief testing, stepping backwards from the current version through each single version for a number of versions, with no luck. I then started taking larger steps backwards with plug-in versions, eg skipping over several, I eventually landed on 1.7.0 beta 37, before I got it to behave again. So it, however, is not the case that I had tested each version all the way backwards until it finally worked. I did that at the outset, but started making bigger jumps the more of it I had to do without success… so for instance, 38, 39, 40, we’re not tested. I did not, but wish I had documented exactly each version I back stepped too… But what I also observed that seemed a little bit different to me, is that the light seemed to just be coming on by itself… Previously, the problem had been motion would trigger it it would come on, but then stay on versus shutting off after the set time… This problem recently seems to after some period of a few minutes just turn itself on. Around five minutes seemed to be the interval. I was observing doing some Driveway camera testing which let me monitor it from inside the house since this takes place at night. Anyway, I can open a formal ticket certainly if you need me to, but the last couple of times this happened, you seem to know exactly what had been the culprit and resolved it quickly in a subsequent update. So I figured I’d reach out quickly from current email, I saw from you, to see if you had any thoughts as to what could be happening here.My neighbor did tell me it had been going on for a few months, they just had not said anything apparently thinking I had done it intentionally, but that almost matches how far back I had to go in the plug-in back, stepping just top of brain…FYIRandy On Oct 18, 2023, at 2:04 AM, Lester Chan @.***> wrote: Not sure on this line https://github.com/0x5e/homebridge-tuya-platform/blob/develop_1.7.0/src/core/TuyaOpenAPI.ts#L313, whether we should just log this.log.error(e.message);

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

0x5e commented 7 months ago

Hi there, this is Randy Massey checking in…It’s been a little while since I’ve needed to toss a message in, I apologize I’m just jumping onto a recent email versus opening a ticket, it’s close to midnight, and I’m heading to bed..But in short, the Driveway motion sensor light, & we did run into this 2 times before as I recall, once, when the adaptive lighting was activated, and then I think randomly at a certain plug-in beta update,some a few months after the adaptive lighting scenario where the light would come on, and then just stay on… That’s been happening again, unfortunately, I didn’t know this was taking place until the neighbor informed me recently. I did some brief testing, stepping backwards from the current version through each single version for a number of versions, with no luck. I then started taking larger steps backwards with plug-in versions, eg skipping over several, I eventually landed on 1.7.0 beta 37, before I got it to behave again. So it, however, is not the case that I had tested each version all the way backwards until it finally worked. I did that at the outset, but started making bigger jumps the more of it I had to do without success… so for instance, 38, 39, 40, we’re not tested. I did not, but wish I had documented exactly each version I back stepped too… But what I also observed that seemed a little bit different to me, is that the light seemed to just be coming on by itself… Previously, the problem had been motion would trigger it it would come on, but then stay on versus shutting off after the set time… This problem recently seems to after some period of a few minutes just turn itself on. Around five minutes seemed to be the interval. I was observing doing some Driveway camera testing which let me monitor it from inside the house since this takes place at night. Anyway, I can open a formal ticket certainly if you need me to, but the last couple of times this happened, you seem to know exactly what had been the culprit and resolved it quickly in a subsequent update. So I figured I’d reach out quickly from current email, I saw from you, to see if you had any thoughts as to what could be happening here.My neighbor did tell me it had been going on for a few months, they just had not said anything apparently thinking I had done it intentionally, but that almost matches how far back I had to go in the plug-in back, stepping just top of brain…FYIRandy On Oct 18, 2023, at 2:04 AM, Lester Chan @.> wrote: Not sure on this line https://github.com/0x5e/homebridge-tuya-platform/blob/develop_1.7.0/src/core/TuyaOpenAPI.ts#L313, whether we should just log this.log.error(e.message); —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.>

Hi @randymassey , unfortunately adaptive lighting is not supported for all light devices, some device will turned on when brightness changed, even when it's off :(