-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[C++] Fix race condition in BlockingQueue #8765
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great find!
We have a fix of the integration tests. Please rebase your code. Thanks. |
f7b7f4c
to
12ebdc0
Compare
Rebase and force push without code changed. |
You need to rebase to the latest master again because a spotbugs check issue was fixed recently by #8819 |
12ebdc0
to
2dc4977
Compare
Rebased again. |
### Motivation BlockingQueue has race condition that can cause threads waiting forever in multithreading environment. ProducerImpl uses BlockingQueue as pendingMessagesQueue_ and can be blocked forever at it. This PR fixes race condition in BlockingQueue. #### Race condition details https://github.com/apache/pulsar/blob/91e2f832178d9ffd5d78161145d895910296c2d9/pulsar-client-cpp/lib/BlockingQueue.h#L172-L185 Use BlockingQueue::Pop as example, its procedure is: 1. lock 2. check wasFull and then change queue state 3. unlock 4. if wasFull, notify one thread waiting at queueFullCondition Race condition sequence: 1. queue is full and there are multiple threads waiting on queueFullCondition 2. thread A call Pop, lock, wasFull is true, unlock -> queue has one free space 3. thread B call Pop, lock, wasFull is false, unlock -> queue has two free spaces 4. thread A notify one thread waiting at queueFullCondition 5. queue is no loger full again 6. result: except one thread is notified by A, other threads waiting on queueFullCondition are waiting forever ### Modifications * Use notify_all instead of notify_one to notify threads waiting on condition variables Reason: Currently only notify threads when queue is full or empty. After unlock, other threads may change queue state, so thread to notify condition can not determine how queue state changed and should use notify_all in case of more then one change occured. ### Verifying this change - Add a test case BlockingQueueTest.testPushPopRace to test concurrent push and pop (cherry picked from commit 18b3876)
### Motivation BlockingQueue has race condition that can cause threads waiting forever in multithreading environment. ProducerImpl uses BlockingQueue as pendingMessagesQueue_ and can be blocked forever at it. This PR fixes race condition in BlockingQueue. #### Race condition details https://github.com/apache/pulsar/blob/91e2f832178d9ffd5d78161145d895910296c2d9/pulsar-client-cpp/lib/BlockingQueue.h#L172-L185 Use BlockingQueue::Pop as example, its procedure is: 1. lock 2. check wasFull and then change queue state 3. unlock 4. if wasFull, notify one thread waiting at queueFullCondition Race condition sequence: 1. queue is full and there are multiple threads waiting on queueFullCondition 2. thread A call Pop, lock, wasFull is true, unlock -> queue has one free space 3. thread B call Pop, lock, wasFull is false, unlock -> queue has two free spaces 4. thread A notify one thread waiting at queueFullCondition 5. queue is no loger full again 6. result: except one thread is notified by A, other threads waiting on queueFullCondition are waiting forever ### Modifications * Use notify_all instead of notify_one to notify threads waiting on condition variables Reason: Currently only notify threads when queue is full or empty. After unlock, other threads may change queue state, so thread to notify condition can not determine how queue state changed and should use notify_all in case of more then one change occured. ### Verifying this change - Add a test case BlockingQueueTest.testPushPopRace to test concurrent push and pop (cherry picked from commit 18b3876)
Motivation
BlockingQueue has race condition that can cause threads waiting forever in multithreading environment. ProducerImpl uses BlockingQueue as pendingMessagesQueue_ and can be blocked forever at it. This PR fixes race condition in BlockingQueue.
Race condition details
pulsar/pulsar-client-cpp/lib/BlockingQueue.h
Lines 172 to 185 in 91e2f83
Use BlockingQueue::Pop as example, its procedure is:
Race condition sequence:
Modifications
Reason: Currently only notify threads when queue is full or empty. After unlock, other threads may change queue state, so thread to notify condition can not determine how queue state changed and should use notify_all in case of more then one change occured.
Verifying this change
Does this pull request potentially affect one of the following parts:
Documentation