Program: 76527
Project: 76527
Buggy source:
/** 
 * Determine which branch suceeds in rendezvousing. 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 perfromed direclt 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 each 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 none of thebranches were enabled.
 */
protected int chooseBranch(ConditionalBranch[] branches){
  try {
synchronized (_getInternalLock()) {
      _resetConditionalState();
      _threadList=new LinkedList();
      ConditionalBranch onlyBranch=null;
      for (int i=0; i < branches.length; i++) {
        if (branches[i].getGuard()) {
          Nameable act=(Nameable)branches[i].getParent();
          String name=act.getName() + branches[i].getID();
          Thread t=new Thread((Runnable)branches[i],name);
          _threadList.insertFirst(t);
          onlyBranch=branches[i];
        }
      }
      int num=_threadList.size();
      if (num == 0) {
        return _successfulBranch;
      }
 else       if (num == 1) {
        if (onlyBranch instanceof ConditionalSend) {
          Token t=onlyBranch._token;
          onlyBranch.getReceiver().put(t);
          return onlyBranch.getID();
        }
 else {
          Token tmp=onlyBranch.getReceiver().get();
          onlyBranch._token=tmp;
          return onlyBranch.getID();
        }
      }
 else {
        Enumeration threads=_threadList.elements();
        while (threads.hasMoreElements()) {
          Thread thread=(Thread)threads.nextElement();
          thread.start();
          _branchesActive++;
        }
        _branchesStarted=_branchesActive;
        while ((_successfulBranch == -1) && (_branchesActive > 0)) {
          _getInternalLock().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.insertFirst(rec);
        branches[i].setAlive(false);
      }
    }
    (new NotifyThread(tmp)).start();
synchronized (_getInternalLock()) {
      while (_branchesActive != 0) {
        _getInternalLock().wait();
      }
      if (_branchesActive != 0) {
        throw new InvalidStateException(getName() + "String_Node_Str" + "String_Node_Str");
      }
    }
  }
 catch (  InterruptedException ex) {
    throw new TerminateProcessException(this,"String_Node_Str" + "String_Node_Str");
  }
  if (_successfulBranch == -1) {
    throw new TerminateProcessException(this,"String_Node_Str" + "String_Node_Str");
  }
  _threadList=null;
  return _successfulBranch;
}

Fixed version (ground truth):
/** 
 * Determine which branch suceeds in rendezvousing. 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 perfromed direclt 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 each 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 none of thebranches were enabled.
 */
protected int chooseBranch(ConditionalBranch[] branches){
  try {
synchronized (_getInternalLock()) {
      _resetConditionalState();
      _threadList=new LinkedList();
      ConditionalBranch onlyBranch=null;
      for (int i=0; i < branches.length; i++) {
        if (branches[i].getGuard()) {
          Nameable act=(Nameable)branches[i].getParent();
          String name=act.getName() + branches[i].getID();
          Thread t=new Thread((Runnable)branches[i],name);
          _threadList.insertFirst(t);
          onlyBranch=branches[i];
        }
      }
      int num=_threadList.size();
      if (num == 0) {
        return _successfulBranch;
      }
 else       if (num == 1) {
        if (onlyBranch instanceof ConditionalSend) {
          Token t=onlyBranch._token;
          onlyBranch.getReceiver().put(t);
          return onlyBranch.getID();
        }
 else {
          Token tmp=onlyBranch.getReceiver().get();
          onlyBranch._token=tmp;
          return onlyBranch.getID();
        }
      }
 else {
        Enumeration threads=_threadList.elements();
        while (threads.hasMoreElements()) {
          Thread thread=(Thread)threads.nextElement();
          thread.start();
          _branchesActive++;
        }
        _branchesStarted=_branchesActive;
        while ((_successfulBranch == -1) && (_branchesActive > 0)) {
          _getInternalLock().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.insertFirst(rec);
        branches[i].setAlive(false);
      }
    }
    (new NotifyThread(tmp)).start();
synchronized (_getInternalLock()) {
      while (_branchesActive != 0) {
        _getInternalLock().wait();
      }
      if (_branchesActive != 0) {
        throw new InvalidStateException(getName() + "String_Node_Str" + "String_Node_Str");
      }
    }
  }
 catch (  InterruptedException ex) {
    throw new TerminateProcessException(this,"String_Node_Str" + "String_Node_Str");
  }
  if (_successfulBranch == -1) {
    if (_blocked) {
      ((CSPDirector)getDirector())._actorUnblocked();
    }
    throw new TerminateProcessException(this,"String_Node_Str" + "String_Node_Str");
  }
  _threadList=null;
  return _successfulBranch;
}