From 05fd2c58cb9965b60e3b39de93e06abfb4f286da 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 d4bd0000..1f322157 100644 --- a/libfprint/fp-context.c +++ b/libfprint/fp-context.c @@ -426,6 +426,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)); @@ -564,8 +565,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); } /**