Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-3038

Use of volatile in cpp library



    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 0.9.2, 0.9.3, 0.10.0
    • 0.10.0
    • C++ - Library
    • None


      In the cpp library there are several member variables which are declared volatile, I believe with the intention of providing some sort of thread-safety.
      While volatile can be used in this way in Java and C#, in C++ it cannot! It does not provide any guarantees with regard to instruction (re-)ordering, i.e. there are no implied memory barriers like you would get by using explicit locking or atomic variables.
      This means that all uses of volatile should be examined, the volatile qualifier should be removed and replaced by proper synchronization.

      The affected member variables are:

      1. (Issue moved to THRIFT-3972) NoStarveReadWriteMutex::writerWaiting_
        Unprotected read access in acquireRead(). Data race can be seen by running the unit test with Helgrind.
      2. (already fixed) TFileTransport::forceFlush_
        Always accessed while holding mutex_. In this case, the volatile can just be removed.
      3. (2nd pr: removed volatile) TFileTransport::closing_
        Sometimes accessed while holding mutex_ (in combination with the notEmpty_ Monitor),
        but, e.g., enqueueEvent reads closing_ without any synchronization.
      4. (already fixed) TThreadPoolServer::stop_, TThreadedServer::stop_
        Accessed (read and written) without synchronization. These would probably be fine using an atomic data type. Or, use explicit locking or signaling.
      5. (already fixed) TThreadPoolServer::timeout_, TThreadPoolServer::taskExpiration_
        Should probably use a lock.
      6. (2nd pr: removed volatile) Mutex.cpp has mutexProfilingCounter as static variable. This probably doesn’t break anything, but still the volatile serves no real purpose.

      While some of the fixes are probably simple, in general I think someone with better knowledge of the code should have a look at this.
      Items marked (already fixed) were addressed in https://github.com/apache/thrift/pull/981 in 0.10.0.




            jking3 James E. King III
            adriaan42 Adriaan Schmidt
            2 Vote for this issue
            8 Start watching this issue