Program: 106947
Project: 106947
Buggy source:
@Override public <T extends VMInstanceVO>boolean advanceStop(T vm,boolean forced,User user,Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  long vmId=vm.getId();
  State state=vm.getState();
  if (state == State.Stopped) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return true;
  }
  if (state == State.Destroyed || state == State.Expunging || state == State.Error) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ state);
    }
    return true;
  }
  VirtualMachineGuru<T> vmGuru=getVmGuru(vm);
  try {
    if (!stateTransitTo(vm,Event.StopRequested,vm.getHostId())) {
      throw new ConcurrentOperationException("String_Node_Str");
    }
  }
 catch (  NoTransitionException e1) {
    if (!forced) {
      throw new ConcurrentOperationException("String_Node_Str");
    }
    vm=vmGuru.findById(vmId);
    if (vm == null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + vmId);
      }
      return true;
    }
  }
  VirtualMachineProfile<T> profile=new VirtualMachineProfileImpl<T>(vm);
  if ((vm.getState() == State.Starting || vm.getState() == State.Stopping || vm.getState() == State.Migrating) && forced) {
    ItWorkVO work=_workDao.findByOutstandingWork(vm.getId(),vm.getState());
    if (work != null) {
      if (cleanup(vmGuru,new VirtualMachineProfileImpl<T>(vm),work,Event.StopRequested,forced,user,account)) {
        try {
          return stateTransitTo(vm,Event.AgentReportStopped,null);
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
          return false;
        }
      }
    }
  }
  if (vm.getHostId() != null) {
    String routerPrivateIp=null;
    if (vm.getType() == VirtualMachine.Type.DomainRouter) {
      routerPrivateIp=vm.getPrivateIpAddress();
    }
    StopCommand stop=new StopCommand(vm,vm.getInstanceName(),null,routerPrivateIp);
    boolean stopped=false;
    StopAnswer answer=null;
    try {
      answer=(StopAnswer)_agentMgr.send(vm.getHostId(),stop);
      stopped=answer.getResult();
      if (!stopped) {
        throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
      }
      vmGuru.finalizeStop(profile,answer);
    }
 catch (    AgentUnavailableException e) {
    }
catch (    OperationTimedoutException e) {
    }
 finally {
      if (!stopped) {
        if (!forced) {
          s_logger.warn("String_Node_Str" + vm);
          try {
            stateTransitTo(vm,Event.OperationFailed,vm.getHostId());
          }
 catch (          NoTransitionException e) {
            s_logger.warn("String_Node_Str" + vm);
          }
          return false;
        }
 else {
          s_logger.warn("String_Node_Str" + vm + "String_Node_Str");
          vmGuru.finalizeStop(profile,answer);
        }
      }
    }
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug(vm + "String_Node_Str");
  }
  try {
    _networkMgr.release(profile,forced);
    s_logger.debug("String_Node_Str" + vm);
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (vm.getHypervisorType() != HypervisorType.BareMetal) {
      _storageMgr.release(profile);
      s_logger.debug("String_Node_Str" + vm);
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  vm.setReservationId(null);
  try {
    return stateTransitTo(vm,Event.OperationSucceeded,null);
  }
 catch (  NoTransitionException e) {
    s_logger.warn(e.getMessage());
    return false;
  }
}

Fixed version (ground truth):
@Override public <T extends VMInstanceVO>boolean advanceStop(T vm,boolean forced,User user,Account account) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  State state=vm.getState();
  if (state == State.Stopped) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return true;
  }
  if (state == State.Destroyed || state == State.Expunging || state == State.Error) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ state);
    }
    return true;
  }
  Long hostId=vm.getHostId();
  if (hostId == null) {
    try {
      stateTransitTo(vm,Event.AgentReportStopped,null,null);
    }
 catch (    NoTransitionException e) {
      s_logger.warn(e.getMessage());
    }
    return true;
  }
  VirtualMachineGuru<T> vmGuru=getVmGuru(vm);
  try {
    if (!stateTransitTo(vm,forced ? Event.AgentReportStopped : Event.StopRequested,vm.getHostId(),null)) {
      throw new ConcurrentOperationException("String_Node_Str");
    }
  }
 catch (  NoTransitionException e1) {
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
  }
  VirtualMachineProfile<T> profile=new VirtualMachineProfileImpl<T>(vm);
  if ((vm.getState() == State.Starting || vm.getState() == State.Stopping || vm.getState() == State.Migrating) && forced) {
    ItWorkVO work=_workDao.findByOutstandingWork(vm.getId(),vm.getState());
    if (work != null) {
      if (cleanup(vmGuru,new VirtualMachineProfileImpl<T>(vm),work,Event.StopRequested,forced,user,account)) {
        try {
          return stateTransitTo(vm,Event.AgentReportStopped,null);
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
          return false;
        }
      }
    }
  }
  if (vm.getHostId() != null) {
    String routerPrivateIp=null;
    if (vm.getType() == VirtualMachine.Type.DomainRouter) {
      routerPrivateIp=vm.getPrivateIpAddress();
    }
    StopCommand stop=new StopCommand(vm,vm.getInstanceName(),null,routerPrivateIp);
    boolean stopped=false;
    StopAnswer answer=null;
    try {
      answer=(StopAnswer)_agentMgr.send(vm.getHostId(),stop);
      stopped=answer.getResult();
      if (!stopped) {
        throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
      }
      vmGuru.finalizeStop(profile,answer);
    }
 catch (    AgentUnavailableException e) {
    }
catch (    OperationTimedoutException e) {
    }
 finally {
      if (!stopped) {
        if (!forced) {
          s_logger.warn("String_Node_Str" + vm);
          try {
            stateTransitTo(vm,Event.OperationFailed,vm.getHostId());
          }
 catch (          NoTransitionException e) {
            s_logger.warn("String_Node_Str" + vm);
          }
          return false;
        }
 else {
          s_logger.warn("String_Node_Str" + vm + "String_Node_Str");
          vmGuru.finalizeStop(profile,answer);
        }
      }
    }
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug(vm + "String_Node_Str");
  }
  try {
    _networkMgr.release(profile,forced);
    s_logger.debug("String_Node_Str" + vm);
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (vm.getHypervisorType() != HypervisorType.BareMetal) {
      _storageMgr.release(profile);
      s_logger.debug("String_Node_Str" + vm);
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  vm.setReservationId(null);
  try {
    return stateTransitTo(vm,Event.OperationSucceeded,null);
  }
 catch (  NoTransitionException e) {
    s_logger.warn(e.getMessage());
    return false;
  }
}