Skip to content

Memory leak: _resolvers entries are never removed when a protocol 3.5 device answers get() with a different sequence number #713

Open

Description

@jowgn

Environment

  • tuyapi 7.7.1 (via node-red-contrib-tuya-smart-device 5.4.0), Node.js 24, Node-RED 4.1, running on a Victron Cerbo GX (ARM, 1 GB RAM)
  • Device: EV wallbox, Tuya protocol 3.5, polled with get({ schema: true }) every 30 s

What happens
Every get() goes through _send(), which stores this._resolvers[sequenceNo] inside a pRetry operation. The device does answer — the data arrives as a data event — but the response packet carries a different sequenceN, so the packet handler never finds or deletes the resolver. The promise never settles, so the resolver, the pending p-retry RetryOperation and the closures behind it (including buffers of roughly 60 kB each) stay in memory forever.

A heap snapshot after 12.7 h showed 1501 entries in TuyaDevice._resolvers, 1501 RetryOperation objects and about 90 MB of retained ArrayBuffers — almost exactly one entry per poll. Memory grew by about 7 MB/h until the system ran out of memory and restarted every 36–40 h.

Workaround we are using
In _send(), right after the resolver is registered:

this._resolvers[sequenceNo] = data => resolve(data);
setTimeout(() => {
  if (this._resolvers[sequenceNo]) {
    delete this._resolvers[sequenceNo];
    resolve(undefined);
  }
}, 15000).unref();

With this change memory stays flat (no growth over 12 h). A cleaner fix might be a general timeout in _send(), or matching protocol 3.5 responses to the pending request even when the sequence number differs.

Related but old and closed: #233.

Activity

  1. Apollon77 commented on Nov 11, 2026

    @Apollon77
    Collaborator

    @jowgn You could also do a PR for it :-) That would be great

  2. jowgn commented on Nov 11, 2026

    @jowgn
    Author

    @jowgn You could also do a PR for it :-) That would be great

    I will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions