Skip to content

Fix #203: libusb sometimes cleaned up too early. - #227

Merged
mcuee merged 1 commit into
pyusb:masterfrom
minkustree:master
Nov 25, 2019
Merged

mcuee merged 1 commit into
pyusb:masterfrom
minkustree:master

Conversation

@minkustree

Copy link
Copy Markdown
Contributor

I think the cause of #203 is each time libusb1.get_backend() is being called, a new _LibUSB backend object is being created and returned.

When the first of these goes out of scope and is finalized, it de-inits the underlying libusb1 library, invalidating any remaining backend object instances and calls made through them.

My fix is to assume that there is only going to be one _LibUSB backend object and cache it, returning it to subsequent libusb1.get_backend calls. This assumption mirrors the one made about _lib.

Each call to libusb1's get_backend returns a new _LibUSB object.
When each  _LibUSB object falls out of scope, it calls ends up
calling libusb_exit() which deinitializes libusb, even if there are still
references in scope.

Practically speaking, pyusb seems to assume there's only one
context active, so returning the same _LibUSB object on
subsequent calls to get_backend should prevent there from being
more than one call to libusb_exit() when backend objects go
out of scope.

This should fix errors where the libusb library was being used after
libusb_exit() had been called.
@pelrun

pelrun commented Mar 12, 2019

Copy link
Copy Markdown

This completely solves the access violation errors I'm getting on Windows with the current release version.

@hgcummings

Copy link
Copy Markdown

This completely solves the access violation errors I'm getting on Windows with the current release version.

For me as well (on Windows 10). I hope this can be merged soon.

@ladyada

ladyada commented Sep 21, 2019

Copy link
Copy Markdown

tossing in another "fixes libusb-1.0.dll 1.0.22 with windows10"
@SimplicityGuy hiya are you still maintaining this? if not, no biggie - we'll just tell folks to install the PR branch

@jonasmalacofilho

Copy link
Copy Markdown
Member

@minkustree

I would like to apply this fix on top of v1.0.2 and redistribute it under that license.¹

The license was changed after v1.0.2 (BSD -> Apache, see #201), but development has halted, everyone's still using v1.0.2 and I don't want to force the license change downstream.

So can I ask you to dual license your patch to allow it to be used under the 3-clause BSD license the project used before?


¹ I would be redistributing your patch as part of the liquidctl binaries for Windows.

@minkustree

Copy link
Copy Markdown
Contributor Author

Yes @jonasmalacofilho I am happy to dual-license the patch under the previous BSD-3 license, and hereby do so.

jonasmalacofilho added a commit to liquidctl/liquidctl that referenced this pull request Nov 17, 2019
The patch for PyUSB successfully prevents access violation errors during
cleanup with libusb 1.0.22, but "some libusb_devices were leaked"
warnings can still be seen with LIBUSB_DEBUG=3.

Patch: 8041ec11b2c4 ("Fix #203: libusb sometimes cleaned up too early.")
Related: pyusb/pyusb#227 ("Fix #203: libusb sometimes cleaned up too early.")
Related: pyusb/pyusb#203 ("OSError with wxPython #203")
@jonasmalacofilho

Copy link
Copy Markdown
Member

@minkustree

Thank you!

FYI I can still see some "libusb_devices were leaked" warnings with LIBUSB_DEBUG=3... I'll take a look and let you know if I come up with any additional patches.

@mcuee
mcuee merged commit f644ea7 into pyusb:master Nov 25, 2019
@jonasmalacofilho

Copy link
Copy Markdown
Member

@mcuee

Thank you for merging this and the other three PRs.

@ladyada

ladyada commented Nov 25, 2019

Copy link
Copy Markdown

yay! thats great - thanks @mcuee !

@ladyada

ladyada commented Nov 27, 2019

Copy link
Copy Markdown

@mcuee can you get the pypi version bumped so this fix can be distributed :)

@SimplicityGuy

Copy link
Copy Markdown
Contributor

I’ve done an awful job maintaining the project due to my day responsibilities. @mcuee , I can assign over the pypi project to you.

@ladyada

ladyada commented Nov 27, 2019

Copy link
Copy Markdown

@SimplicityGuy don't feel awful - you've done a great job getting it this far :) its really a wonderful library and very reliable!

@mcuee

mcuee commented Nov 29, 2019

Copy link
Copy Markdown
Member

@SimplicityGuy
I am actually not a developer myself and has limited understanding of Python and C.
I think pyusb is a great project. We can add more maintainers.

@mcuee

mcuee commented Nov 29, 2019

Copy link
Copy Markdown
Member

To help the project a bit, I have cleaned up the issues list.

@jonasmalacofilho

Copy link
Copy Markdown
Member

@mcuee, @SimplicityGuy

I can help as well.

@mcuee

mcuee commented Nov 29, 2019

Copy link
Copy Markdown
Member

@jonasmalacofilho added you. Please accept the invitation to be added.

@jonasmalacofilho

Copy link
Copy Markdown
Member

@mcuee thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants