Skip to content

fix: replace rusb GlobalContext with explicit Context to prevent panics#191

Merged
cocool97 merged 1 commit intococool97:mainfrom
zbowling:fix/replace-global-context-with-explicit-rusb-context
Mar 30, 2026
Merged

fix: replace rusb GlobalContext with explicit Context to prevent panics#191
cocool97 merged 1 commit intococool97:mainfrom
zbowling:fix/replace-global-context-with-explicit-rusb-context

Conversation

@zbowling
Copy link
Copy Markdown
Contributor

Summary

  • Replace all uses of rusb::devices() (which internally uses GlobalContext and panics via Once::call_once if libusb_init fails) with explicit rusb::Context::new() + context.devices() which returns a Result instead
  • Replace all GlobalContext type references with Context in USBTransport struct and method signatures
  • This makes the crate usable in environments without USB support (CI servers, containers, etc.) by returning errors instead of panicking

Test plan

  • cargo clippy passes with no warnings
  • cargo test passes (5 unit tests + 4 doc-tests)
  • Manual testing on a system without libusb installed to confirm error is returned instead of panic

…nt panics

rusb's GlobalContext uses Once::call_once internally, which panics if
libusb_init fails (e.g. in CI servers, containers, or environments
without USB support). Replace all uses with explicit rusb::Context::new()
which returns a Result instead of panicking.
@zbowling
Copy link
Copy Markdown
Contributor Author

This is fixing an issue with some of my tests in CI where libusb doesn't exist.

@cocool97
Copy link
Copy Markdown
Owner

Thanks for your PR ! :)

@cocool97 cocool97 merged commit 76b1d7a into cocool97:main Mar 30, 2026
11 checks passed
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