auth-utils: Fixing memory leak (release transaction mutex after references are 0) #2153
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context: #2151 sparked an internal discussion within myself regarding the memory leak we are facing.
Problem: We are facing a memory leak after the first few release candidates.
Motivation: #2151 seemed like it was headed in the right direction, but as soon as I saw that it was actually producing race conditions, I knew the fix to the confirmed leak wasn't there. I also realized that the make-mutex file hasn't been edited for years and likely not the reason why Baileys is leaking.
Rushing to try and think where this leak could be, I remembered my experimental addTransactionCapability rewrite (this system fucking sucks and will be removed from Baileys entirely).
And there it was, confirmation: The addTransactionCapability function was never clearing any of the mutexes with 0 references to them.
It remains to be seen if this patch is correct - I whipped it up directly. It also remains to be seen if #2151 is going to get merged with this PR or not, as that function needs complete removal or modernization.
Please test this out on your sessions, it will likely have 0 stability impact due to us just running a clearing function on empty mutexes. This is still an experimental change, and please run some sort of perf monitoring tool for hours.
PS: Please support my work on GitHub Sponsors, as it takes a lot of prioritization to drop my exams and focus on this for 2 hours in a row. https://purpshell.dev/sponsor