Skip to content

feat: Mutex redesign - isolate event processing for better ordering and throughput#2137

Merged
purpshell merged 9 commits into
WhiskeySockets:masterfrom
Santosl2:fix/create-individual-mutex
Dec 12, 2025
Merged

feat: Mutex redesign - isolate event processing for better ordering and throughput#2137
purpshell merged 9 commits into
WhiskeySockets:masterfrom
Santosl2:fix/create-individual-mutex

Conversation

@Santosl2

@Santosl2 Santosl2 commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

This pull request refactors the mutex usage in the socket modules to improve concurrency control and processing order for different event types. Instead of a single processingMutex, the code now uses dedicated mutexes for messages, receipts, notifications, and app state patches. This change enhances clarity, prevents resource contention, and allows more granular locking for each event type.

Concurrency and Mutex Refactoring:

  • Replaced the single processingMutex with four dedicated mutexes: messageMutex, receiptMutex, appStatePatchMutex, and notificationMutex in src/Socket/chats.ts. Each mutex now ensures ordered processing for its respective event type.
  • Updated all function signatures and exports in src/Socket/chats.ts, src/Socket/messages-recv.ts, and src/Socket/messages-send.ts to use the new mutexes instead of processingMutex. [1] [2] [3]

Event Processing Improvements:

  • Changed event handlers in src/Socket/messages-recv.ts to use the appropriate mutex for receipts (receiptMutex), notifications (notificationMutex), and messages (messageMutex) to ensure each type is processed in order independently. [1] [2] [3]
  • Updated app state patch processing in src/Socket/chats.ts to use appStatePatchMutex, ensuring app state patches are handled in order without blocking other event types.
  • Modified message sending logic in src/Socket/messages-send.ts to use messageMutex for appending messages, improving isolation between message events.

@whiskeysockets-bot

whiskeysockets-bot commented Dec 2, 2025

Copy link
Copy Markdown
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@Santosl2/Baileys-1#fix/create-individual-mutex

# Yarn (v2+)
yarn add @whiskeysockets/baileys@Santosl2/Baileys-1#fix/create-individual-mutex

# PNPM
pnpm add @whiskeysockets/baileys@Santosl2/Baileys-1#fix/create-individual-mutex

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

@Santosl2 Santosl2 changed the title Fix/create individual mutex fix: messages.upsert now works fine Dec 2, 2025
@Santosl2 Santosl2 changed the title fix: messages.upsert now works fine feat: Mutex redesign - isolate event processing for better ordering and throughput Dec 2, 2025
@purpshell

Copy link
Copy Markdown
Member

@jlucaso1 please evaluate performance impact and review, seems important

@jlucaso1

jlucaso1 commented Dec 5, 2025

Copy link
Copy Markdown
Collaborator

This is a good solution, but not the definitive, maybe the message.upsert is freezing in the main mutex caused by some deadlock. This approach will mitigate this and will help us to find where the deadlock is happening specifically. About the perfomance (memory usage and cpu) is fine and worth. We are only splitting principles.
LGTM

good work @Santosl2

@Santosl2

Santosl2 commented Dec 5, 2025

Copy link
Copy Markdown
Contributor Author

This is a good solution, but not the definitive, maybe the message.upsert is freezing in the main mutex caused by some deadlock. This approach will mitigate this and will help us to find where the deadlock is happening specifically. About the perfomance (memory usage and cpu) is fine and worth. We are only splitting principles.

LGTM

good work @Santosl2

Thx dude!

@purpshell
purpshell merged commit 829fa8d into WhiskeySockets:master Dec 12, 2025
4 checks passed
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