Program: 106039
Project: 106039
Buggy source:
protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  if (vm == null || vm.getRemoved() != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return;
  }
  advanceStop(vm.getUuid(),false);
  vm=_vmDao.findByUuid(vm.getUuid());
  try {
    if (!stateTransitTo(vm,VirtualMachine.Event.ExpungeOperation,vm.getHostId())) {
      s_logger.debug("String_Node_Str" + vm);
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e) {
    s_logger.debug("String_Node_Str" + vm);
    throw new CloudRuntimeException("String_Node_Str" + vm,e);
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm);
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vm.getHypervisorType());
  s_logger.debug("String_Node_Str");
  List<Command> nicExpungeCommands=hvGuru.finalizeExpungeNics(vm,profile.getNics());
  _networkMgr.cleanupNics(profile);
  s_logger.debug("String_Node_Str");
  List<Command> volumeExpungeCommands=hvGuru.finalizeExpungeVolumes(vm);
  Long hostId=vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
  if (volumeExpungeCommands != null && hostId != null) {
    Commands cmds=new Commands(Command.OnError.Stop);
    for (    Command volumeExpungeCommand : volumeExpungeCommands) {
      cmds.addCommand(volumeExpungeCommand);
    }
    _agentMgr.send(hostId,cmds);
    if (!cmds.isSuccessful()) {
      for (      Answer answer : cmds.getAnswers()) {
        if (!answer.getResult()) {
          s_logger.warn("String_Node_Str" + answer.getDetails());
          throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ answer.getDetails());
        }
      }
    }
  }
  if (hostId != null) {
    volumeMgr.disconnectVolumesFromHost(vm.getId(),hostId);
  }
  volumeMgr.cleanupVolumes(vm.getId());
  VirtualMachineGuru guru=getVmGuru(vm);
  guru.finalizeExpunge(vm);
  _uservmDetailsDao.removeDetails(vm.getId());
  List<Command> finalizeExpungeCommands=hvGuru.finalizeExpunge(vm);
  if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) {
    if (hostId != null) {
      Commands cmds=new Commands(Command.OnError.Stop);
      for (      Command command : finalizeExpungeCommands) {
        cmds.addCommand(command);
      }
      if (nicExpungeCommands != null) {
        for (        Command command : nicExpungeCommands) {
          cmds.addCommand(command);
        }
      }
      _agentMgr.send(hostId,cmds);
      if (!cmds.isSuccessful()) {
        for (        Answer answer : cmds.getAnswers()) {
          if (!answer.getResult()) {
            s_logger.warn("String_Node_Str" + answer.getDetails());
            throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ answer.getDetails());
          }
        }
      }
    }
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
}

Fixed version (ground truth):
protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException {
  if (vm == null || vm.getRemoved() != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    return;
  }
  advanceStop(vm.getUuid(),false);
  vm=_vmDao.findByUuid(vm.getUuid());
  try {
    if (!stateTransitTo(vm,VirtualMachine.Event.ExpungeOperation,vm.getHostId())) {
      s_logger.debug("String_Node_Str" + vm);
      throw new CloudRuntimeException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e) {
    s_logger.debug("String_Node_Str" + vm);
    throw new CloudRuntimeException("String_Node_Str" + vm,e);
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm);
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vm.getHypervisorType());
  s_logger.debug("String_Node_Str");
  List<Command> nicExpungeCommands=hvGuru.finalizeExpungeNics(vm,profile.getNics());
  _networkMgr.cleanupNics(profile);
  s_logger.debug("String_Node_Str");
  List<Command> volumeExpungeCommands=hvGuru.finalizeExpungeVolumes(vm);
  Long hostId=vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
  if (volumeExpungeCommands != null && volumeExpungeCommands.size() > 0 && hostId != null) {
    Commands cmds=new Commands(Command.OnError.Stop);
    for (    Command volumeExpungeCommand : volumeExpungeCommands) {
      cmds.addCommand(volumeExpungeCommand);
    }
    _agentMgr.send(hostId,cmds);
    if (!cmds.isSuccessful()) {
      for (      Answer answer : cmds.getAnswers()) {
        if (!answer.getResult()) {
          s_logger.warn("String_Node_Str" + answer.getDetails());
          throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ answer.getDetails());
        }
      }
    }
  }
  if (hostId != null) {
    volumeMgr.disconnectVolumesFromHost(vm.getId(),hostId);
  }
  volumeMgr.cleanupVolumes(vm.getId());
  VirtualMachineGuru guru=getVmGuru(vm);
  guru.finalizeExpunge(vm);
  _uservmDetailsDao.removeDetails(vm.getId());
  List<Command> finalizeExpungeCommands=hvGuru.finalizeExpunge(vm);
  if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) {
    if (hostId != null) {
      Commands cmds=new Commands(Command.OnError.Stop);
      for (      Command command : finalizeExpungeCommands) {
        cmds.addCommand(command);
      }
      if (nicExpungeCommands != null) {
        for (        Command command : nicExpungeCommands) {
          cmds.addCommand(command);
        }
      }
      _agentMgr.send(hostId,cmds);
      if (!cmds.isSuccessful()) {
        for (        Answer answer : cmds.getAnswers()) {
          if (!answer.getResult()) {
            s_logger.warn("String_Node_Str" + answer.getDetails());
            throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ answer.getDetails());
          }
        }
      }
    }
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
}