From 1a02ad22d9f57dc6dda94324fff96f3300f31ac3 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sat, 6 Aug 2022 16:49:28 -0700 Subject: [PATCH] Handle libusb events when checking if device is connected libusb requires the application to call one of the `libusb_handle_events` functions in order for hotplug events to actually get dispatched. We don't need to wait for any timeout, so we use a default timeout of 0, which should process any pending events, and then return immediately. A test of repeatedly disconnecting and reconnecting a virtual device using USBIP was done, and no crashes of the daemon were observed. Github-Issue: https://github.com/nirenjan/libx52/issues/43 --- libx52/x52_core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libx52/x52_core.c b/libx52/x52_core.c index d1ea6d5..7f730d8 100644 --- a/libx52/x52_core.c +++ b/libx52/x52_core.c @@ -71,11 +71,15 @@ static int _x52_hotplug_callback(libusb_context *ctx, bool libx52_is_connected(libx52_device *dev) { int rc; + struct timeval tv = {0}; + int completed = 0; if (!dev) { return false; } + /* Handle events, and then check if the hotplug callbacks have fired */ + libusb_handle_events_timeout_completed(dev->ctx, &tv, &completed); if (dev->hdl) { if (dev->handle_registered) {