Skip to content

Fix gs_usb USB resource cleanup on Windows - #2106

Open
dmg210 wants to merge 1 commit into
hardbyte:mainfrom
dmg210:main
Open

dmg210 wants to merge 1 commit into
hardbyte:mainfrom
dmg210:main

Conversation

@dmg210

@dmg210 dmg210 commented Oct 20, 2026 •

Copy link
Copy Markdown

Problem

On Windows, reopening a gs_usb CAN adapter for a subsequent operation can fail with:

usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

In my application, the first CAN operation completed successfully, but opening the adapter for the second operation failed. The device remained discoverable.

Fix

Explicitly dispose of the underlying PyUSB device resources during GsUsbBus.shutdown().

Testing

Tested on Windows 10 with Python 3.11, python-can 4.6.1, gs_usb 0.3.1, and a CANBUS adapter using WinUSB.

After adding usb.util.dispose_resources(self.gs_usb.gs_usb) to the shutdown method, I was able to complete a second operation successfully.

Further testing of repeated open/close cycles has been completed locally.

Type of Change

  • Bug fix

On Windows, reopening a `gs_usb` CAN adapter for a subsequent operation can fail with:
`usb.core.USBError: [Errno 13] Access denied (insufficient permissions)`
Comment thread can/interfaces/gs_usb.py
pass
finally:
usb.util.dispose_resources(gs_usb.gs_usb)
usb.util.dispose_resources(self.gs_usb.gs_usb)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this repetition, i'd prefer a try-finally around the whole function body.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: Fix gs_usb USB resource cleanup on Windows

The fix is correct: with an exclusive-access backend (WinUSB), main keeps the PyUSB handle open after shutdown(), and the next open fails with [Errno 13] Access denied. With this PR the device can be reopened, both by index and by bus/address.

Requested changes

  • can/interfaces/gs_usb.py, GsUsbBus.shutdown(): the handle leaks on the failure path. If the re-scan raises (e.g. usb.core.NoBackendError from _find_gs_usb_devices()), the final dispose_resources(self.gs_usb.gs_usb) is never reached. Wrap the body after the already_shutdown check in one try: and call usb.util.dispose_resources(self.gs_usb.gs_usb) in its finally:, as requested in the open review comment:
    try:
        self.gs_usb.stop()
        if self._index is not None:
            ...  # unchanged re-scan block
    finally:
        usb.util.dispose_resources(self.gs_usb.gs_usb)
  • Add a news fragment as required by the contribution guide ("Add a News Fragment for the Changelog"), e.g. doc/changelog.d/2106.fixed.rst:
    ``gs_usb``: release the USB device handle in :meth:`~can.interfaces.gs_usb.GsUsbBus.shutdown`, so the device can be reopened on Windows.

Verdict

request changes

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.

2 participants