Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.3k stars 1.96k forks source link

[BUG] `com.azure.resourcemanager.compute.models.VirtualMachines.list()` query returns deleted VM #35524

Open qooiprww opened 1 year ago

qooiprww commented 1 year ago

Describe the bug com.azure.resourcemanager.compute.models.VirtualMachines.list() query returns deleted VM. Fetching details using the returned VM object will result in ResourceNotFound ApiErrorException

To Reproduce

  1. Manually create a VM from Azure Portal
  2. Query VirtualMachines using com.azure.resourcemanager.compute.models.VirtualMachines.list()
  3. Delete VM from Azure Portal
  4. Query VirtualMachines using com.azure.resourcemanager.compute.models.VirtualMachines.list() with the same client
  5. Find object of deleted VM instance from query result and retrieve powerState from found object to trigger refreshInstanceView
  6. refreshInstanceView will trigger ApiErrorException with code ResourceNotFound
joshfree commented 1 year ago

/cc @weidongxu-microsoft

weidongxu-microsoft commented 1 year ago

@qooiprww

Delete in step 3 takes time. It could be a minutes or 2. Meanwhile, List in step 4 could still return the VM.

But all depends on timing. If now in step5, when you call the refreshInstanceView, the VM just get deleted in backend, you will have error. If you do the refreshInstanceView when you know the VM may be in the process of getting deleted, please do error handling in your code.

qooiprww commented 1 year ago

it appears that step 4 is still returning the deleted VM after the delete process is finished.

v-hongli1 commented 1 year ago

@qooiprww Although the virtual machine has been deleted in step 3, the provisioningState of virtual machine remains in Deleting because the backend process has not been completed, so the List in step 4 can still return the VM. I think it is necessary to add filter .filter(virtualMachine -> !"Deleting".equals(virtualMachine.provisioningState())) for com.azure.resourcemanager.compute.models.VirtualMachines.list().

qooiprww commented 1 year ago

Thanks! I'll give it a try and monitor for exceptions.

qooiprww commented 1 year ago

I did some testing with deleted VMs: After deleting the VM and verified that it's no longer visible in the Resource Group, the com.azure.resourcemanager.compute.models.VirtualMachines.list() request still returned deleted vm with provisioningState Succeeded

image
v-hongli1 commented 1 year ago

@qooiprww Please elaborate on how you did step 3. If you are doing this via APIs, please provide relevant test code snippets, thanks!