- if (ctx->usb_thread_running) {
- ResetEvent(ctx->usb_event);
- g_mutex_unlock(&ctx->usb_mutex);
+ /* Were we triggered by an event from the wait thread, rather than by a
+ * timeout? */
+ int triggered_by_event = (revents & G_IO_IN);
+
+ /* If so, and if the USB event source has not been removed from the
+ * session, reset the event that woke us and tell the wait thread to start
+ * waiting for events again. */
+ if (triggered_by_event && ctx->usb_thread_running) {
+ ResetEvent(ctx->usb_wait_complete_event);
+ SetEvent(ctx->usb_wait_request_event);