[concurrency-interest] RRWL with 'bad' Thread.getId() implementations

Chris Dennis chris.w.dennis at gmail.com
Tue Jun 25 12:50:33 EDT 2013


Hi All,

While dealing with a customer issue, I ran in to the possibility of
breaking a RRWL by feeding in Thread instances with colliding thread-ids.
Inside RRWL the cachedHoldCounter logic assumes that getId() will return a
unique identifier for a thread.  Do people consider this a bug in RRWL or
not? (I think it would be agreed that this is also a bug in the
subclassing of Thread)

Regards,

Chris Dennis

public static void main(String[] args) {
  final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  final CyclicBarrier barrier = new CyclicBarrier(2);
    
  Thread t1 = new EvilThread() {
    public void run() {
      try {
        lock.readLock().lock();
        barrier.await();
        //T2 locks
        barrier.await();
        //T3 locks
        //T3 unlocks
        barrier.await();
        //T2 unlocks
        barrier.await();
        lock.readLock().unlock();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
      
  };
  Thread t2 = new EvilThread() {
    public void run() {
      try {
        //T1 locks
                  barrier.await();
                  lock.readLock().lock();
                  barrier.await();
                  //T3 locks
                  //T3 unlocks
                  barrier.await();
                  lock.readLock().unlock();
                  barrier.await();
                  //T1 unlocks
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
      
  };
  Thread t3 = new EvilThread() {
    public void run() {
      try {
        //T1 locks
        barrier.await();
                  //T2 locks
                  barrier.await();
                  lock.readLock().lock();
                  lock.readLock().unlock();
                  barrier.await();
                  //T2 unlocks
                  barrier.await();
                  //T3 unlocks
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
      
  };
    
  t1.start();
  t2.start();
  t3.start();
  }

  
  static class EvilThread extends Thread {
  public long getId() {
    return 42L;
  }
  }




More information about the Concurrency-interest mailing list