Program: 106163
Project: 106163
Buggy source:
private void advanceStop(VMInstanceVO vm,boolean cleanUpEvenIfUnableToStop) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  State state=vm.getState();
  if (state == State.Stopped) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return;
  }
  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;
  }
  ItWorkVO work=_workDao.findByOutstandingWork(vm.getId(),vm.getState());
  if (work != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ vm.getState()+ "String_Node_Str"+ work.getId());
    }
  }
  Long hostId=vm.getHostId();
  if (hostId == null) {
    if (!cleanUpEvenIfUnableToStop) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
    try {
      stateTransitTo(vm,Event.AgentReportStopped,null,null);
    }
 catch (    NoTransitionException e) {
      s_logger.warn(e.getMessage());
    }
    if (work != null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + work.getId());
      }
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
    return;
  }
  VirtualMachineGuru vmGuru=getVmGuru(vm);
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm);
  try {
    if (!stateTransitTo(vm,Event.StopRequested,vm.getHostId())) {
      throw new ConcurrentOperationException("String_Node_Str");
    }
  }
 catch (  NoTransitionException e1) {
    if (!cleanUpEvenIfUnableToStop) {
      throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
    }
    boolean doCleanup=false;
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str");
    }
    if (state == State.Starting || state == State.Migrating) {
      if (work != null) {
        doCleanup=true;
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vm + "String_Node_Str");
        }
        throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
    }
 else     if (state == State.Stopping) {
      doCleanup=true;
    }
    if (doCleanup) {
      if (cleanup(vmGuru,new VirtualMachineProfileImpl(vm),work,Event.StopRequested,cleanUpEvenIfUnableToStop)) {
        try {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + work.getId());
          }
          if (!changeState(vm,Event.AgentReportStopped,null,work,Step.Done)) {
            throw new CloudRuntimeException("String_Node_Str" + vm);
          }
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
          throw new CloudRuntimeException("String_Node_Str" + vm,e);
        }
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vm);
        }
        throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
    }
  }
  if (vm.getState() != State.Stopping) {
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
  }
  vmGuru.prepareStop(profile);
  StopCommand stop=new StopCommand(vm,getExecuteInSequence());
  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());
    }
 else {
      Integer timeoffset=answer.getTimeOffset();
      if (timeoffset != null) {
        if (vm.getType() == VirtualMachine.Type.User) {
          UserVmVO userVm=_userVmDao.findById(vm.getId());
          _userVmDao.loadDetails(userVm);
          userVm.setDetail("String_Node_Str",timeoffset.toString());
          _userVmDao.saveDetails(userVm);
        }
      }
    }
    vmGuru.finalizeStop(profile,answer);
  }
 catch (  AgentUnavailableException e) {
    s_logger.warn("String_Node_Str" + e.toString());
    if (!forced) {
      throw e;
    }
  }
catch (  OperationTimedoutException e) {
    s_logger.warn("String_Node_Str" + e.toString());
    if (!forced) {
      throw e;
    }
  }
 finally {
    if (!stopped) {
      if (!cleanUpEvenIfUnableToStop) {
        s_logger.warn("String_Node_Str" + vm);
        try {
          stateTransitTo(vm,Event.OperationFailed,vm.getHostId());
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
        }
        throw new CloudRuntimeException("String_Node_Str" + vm);
      }
 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,cleanUpEvenIfUnableToStop);
    s_logger.debug("String_Node_Str" + vm);
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (vm.getHypervisorType() != HypervisorType.BareMetal) {
      volumeMgr.release(profile);
      s_logger.debug("String_Node_Str" + vm);
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (work != null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + work.getId());
      }
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
    if (!stateTransitTo(vm,Event.OperationSucceeded,null)) {
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e) {
    s_logger.warn(e.getMessage());
    throw new CloudRuntimeException("String_Node_Str" + vm);
  }
}

Fixed version (ground truth):
private void advanceStop(VMInstanceVO vm,boolean cleanUpEvenIfUnableToStop) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  State state=vm.getState();
  if (state == State.Stopped) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return;
  }
  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;
  }
  ItWorkVO work=_workDao.findByOutstandingWork(vm.getId(),vm.getState());
  if (work != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ vm.getState()+ "String_Node_Str"+ work.getId());
    }
  }
  Long hostId=vm.getHostId();
  if (hostId == null) {
    if (!cleanUpEvenIfUnableToStop) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
    try {
      stateTransitTo(vm,Event.AgentReportStopped,null,null);
    }
 catch (    NoTransitionException e) {
      s_logger.warn(e.getMessage());
    }
    if (work != null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + work.getId());
      }
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
    return;
  }
  VirtualMachineGuru vmGuru=getVmGuru(vm);
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm);
  try {
    if (!stateTransitTo(vm,Event.StopRequested,vm.getHostId())) {
      throw new ConcurrentOperationException("String_Node_Str");
    }
  }
 catch (  NoTransitionException e1) {
    if (!cleanUpEvenIfUnableToStop) {
      throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
    }
    boolean doCleanup=false;
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str");
    }
    if (state == State.Starting || state == State.Migrating) {
      if (work != null) {
        doCleanup=true;
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vm + "String_Node_Str");
        }
        throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
    }
 else     if (state == State.Stopping) {
      doCleanup=true;
    }
    if (doCleanup) {
      if (cleanup(vmGuru,new VirtualMachineProfileImpl(vm),work,Event.StopRequested,cleanUpEvenIfUnableToStop)) {
        try {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + work.getId());
          }
          if (!changeState(vm,Event.AgentReportStopped,null,work,Step.Done)) {
            throw new CloudRuntimeException("String_Node_Str" + vm);
          }
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
          throw new CloudRuntimeException("String_Node_Str" + vm,e);
        }
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vm);
        }
        throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
      }
    }
  }
  if (vm.getState() != State.Stopping) {
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState());
  }
  vmGuru.prepareStop(profile);
  StopCommand stop=new StopCommand(vm,getExecuteInSequence());
  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());
    }
 else {
      Integer timeoffset=answer.getTimeOffset();
      if (timeoffset != null) {
        if (vm.getType() == VirtualMachine.Type.User) {
          UserVmVO userVm=_userVmDao.findById(vm.getId());
          _userVmDao.loadDetails(userVm);
          userVm.setDetail("String_Node_Str",timeoffset.toString());
          _userVmDao.saveDetails(userVm);
        }
      }
    }
    vmGuru.finalizeStop(profile,answer);
  }
 catch (  AgentUnavailableException e) {
    s_logger.warn("String_Node_Str" + e.toString());
  }
catch (  OperationTimedoutException e) {
    s_logger.warn("String_Node_Str" + e.toString());
  }
 finally {
    if (!stopped) {
      if (!cleanUpEvenIfUnableToStop) {
        s_logger.warn("String_Node_Str" + vm);
        try {
          stateTransitTo(vm,Event.OperationFailed,vm.getHostId());
        }
 catch (        NoTransitionException e) {
          s_logger.warn("String_Node_Str" + vm);
        }
        throw new CloudRuntimeException("String_Node_Str" + vm);
      }
 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,cleanUpEvenIfUnableToStop);
    s_logger.debug("String_Node_Str" + vm);
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (vm.getHypervisorType() != HypervisorType.BareMetal) {
      volumeMgr.release(profile);
      s_logger.debug("String_Node_Str" + vm);
    }
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str",e);
  }
  try {
    if (work != null) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + work.getId());
      }
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
    if (!stateTransitTo(vm,Event.OperationSucceeded,null)) {
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e) {
    s_logger.warn(e.getMessage());
    throw new CloudRuntimeException("String_Node_Str" + vm);
  }
}