Skip to content

Fix USB discovery on Windows in case there is no USB port - #4996

Merged
wborn merged 2 commits into
openhab:mainfrom
lolodomo:fix_scanUSB
Aug 31, 2025
Merged

Fix USB discovery on Windows in case there is no USB port#4996
wborn merged 2 commits into
openhab:mainfrom
lolodomo:fix_scanUSB

Conversation

@lolodomo

Copy link
Copy Markdown
Contributor

Fix #4993

Fix openhab#4993

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@lolodomo
lolodomo requested a review from a team as a code owner August 30, 2025 07:32
@lolodomo

Copy link
Copy Markdown
Contributor Author

This is not tested.

@wborn
wborn requested review from andrewfg and Copilot August 30, 2025 13:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in Windows USB serial device discovery where the application would crash when no USB ports are present. The fix wraps registry access calls in try-catch blocks to handle RuntimeExceptions gracefully.

  • Added exception handling around all Advapi32Util.registryGetKeys() and Advapi32Util.registryGetValues() calls
  • Added debug logging for registry access failures
  • Returns empty results or continues processing when registry operations fail

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@andrewfg andrewfg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I never saw a Windows PC without a USB port, so would not know how to physically test it. However the code LGTM.

@wborn

wborn commented Aug 30, 2025

Copy link
Copy Markdown
Member

You can disable USB ports in the BIOS or in the device manager or not install the device drivers. Old PCs also don't have them. 😄

@lolodomo

Copy link
Copy Markdown
Contributor Author

What is your feeling about Copilot suggestion ?
That is true that I could use com.sun.jna.platform.win32.Win32Exception

@andrewfg

Copy link
Copy Markdown
Contributor

could use com.sun.jna.platform.win32.Win32Exception

I think any exception is bad. So you should catch and log them all. Trying to double guess what detail exceptions may or may not be thrown, on whatever machine and OS it may be, seems like an invitation for another user to open another issue at sometime. Just catch them all..

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@lolodomo

Copy link
Copy Markdown
Contributor Author

I finally applied Copilot suggestions after checking the code of Advapi32Util.registryGetKeys and Advapi32Util.registryGetValues.

@wborn wborn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@wborn
wborn merged commit 69d4c83 into openhab:main Aug 31, 2025
4 checks passed
@wborn wborn added this to the 5.1 milestone Aug 31, 2025
@wborn wborn added the bug An unexpected problem or unintended behavior of the Core label Aug 31, 2025
@lolodomo
lolodomo deleted the fix_scanUSB branch August 31, 2025 10:02
@lolodomo

Copy link
Copy Markdown
Contributor Author

A backport to 5.0.x is probably recommended.

wborn pushed a commit that referenced this pull request Sep 3, 2025
Fix #4993

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@wborn wborn added the backported A PR that has been cherry-picked to a patch release branch label Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported A PR that has been cherry-picked to a patch release branch bug An unexpected problem or unintended behavior of the Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception in discovery.addon.usb.UsbAddonFinder if there is no usb (virtual machine)

4 participants