Skip to content

fix(module): add idempotency for usb cleanup in virtualization-dra#2216

Open
danilrwx wants to merge 4 commits intomainfrom
fix/usb/dra
Open

fix(module): add idempotency for usb cleanup in virtualization-dra#2216
danilrwx wants to merge 4 commits intomainfrom
fix/usb/dra

Conversation

@danilrwx
Copy link
Copy Markdown
Contributor

@danilrwx danilrwx commented Apr 14, 2026

Description

Add idempotency to USB device cleanup in the virtualization-dra component.

Changes:

  • store.go: Add early return if claim is already unprepared. Use hasCount check to handle missing or zero counts gracefully. Replace if/else with switch for clearer count handling (0, 1, >1).
  • usbgateway.go: Make Detach idempotent — log and skip if device is already detached instead of failing. Always clean up the attach record after detach attempt.
  • attach_record.go: Add RemoveEntryByDeviceName method to remove a specific device entry from the attach record. Extract storeLocked helper to avoid code duplication.

Why do we need it, and what problem does it solve?

When UnprepareResources is called multiple times for the same claim (e.g., during retries or race conditions), the cleanup logic could fail or produce incorrect results:

  • Detaching an already-detached device caused errors.
  • The attach record was not cleaned up if the device was already detached.
  • The usbipAllocatedDevicesCount map could be accessed for non-existent keys, leading to incorrect behavior.

After this fix, repeated cleanup calls are safe and produce consistent results.

What is the expected result?

  1. Call UnprepareResources for a claim that has USB devices attached via USBGateway.
  2. Devices are detached and attach records are removed.
  3. Call UnprepareResources again for the same claim.
  4. No errors occur — the operation is a no-op.

Checklist

  • The code is covered by unit tests.
  • e2e tests passed.
  • Documentation updated according to the changes.
  • Changes were tested in the Kubernetes cluster manually.

Changelog entries

section: module
type: fix
summary: Add idempotency for USB device cleanup in virtualization-dra to prevent errors on repeated unprepare calls.
impact_level: low

Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
@danilrwx danilrwx marked this pull request as ready for review April 14, 2026 16:58
@danilrwx danilrwx added this to the v1.8.0 milestone Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant