New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix leaking udev reference in LinuxUsbDevice #1678
Conversation
Gah, good find. I should have seen that when looking at #1670. Your fix doesn't handle the case where This whole class could use a refactoring, if you wouldn't mind putting in a bit more work here:
At a minimum please change your PR to do something like this pattern:
However, if you can refactor to the example in |
5c117d8
to
fb371a3
Compare
This change fixes a leaking udev device reference when enumerating devices. Devices with a devtype that was not "usb_device" were never freed during the enumeration. The enumeration process has been refactored (in line with the implementation in LinuxHWDiskStore) to use the more object-oriented API for udev that's now available from JNA.
fb371a3
to
710873c
Compare
Thanks for the pointers @dbwiddis – I have updated the PR to reflect the approach used in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This looks great and I'll go ahead and merge it.
for (UdevListEntry dev_list_entry = devices; dev_list_entry != null; dev_list_entry = Udev.INSTANCE | ||
.udev_list_entry_get_next(dev_list_entry)) { | ||
// Enumerate all usb devices and build information maps | ||
Udev.UdevContext udev = Udev.INSTANCE.udev_new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again, I see that it's possible to return null if the native call fails, but of the four times we call this, we only null check one of them. No need to change this PR for that but I'll have to think about whether we should update all of them.
// Enumerate all usb devices and build information maps | ||
Udev.UdevContext udev = Udev.INSTANCE.udev_new(); | ||
try { | ||
UdevEnumerate enumerate = udev.enumerateNew(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to udev_new this can theoretically return null but probably never will. Something to think about for a future fix.
* Refactor LinuxUsbDevice to fix leaking reference This change fixes a leaking udev device reference when enumerating devices. Devices with a devtype that was not "usb_device" were never freed during the enumeration. The enumeration process has been refactored (in line with the implementation in LinuxHWDiskStore) to use the more object-oriented API for udev that's now available from JNA. * Update changelog
This change fixes a leaking device reference in the Linux USB device implementation. When an interface device is encountered (i.e. devtype is "usb_device") the rest of the loop was skipped and the allocated device would never be unreferenced. This change adds an additional explicit unreference in this case.
This was the underlying leak causing the issue reported by @botto in #1670