Uploaded image for project: 'ZooKeeper'
  1. ZooKeeper
  2. ZOOKEEPER-1739

thread safe bug in FastLeaderElection: instance of WorkerSender is not safe published, WorkerSender thread may see that WorkerSender.manager is the default value null

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 3.4.5
    • None
    • leaderElection
    • None

    Description

      I am reading the trunk source code recently and find a thread-safe problem, but i'm not quite sure.

      in FastLeaderElection:

      class WorkerSender implements Runnable { 
          volatile boolean stop; 
          QuorumCnxManager manager; 
      
          WorkerSender(QuorumCnxManager manager){ 
              this.stop = false; 
              this.manager = manager; 
          } 
      
          public void run() {
       ...
          }
      }
      
      ...
      
      Messenger(QuorumCnxManager manager) {
      
          this.ws = new WorkerSender(manager);
      
          Thread t = new Thread(this.ws,
                  "WorkerSender[myid=" + self.getId() + "]");
          t.setDaemon(true);
          t.start();
      
          this.wr = new WorkerReceiver(manager);
      
          t = new Thread(this.wr,
                  "WorkerReceiver[myid=" + self.getId() + "]");
          t.setDaemon(true);
          t.start();
      }
      ...
      

      The instance of WorkerSender is constructed in main thread, and its field manager is assigned , and it is used in another thread. The later thread may see that WorkerSender.manager is the default value null. The solution may be:
      (1) change

       
      WorkerSender(QuorumCnxManager manager){ 
              this.stop = false; 
              this.manager = manager; 
      } 
      

      to

      WorkerSender(QuorumCnxManager manager){ 
      	this.manager = manager; 
      	this.stop = false; 
      } 
      

      or(2)
      change

      QuorumCnxManager manager; 
      

      to

      final QuorumCnxManager manager;
      

      Attachments

        1. ZOOKEEPER-1739.patch
          4 kB
          qingjie qiao

        Activity

          People

            qiaoqingjie qingjie qiao
            qiaoqingjie qingjie qiao
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: