Program: 105790
Project: 105790
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(vm.getHypervisorType()),false);
  boolean stopped=false;
  StopAnswer answer=null;
  try {
    answer=(StopAnswer)_agentMgr.send(vm.getHostId(),stop);
    if (answer != null) {
      if (vm.getType() == VirtualMachine.Type.User) {
        String platform=answer.getPlatform();
        if (platform != null) {
          UserVmVO userVm=_userVmDao.findById(vm.getId());
          _userVmDao.loadDetails(userVm);
          userVm.setDetail("String_Node_Str",platform);
          _userVmDao.saveDetails(userVm);
        }
      }
      stopped=answer.getResult();
      if (!stopped) {
        throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
      }
      vmGuru.finalizeStop(profile,answer);
      GPUDeviceTO gpuDevice=stop.getGpuDevice();
      if (gpuDevice != null) {
        _resourceMgr.updateGPUDetails(vm.getHostId(),gpuDevice.getGroupDetails());
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str" + vm.instanceName);
    }
  }
 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);
  }
}

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) || (state == State.Stopping)) {
      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());
      }
    }
    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(vm.getHypervisorType()),false);
  boolean stopped=false;
  StopAnswer answer=null;
  try {
    answer=(StopAnswer)_agentMgr.send(vm.getHostId(),stop);
    if (answer != null) {
      if (vm.getType() == VirtualMachine.Type.User) {
        String platform=answer.getPlatform();
        if (platform != null) {
          UserVmVO userVm=_userVmDao.findById(vm.getId());
          _userVmDao.loadDetails(userVm);
          userVm.setDetail("String_Node_Str",platform);
          _userVmDao.saveDetails(userVm);
        }
      }
      stopped=answer.getResult();
      if (!stopped) {
        throw new CloudRuntimeException("String_Node_Str" + answer.getDetails());
      }
      vmGuru.finalizeStop(profile,answer);
      GPUDeviceTO gpuDevice=stop.getGpuDevice();
      if (gpuDevice != null) {
        _resourceMgr.updateGPUDetails(vm.getHostId(),gpuDevice.getGroupDetails());
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str" + vm.instanceName);
    }
  }
 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);
  }
}