From c439378ac150791d80a51f49140b1db87062203b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sawicz?= Date: Thu, 19 Sep 2024 11:45:53 +0000 Subject: [PATCH] Less flaky device handling (#3602) Fix some dubious assumption in DefaultInputDeviceHub. Fixes: #3601 --- src/server/input/default_input_device_hub.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/server/input/default_input_device_hub.cpp b/src/server/input/default_input_device_hub.cpp index 7e189d86431..63ea6d26843 100644 --- a/src/server/input/default_input_device_hub.cpp +++ b/src/server/input/default_input_device_hub.cpp @@ -182,11 +182,11 @@ void mi::ExternalInputDeviceHub::Internal::changes_complete() observer->changes_complete(); }); - auto end_it = handles.end(); for (auto const& dev : removed) - end_it = remove(begin(handles), end(handles), dev); - if (end_it != handles.end()) - handles.erase(end_it, end(handles)); + { + handles.erase(remove(begin(handles), end(handles), dev), end(handles)); + } + for (auto const& dev : added) { if (auto handle = std::dynamic_pointer_cast(dev)) @@ -757,6 +757,13 @@ void mi::DefaultInputDeviceHub::device_changed(Device* dev) { std::unique_lock lock{mutex}; auto dev_it = find_if(begin(handles), end(handles), [dev](auto const& ptr){return ptr.get() == dev;}); + + if (dev_it == end(handles)) + { + log_debug("Ignoring changes to unknown device (it was likely removed already"); + return; + } + std::shared_ptr const dev_shared = *dev_it; if (pending_changes) {