From 069eeccb603d4c864e4be2be889f7ecef43de261 Mon Sep 17 00:00:00 2001 From: Alessandro Di Ronza Date: Mon, 27 Jul 2026 22:27:54 +0000 Subject: [PATCH] CLI: return a device list again, and print the version --- DetectionManager.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ cli.cpp | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/DetectionManager.cpp b/DetectionManager.cpp index 039eb2b70..41ebea84a 100644 --- a/DetectionManager.cpp +++ b/DetectionManager.cpp @@ -31,6 +31,11 @@ using namespace std::chrono_literals; \*---------------------------------------------------------*/ const char* DETECTIONMANAGER = "DetectionManager"; +/*---------------------------------------------------------*\ +| Upper bound for WaitForDetection(), in milliseconds | +\*---------------------------------------------------------*/ +#define DETECTION_WAIT_TIMEOUT_MS 60000 + /*---------------------------------------------------------*\ | Warning Dialog Strings | \*---------------------------------------------------------*/ @@ -502,6 +507,16 @@ void DetectionManager::BeginDetection() \*-----------------------------------------------------*/ if(detection_ready) { + /*-------------------------------------------------*\ + | Flag detection as in progress here rather than in | + | the background thread. WaitForDetection() must | + | not be able to return between this call and the | + | thread actually starting, or a caller that only | + | wants the finished device list, such as the CLI, | + | gets an empty one. | + \*-------------------------------------------------*/ + detection_in_progress = true; + RunInBackgroundThread(std::bind(&DetectionManager::BackgroundDetectDevices, this)); } } @@ -518,6 +533,32 @@ std::string DetectionManager::GetDetectionString() void DetectionManager::WaitForDetection() { + /*-----------------------------------------------------*\ + | Taking the mutex is not enough on its own: detection | + | runs in a background thread and there is a window | + | between BeginDetection() and that thread taking the | + | mutex where it would be free. Wait for the in | + | progress flag to clear first, then take the mutex to | + | make sure the detection thread is done. | + \*-----------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Bounded, so that a detection that never starts cannot | + | hang the caller forever: waiting too long is a bug, | + | but blocking startup indefinitely would be worse | + \*-----------------------------------------------------*/ + unsigned int waited_ms = 0; + + while(detection_in_progress.load() && waited_ms < DETECTION_WAIT_TIMEOUT_MS) + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + waited_ms += 10; + } + + if(detection_in_progress.load()) + { + LOG_WARNING("[%s] Gave up waiting for detection after %u ms", DETECTIONMANAGER, waited_ms); + } + DetectDevicesMutex.lock(); DetectDevicesMutex.unlock(); } diff --git a/cli.cpp b/cli.cpp index 4c974b631..544dc97f1 100644 --- a/cli.cpp +++ b/cli.cpp @@ -487,7 +487,7 @@ void OptionListDevices(std::vector& rgb_controllers) \*---------------------------------------------------------*/ if(!controller->GetVersion().empty()) { - std::cout << " Version: " << controller->GetLocation() << std::endl; + std::cout << " Version: " << controller->GetVersion() << std::endl; } /*---------------------------------------------------------*\