Code Monkey home page Code Monkey logo

Comments (6)

walac avatar walac commented on May 20, 2024

My guess is that the kernel releases the resources allocated by claim_interface operation once the device is disconnected. If I am right, I think the only way to safely do the cleanup is after bug #64 lands, then at disconnect event, we can release the interface.

We can also try to reenumerate the device before call libusb_release_interface to check if it is still there (inside the backend), but it feels hacky.

from pyusb.

walac avatar walac commented on May 20, 2024

What kernel and libusb version are you using? I tried to reproduce the problem but I couldn't. Could you please also elaborate more on how to reproduce this?

from pyusb.

stephenrauch avatar stephenrauch commented on May 20, 2024

Kernel is Centos 5, which is 2.6.18. I am currently using Libusb 1.0.19. But as I stated earlier:

The exception happens every time when the device is in a certain configuration, but does
not happen when the device is in another configuration.

This may be difficult for you to reproduce, since you don't have the device, and in my case it was specific to a certain configuration on that device.

My suggestion would be to catch and ignore the error in release_interface. If you are confident that your code has a correct device (it worked earlier) and owns the interface that it is attempting to release, then the code should be fine, and an error of "No Such Device" could be confidently ignored.


I can and will test anything you need. Let me know.

from pyusb.

walac avatar walac commented on May 20, 2024

Could you please try this patch and check if the error is gone?

From 8ab3b5b991b859aaf4e2b6d1813fbdbadfb50d5d Mon Sep 17 00:00:00 2001
From: Wander Lairson Costa <[email protected]>
Date: Sun, 18 Jan 2015 10:30:48 -0200
Subject: [PATCH] core: Handle exceptions when releasing the interfaces.

At object destruction, claimed interfaces are released. If the device
is disconnected, the call to release_interface may fail and raise an
exception.

closes #84.
---
 usb/core.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/usb/core.py b/usb/core.py
index 6174e93..50446db 100644
--- a/usb/core.py
+++ b/usb/core.py
@@ -220,7 +220,12 @@ class _ResourceManager(object):
     def release_all_interfaces(self, device):
         claimed = copy.copy(self._claimed_intf)
         for i in claimed:
-            self.managed_release_interface(device, i)
+            try:
+                self.managed_release_interface(device, i)
+            except USBError:
+                # Ignore errors when releasing the interfaces
+                # When the device is disconnected, the call may fail
+                pass

     def dispose(self, device, close_handle = True):
         self.release_all_interfaces(device)
-- 
1.9.1

from pyusb.

stephenrauch avatar stephenrauch commented on May 20, 2024

Verified this patch prevents the exception from getting back up to the del() method and subsequently being reported via stderr.

from pyusb.

walac avatar walac commented on May 20, 2024

Thanks for testing the patch.

from pyusb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.