Skip to content

Conversation

@purpshell
Copy link
Member

@purpshell purpshell commented Dec 6, 2025

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

@whiskeysockets-bot
Copy link
Contributor

Thanks for opening this pull request and contributing to the project!

The next step is for the maintainers to review your changes. If everything looks good, it will be approved and merged into the main branch.

In the meantime, anyone in the community is encouraged to test this pull request and provide feedback.

✅ How to confirm it works

If you’ve tested this PR, please comment below with:

Tested and working ✅

This helps us speed up the review and merge process.

📦 To test this PR locally:

# NPM
npm install @whiskeysockets/baileys@WhiskeySockets/Baileys#fix/mem-leak

# Yarn (v2+)
yarn add @whiskeysockets/baileys@WhiskeySockets/Baileys#fix/mem-leak

# PNPM
pnpm add @whiskeysockets/baileys@WhiskeySockets/Baileys#fix/mem-leak

If you encounter any issues or have feedback, feel free to comment as well.

@purpshell purpshell requested a review from jlucaso1 December 6, 2025 01:43
@YonkoSam
Copy link
Contributor

YonkoSam commented Dec 6, 2025

you should use the same map for both the mutex and refCount its 100% safe against race conditions

@renatoiub
Copy link
Contributor

One place where I see a serious memory leak is in the event buffer; each upsert keeps accumulating until it consumes a huge amount of memory. I think the problem is there.

@purpshell
Copy link
Member Author

purpshell commented Dec 12, 2025

One place where I see a serious memory leak is in the event buffer; each upsert keeps accumulating until it consumes a huge amount of memory. I think the problem is there.

I saw a new PR lately that fixes this, I think I approved it
EDIT: #2160

@purpshell purpshell merged commit a7a53ad into master Dec 12, 2025
4 checks passed
@Salientekill
Copy link

One place where I see a serious memory leak is in the event buffer; each upsert keeps accumulating until it consumes a huge amount of memory. I think the problem is there.

I saw a new PR lately that fixes this, I think I approved it EDIT: #2160

Not yet approved 🫡

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.

7 participants