Program: 76166
Project: 76166
Buggy source:
/** 
 * Determine which branch succeeds with a rendezvous. This method is central to nondeterministic rendezvous. It is passed in an array of branches, each element of which represents one of the conditional rendezvous branches. If the guard for the branch is false then the branch is not enabled.  It returns the id of the successful branch, or -1 if none of the branches were enabled. <p> If exactly one branch is enabled, then the communication is performed directly and the id of the enabled branch  is returned. If more than one branch is enabled, a thread  is created and started for each enabled branch. These threads try to rendezvous until one succeeds. After a thread succeeds the other threads are killed, and the id of the successful branch is returned. <p>
 * @param branches The set of conditional branches involved.
 * @return The ID of the successful branch, or -1 if none of thebranches were enabled.
 */
public int chooseBranch(ConditionalBranch[] branches){
  try {
synchronized (_internalLock) {
      _resetConditionalState();
      _threadList=new LinkedList();
      ConditionalBranch onlyBranch=null;
      for (int i=0; i < branches.length; i++) {
        if (branches[i].getGuard()) {
          Nameable act=(Nameable)branches[i].getController().getParent();
          String name=act.getName() + branches[i].getID();
          Thread t=new Thread((Runnable)branches[i],name);
          _threadList.add(0,t);
          onlyBranch=branches[i];
        }
      }
      int num=_threadList.size();
      if (num == 0) {
        return _successfulBranch;
      }
 else       if (num == 1) {
        if (onlyBranch instanceof ConditionalSend) {
          Token t=onlyBranch.getToken();
          onlyBranch.getReceiver().put(t);
          return onlyBranch.getID();
        }
 else {
          Token tmp=onlyBranch.getReceiver().get();
          onlyBranch.setToken(tmp);
          return onlyBranch.getID();
        }
      }
 else {
        Iterator threads=_threadList.iterator();
        while (threads.hasNext()) {
          Thread thread=(Thread)threads.next();
          thread.start();
          _branchesActive++;
        }
        _branchesStarted=_branchesActive;
        while ((_successfulBranch == -1) && (_branchesActive > 0)) {
          _internalLock.wait();
        }
      }
    }
    LinkedList tmp=new LinkedList();
    for (int i=0; i < branches.length; i++) {
      if ((i != _successfulBranch) && (branches[i].getGuard())) {
        Receiver rec=branches[i].getReceiver();
        tmp.add(0,rec);
        branches[i].setAlive(false);
      }
    }
    (new NotifyThread(tmp)).start();
synchronized (_internalLock) {
      while (_branchesActive != 0) {
        _internalLock.wait();
      }
      if (_branchesActive != 0) {
        throw new InvalidStateException(((Nameable)getParent()).getName() + "String_Node_Str" + "String_Node_Str");
      }
    }
  }
 catch (  InterruptedException ex) {
    throw new TerminateProcessException(((Nameable)getParent()).getName() + "String_Node_Str");
  }
  if (_successfulBranch == -1) {
    if (_blocked) {
    }
    throw new TerminateProcessException(((Nameable)getParent()).getName() + "String_Node_Str" + "String_Node_Str");
  }
  _threadList=null;
  return _successfulBranch;
}

Fixed version (ground truth):
/** 
 * Determine which branch succeeds with a rendezvous. This method is central to nondeterministic rendezvous. It is passed in an array of branches, each element of which represents one of the conditional rendezvous branches. If the guard for the branch is false then the branch is not enabled.  It returns the id of the successful branch, or -1 if none of the branches were enabled. <p> If exactly one branch is enabled, then the communication is performed directly and the id of the enabled branch  is returned. If more than one branch is enabled, a thread  is created and started for each enabled branch. These threads try to rendezvous until one succeeds. After a thread succeeds the other threads are killed, and the id of the successful branch is returned. <p>
 * @param branches The set of conditional branches involved.
 * @return The ID of the successful branch, or -1 if none of thebranches were enabled.
 */
public int chooseBranch(ConditionalBranch[] branches){
  try {
synchronized (_internalLock) {
      _resetConditionalState();
      _threadList=new LinkedList();
      ConditionalBranch onlyBranch=null;
      for (int i=0; i < branches.length; i++) {
        if (branches[i].getGuard()) {
          Nameable act=(Nameable)branches[i].getController().getParent();
          String name=act.getName() + branches[i].getID();
          Thread t=new Thread((Runnable)branches[i],name);
          _threadList.add(0,t);
          onlyBranch=branches[i];
        }
      }
      int num=_threadList.size();
      if (num == 0) {
        return _successfulBranch;
      }
 else       if (num == 1) {
        if (onlyBranch instanceof ConditionalSend) {
          Token t=onlyBranch.getToken();
          onlyBranch.getReceiver().put(t);
          return onlyBranch.getID();
        }
 else {
          Token tmp=onlyBranch.getReceiver().get();
          onlyBranch.setToken(tmp);
          return onlyBranch.getID();
        }
      }
 else {
        Iterator threads=_threadList.iterator();
        while (threads.hasNext()) {
          Thread thread=(Thread)threads.next();
          thread.start();
          _branchesActive++;
        }
        _branchesStarted=_branchesActive;
        while ((_successfulBranch == -1) && (_branchesActive > 0)) {
          _internalLock.wait();
        }
      }
    }
    LinkedList tmp=new LinkedList();
    for (int i=0; i < branches.length; i++) {
      if ((i != _successfulBranch) && (branches[i].getGuard())) {
        Receiver rec=branches[i].getReceiver();
        tmp.add(0,rec);
        branches[i].setAlive(false);
      }
    }
    (new NotifyThread(tmp)).start();
synchronized (_internalLock) {
      while (_branchesActive != 0) {
        _internalLock.wait();
      }
      if (_branchesActive != 0) {
        throw new InvalidStateException(((Nameable)getParent()).getName() + "String_Node_Str" + "String_Node_Str");
      }
    }
  }
 catch (  InterruptedException ex) {
    throw new TerminateProcessException(((Nameable)getParent()).getName() + "String_Node_Str");
  }
  if (_successfulBranch == -1) {
    if (_blocked) {
      _getDirector()._actorUnBlocked(new CSPReceiver());
    }
    throw new TerminateProcessException(((Nameable)getParent()).getName() + "String_Node_Str" + "String_Node_Str");
  }
  _threadList=null;
  return _successfulBranch;
}