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

Fix memory leak in subscription "message-id to topic" mapping #1535

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

bverhoeven
Copy link
Contributor

We've been using this library extensively in a project that makes a lot of subscriptions and have noticed that it's been leaking memory (which is causing NodeJS to crash with "JavaScript heap out of memory").

We've pinpointed this to the MQTT.js library's "message id to subscription" mapping:

A message ID is assigned and stored in messageIdToTopic whenever a new subscription is started and resubscription is enabled (which is the default). The message-id to topic mapping is never cleared and keeps accumulating over time, which creates a memory leak that can not be resolved by the caller.

This mapping seems to have been introduced as a way to prevent resubscriptions of topics that have failed to subscribe on their first attempt, as introduced in as part of #665 in f6d12f0

There's, unless I'm overlooking something, no reason to keep this mapping in place once an acknowledgement is received. With that assumption, I've created a few tests to reproduce the issue and have created a simple fix that clears the mapping when SUBACK is received.

We've been running a patched version of the library and have noticed a significant reduce in memory usage and can confirm that, for us, the memory leak has been resolved. Would love to get this included upstream though. If there's something that we missed, I'd love to know too!

@robertsLando
Copy link
Member

@YoDaMa ping

Copy link

@harrywebdev harrywebdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@robertsLando
Copy link
Member

@vishnureddy17 @ryanwinter @BertKleewein anyone here that can kindly merge this PR?

@vishnureddy17 vishnureddy17 merged commit 8c77eec into mqttjs:master Jun 22, 2023
robertsLando added a commit that referenced this pull request Jul 3, 2023
…1622)

@vishnureddy17 Fix typo in README 00bf657
@bverhoeven Fix memory leak in subscription topic mapping (#1535) 8c77eec
@mwohlert @robertsLando @ynagasaki feat: allow user to disable pre-generated write cache (#1151) 0d11888

Co-authored-by: Daniel Lando <[email protected]>
Co-authored-by: Yoshi Nagasaki <[email protected]>
Co-authored-by: Dmitry Kurmanov <[email protected]>
Co-authored-by: Vishnu Reddy <[email protected]>
Co-authored-by: Bas Verhoeven <[email protected]>
Co-authored-by: Michel Wohlert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants