fix: data race in configureDevices with PropertyCollector in vcsim #3947
+2
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Use cloneDevice() to create a deep copy of the device list before modification, preventing race between AssignController writes and PropertyCollector's deepCopy reads.
I was hitting a race between
A data race occurs between two concurrent operations:
AssignController()inobject/virtual_device_list.go:547appends tocontroller.Deviceslice duringReconfigVMTaskhas been called.deepCopy()insimulator/object.go:51marshals VM properties (including the same controller) duringPropertyCollector.RetrievePropertiesEx()The fix is to use deep copy of devicelist to do config device.
With shared slice (without fix):
configureDevices() starts
├─> devices points to SAME array as vm.Config.Hardware.Device
├─> AssignController modifies controller.Device
│ └─> VM sees PARTIAL changes immediately (inconsistent state)
└─> ctx.Update() - already partially modified
With deep copy (with fix):
configureDevices() starts
├─> devices is a SEPARATE copy
├─> AssignController modifies the COPY
│ └─> VM still has OLD state (consistent)
└─> ctx.Update() replaces VM's devices with modified copy
└─> VM now has NEW state (consistent)
How Has This Been Tested?
It is hard to repo inside vcsim package with the whole
AssignControllercall .I wrote similiar code to mimic what AssignController did(Especially focus on directly inplace replacement device slice by append) , it 100% reproduce the same backtrace.
Guidelines
Please read and follow the
CONTRIBUTIONguidelines of this project.