Skip to content

Commit

Permalink
hotplug_exit: mutex protection of context device list while cleaning it
Browse files Browse the repository at this point in the history
This prevents concurrent access to the list by the event background
thread, which could still be processing a previous hotplug event and
having to modify the list
  • Loading branch information
sonatique committed Feb 13, 2024
1 parent 4067d23 commit 8b20a20
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libusb/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
free(msg);
}

usbi_mutex_lock(&ctx->usb_devs_lock); /* hotplug thread might still be processing an already triggered event, possibly accessing this list as well */
/* free all discovered devices */
for_each_device_safe(ctx, dev, next_dev) {
/* remove the device from the usb_devs list only if there are no
Expand All @@ -222,11 +223,12 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
if (usbi_atomic_load(&dev->refcnt) == 1) {
list_del(&dev->list);
}

usbi_recursively_remove_parents(dev, next_dev);

libusb_unref_device(dev);
}
usbi_mutex_unlock(&ctx->usb_devs_lock);

usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
}
Expand Down

0 comments on commit 8b20a20

Please sign in to comment.