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 4, 2024
1 parent 7a470cf commit e8a3b16
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libusb/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
}

/* free all discovered devices. due to parent references loop until no devices are freed. */
usbi_mutex_lock(&ctx->usb_devs_lock); /* hotplug thread might still be processing an already triggered event, possibly accessing this list as well */
for_each_device_safe(ctx, dev, next_dev) {
/* remove the device from the usb_devs list only if there are no
* references held, otherwise leave it on the list so that a
Expand All @@ -225,6 +226,7 @@ void usbi_hotplug_exit(struct libusb_context *ctx)

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

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

0 comments on commit e8a3b16

Please sign in to comment.