git: 641bb44d8538 - stable/14 - libusb: Emit event after deregistering hotplug handler

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Wed, 14 May 2025 14:24:12 UTC
The branch stable/14 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=641bb44d8538c66446aea556baa22bbf5bd14255

commit 641bb44d8538c66446aea556baa22bbf5bd14255
Author:     SHENGYI HUNG <aokblast@FreeBSD.org>
AuthorDate: 2025-05-05 17:52:45 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-05-14 14:23:38 +0000

    libusb: Emit event after deregistering hotplug handler
    
    The original implementation did not emit an event when a hotplug handler
    was deregistered. This omission causes issues for programs that follow
    the best practices recommended by libusb—particularly those that use
    `libusb_hotplug_register_callback()` or similar functions in a loop
    while managing hotplug handlers dynamically.
    
    Without emitting an event after deregistration, these programs can
    become stuck waiting indefinitely for an event that will never come,
    as the condition to break out of the wait loop is never satisfied.
    
    See: https://github.com/libusb/libusb/blob/6c0ae1ab456da49e7805115e77ce0428ace4ea41/libusb/hotplug.c#L459
    
    Reviewed by:    bapt
    Sponsored By:   FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D50170
    
    (cherry picked from commit d2852659180307475a8376ce86aa587cccbb10bf)
---
 lib/libusb/libusb10_hotplug.c | 1 +
 lib/libusb/libusb10_io.c      | 1 +
 2 files changed, 2 insertions(+)

diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c
index 98903686f76b..87490d3a76c7 100644
--- a/lib/libusb/libusb10_hotplug.c
+++ b/lib/libusb/libusb10_hotplug.c
@@ -246,6 +246,7 @@ void libusb_hotplug_deregister_callback(libusb_context *ctx,
 
 	HOTPLUG_LOCK(ctx);
 	TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry);
+	libusb_interrupt_event_handler(ctx);
 	HOTPLUG_UNLOCK(ctx);
 
 	free(handle);
diff --git a/lib/libusb/libusb10_io.c b/lib/libusb/libusb10_io.c
index 59bac60c4672..2790142486ba 100644
--- a/lib/libusb/libusb10_io.c
+++ b/lib/libusb/libusb10_io.c
@@ -231,6 +231,7 @@ do_done:
 
 	/* Wakeup other waiters */
 	pthread_cond_broadcast(&ctx->ctx_cond);
+	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub complete");
 
 	return (err);
 }