erobot opened a new pull request #8765:
URL: https://github.com/apache/pulsar/pull/8765


   ### 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
   
   ### Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API: (no)
     - The schema: (no)
     - The default values of configurations: (no)
     - The wire protocol: (no)
     - The rest endpoints: (no)
     - The admin cli options: (no)
     - Anything that affects deployment: (no)
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (no)


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to