Skip to content

Conversation

@YonkoSam
Copy link
Contributor

to be honest all this code should be rewritten, but for now, this will drastically improve CPU utilization.

Copilot AI review requested due to automatic review settings December 10, 2025 21:34
@whiskeysockets-bot
Copy link
Contributor

whiskeysockets-bot commented Dec 10, 2025

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@YonkoSam/Baileys#fix-wasted-cpu

# Yarn (v2+)
yarn add @whiskeysockets/baileys@YonkoSam/Baileys#fix-wasted-cpu

# PNPM
pnpm add @whiskeysockets/baileys@YonkoSam/Baileys#fix-wasted-cpu

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

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a performance optimization to improve CPU utilization by moving the retryCount > 1 check before expensive session validation operations, and fixes a resource leak in the event buffer system that was scheduling thousands of redundant flush timeouts.

Key changes:

  • Moved retry count validation from inside shouldRecreateSession() to the call sites, avoiding unnecessary session validation when retryCount <= 1
  • Added flushPendingTimeout tracking to prevent scheduling multiple flush timeouts in the event buffer
  • Modified event buffer's bufferCount initialization and increment logic

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Utils/message-retry-manager.ts Removed retryCount parameter and internal retry count check from shouldRecreateSession(), simplifying the method to only handle session existence and recreation timing
src/Socket/messages-recv.ts Added retryCount > 1 guard before calling session validation logic, moved retry count check upstream to avoid expensive operations; updated optional chaining for safer property access
src/Utils/event-buffer.ts Added flushPendingTimeout variable to prevent scheduling multiple flush timeouts; modified bufferCount initialization to reset on new buffering session

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Salientekill
Copy link

Thanks for the work 👍

@Salientekill
Copy link

Wouldn't it be good to open another PR refactoring the entire code to improve maintainability and reduce complexity?

@YonkoSam
Copy link
Contributor Author

Wouldn't it be good to open another PR refactoring the entire code to improve maintainability and reduce complexity?

somebody that is more familiar with the library should take care of that

Copy link
Member

@purpshell purpshell left a comment

Choose a reason for hiding this comment

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

I agree that this should be rewritten. I don't see any immediate issues from the code you have suggested. Pending testing before merge

@purpshell
Copy link
Member

Wouldn't it be good to open another PR refactoring the entire code to improve maintainability and reduce complexity?

somebody that is more familiar with the library should take care of that

yeah I will either work on this myself or assign it to someone like @vinikjkkj or @jlucaso1

The code from @pvictorlv is totally horrible. I mean that in the best way possible. I will gut it all out and build retries step by step from scratch following the mechanisms of whatsmeow and whatsapp web

@YonkoSam
Copy link
Contributor Author

Wouldn't it be good to open another PR refactoring the entire code to improve maintainability and reduce complexity?

somebody that is more familiar with the library should take care of that

yeah I will either work on this myself or assign it to someone like @vinikjkkj or @jlucaso1

The code from @pvictorlv is totally horrible. I mean that in the best way possible. I will gut it all out and build retries step by step from scratch following the mechanisms of whatsmeow and whatsapp web

that will 100% fix the memeory leak issues

@techwebsolucao
Copy link

@YonkoSam i trying revert this commit and fix compatibility

Copy link
Collaborator

@vinikjkkj vinikjkkj left a comment

Choose a reason for hiding this comment

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

you sent event-buffer changes from your other PR here

@YonkoSam
Copy link
Contributor Author

you sent event-buffer changes from your other PR here

fixed

@purpshell purpshell requested a review from vinikjkkj December 12, 2025 21:21
@github-actions
Copy link
Contributor

This PR is stale because it has been open for 14 days with no activity. Remove the stale label or comment or this will be closed in 14 days

@github-actions github-actions bot added the Stale label Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants