GitHub user zentol opened a pull request:

    https://github.com/apache/flink/pull/5474

    [FLINK-8529][cassandra] Fix race condition

    ## What is the purpose of the change
    
    This PR fixes a deadlock that could happen if a callback is executed during 
`CassandraSinkBase#waitForPendingUpdates`.
    
    waitForPendingUpdates:
    ```
    U1: while (updatesPending.get() > 0)
    U2:     synchronized (updatesPending)
    U3:         updatesPending.wait();
    ```
    
    callback:
    ```
    C1: int pending = updatesPending.decrementAndGet();
    C2: if (pending == 0)
    C3: synchronized (updatesPending)
    C4:     updatesPending.notifyAll();
    ```
    
    Sequence causing deadlock: U1 -> C1 ... C4 -> U2 -> U3
    (`updatesPending == 1` at the start of sequence)
    
    This was fixed by switching lines U1 and U2:
    ```
    U2: synchronized (updatesPending)
    U1:         while (updatesPending.get() > 0)
    U3:             updatesPending.wait();
    ```
    
    If C1 runs
    * before U2, then waitForPendingUpdates sees that `updatesPending == 0` and 
exits without waiting
    * after U2, then waitForPendingUpdates is guaranteed to call wait() before 
the callback calls notifyAll()
    
    ## Verifying this change
    
    The deadlock was reproduced by introducing `OneShotLatches` into the 
callback/waitForPendingUpdates to force the above execution sequence.
    
    Don't think we can test this properly since it's a timing problem.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/zentol/flink 8520

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/flink/pull/5474.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #5474
    
----
commit 88b928b16228245756de12094e1d8a116caf0843
Author: zentol <chesnay@...>
Date:   2018-02-13T11:10:22Z

    [FLINK-8529][cassandra] Fix race condition

----


---

Reply via email to