From c3ef68fa5e3a8a7f438ac01d03922d0466e0f73e Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Wed, 24 Nov 2021 12:09:18 +0100 Subject: [PATCH] context: Ensure mainloop is idle before enumeration completes This ensures that we have processed all hotplug events before considering enumeration to be complete. This is important due to USB persist being turned off. At resume time, devices will disappear and immediately re-appear. In this situatoin, enumerate could first see the old state with a removed device resulting in it to not be discovered. As a hotplug event is semingly emitted by the kernel immediately, we can simply make sure to process this hotplug event before returning from enumerate. Closes: fprintd#119 --- libfprint/fp-context.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libfprint/fp-context.c b/libfprint/fp-context.c index a5b45e8f..65c57b22 100644 --- a/libfprint/fp-context.c +++ b/libfprint/fp-context.c @@ -437,6 +437,7 @@ void fp_context_enumerate (FpContext *context) { FpContextPrivate *priv = fp_context_get_instance_private (context); + gboolean dispatched; gint i; g_return_if_fail (FP_IS_CONTEXT (context)); @@ -575,8 +576,19 @@ fp_context_enumerate (FpContext *context) } #endif - while (priv->pending_devices) - g_main_context_iteration (NULL, TRUE); + /* Iterate until 1. we have no pending devices, and 2. the mainloop is idle + * This takes care of processing hotplug events that happened during + * enumeration. + * This is important due to USB `persist` being turned off. At resume time, + * devices will disappear and immediately re-appear. In this situation, + * enumerate could first see the old state with a removed device resulting + * in it to not be discovered. + * As a hotplug event is seemingly emitted by the kernel immediately, we can + * simply make sure to process all events before returning from enumerate. + */ + dispatched = TRUE; + while (priv->pending_devices || dispatched) + dispatched = g_main_context_iteration (NULL, !!priv->pending_devices); } /**