Repository navigation
Make viewer stop-stream non-blocking for immediate UI response - #14695
Conversation
There was a problem hiding this comment.
Pull request overview
Make the viewer “stop streaming” path non-blocking by moving heavy sensor shutdown work to a background task, while keeping UI state updates immediate and synchronous.
Changes:
- Introduce a background stop task (
std::future) andwait_for_stop()to join it. - Run heavy sensor operations (
stop, frame queue drain,close) viastd::async. - Block
play()(and FW update flow / destructor) onwait_for_stop()to ensure clean transitions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| common/subdevice-model.h | Adds wait_for_stop() API and stores _stop_future to track background stop work |
| common/subdevice-model.cpp | Implements async stop workflow; ensures destructor/play wait for pending stop completion |
| common/fw-update-helper.cpp | Waits for all subdevices’ background stop tasks to complete before firmware update |
Comments suppressed due to low confidence (1)
common/subdevice-model.cpp:1
std::asyncruns this lambda on a background thread but it mutates/iteratessubdevice_modelstate viathis(_options_invalidated,depth_sensors). If those members are accessed on the UI thread (or other threads) concurrently, this introduces data races/UB. Consider avoidingthiscapture by copying all required data into the task (e.g., snapshot the relevant sensors/queues before launching), and/or protecting shared members with a mutex/atomics so cross-thread reads/writes are synchronized.
// License: Apache 2.0. See LICENSE file in root directory.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // --- Heavy operations (background) --- | ||
| wait_for_stop(); // Wait for any prior pending stop |
There was a problem hiding this comment.
Although UI flags are flipped before this, stop() can still block the caller/UI thread if a previous background stop is still running (e.g., rapid stop/stop or stop during shutdown). To preserve the PR goal of a non-blocking stop handler, consider making repeated stop() calls return immediately when a stop is already in-flight (e.g., check readiness with wait_for(0)), or serialize stops via a dedicated worker thread/queue rather than blocking here.
| } | ||
| catch (...) {} | ||
| }); |
There was a problem hiding this comment.
Catching and swallowing all exceptions here makes failures (e.g., stop()/close() throwing) silently disappear, which can leave the app in a misleading state and complicate debugging. Prefer at least logging the exception (or letting it propagate into the future and handling/reporting it in wait_for_stop()), so failures are observable.
| void subdevice_model::wait_for_stop() | ||
| { | ||
| if (_stop_future.valid()) | ||
| _stop_future.get(); | ||
| } |
There was a problem hiding this comment.
std::future is not safe to access concurrently. With wait_for_stop() now callable from multiple code paths (e.g., UI thread play()/stop() and FW update flow), concurrent calls to valid()/get() and assignment to _stop_future can race. Guard _stop_future with a mutex (covering valid/get and the assignment in stop()), or redesign to use a single-threaded stop worker and a thread-safe signaling mechanism.
Move the heavy sensor operations (sensor stop, frame queue drain, sensor close) in subdevice_model::stop() to a background thread via std::async, while keeping all UI state updates synchronous. This eliminates the perceived freeze when the user clicks stop. The next play() call gates on wait_for_stop() to ensure the sensor is fully closed before reopening. Details: - Rapid stop-stop calls chain via future move rather than blocking the UI thread, so stop() never blocks the caller. - _stop_future access is protected by _stop_mutex against concurrent callers. - Exceptions in the background task are logged as warnings via viewer.not_model->add_log() instead of being silently swallowed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
726b81b to
39e3a29
Compare
- wait_for_stop(): move future out under lock, then wait without holding the mutex, so the blocking get() does not prevent other callers from checking _stop_future. - Add null check on viewer.not_model before logging in the background stop catch handler. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Addressing Aikido review comments (fixed in b5cdede)1. Null check on 2. Deadlock risk in 3. Deadlock risk in |
Addressing Copilot review comments (fixed in 39e3a29)1. 2. Silent exception swallowing (comment) 3. |
|
I need to play with it a bit but I think some corner cases can happen here. I think that it might have side affects |
|
@Nir-Az - this is why we have wait_for_stop |
Re: corner cases (@Nir-Az)Good questions — here's how each scenario is handled: 1. Click stop, then immediately click startHandled by design. 2. Clicking on controls that are normally locked while streamingSince
|
|
@OhadMeir please review and try it out. |
OhadMeir
left a comment
There was a problem hiding this comment.
Calibration flows call stop then start. Should they call wait_for_stop? I think they are running not on GUI thread. Should check
…ment wait_for_stop
|
Re: calibration flows calling stop then start no additional |
Per review feedback: remove try/catch from async lambda so exceptions propagate via the future and surface through the standard viewer error handling (popup) at each call site. The destructor wraps its own wait_for_stop() in try/catch since destructors must not throw.
|
@OhadMeir I played with it a bit and it looks OK from UI perspective. |
Summary
s->stop(), frame queue drain,s->close()) insubdevice_model::stop()to a background thread viastd::async, keeping all UI state updates synchronous for instant visual feedbacksubdevice_model::play()onwait_for_stop()so the next start-stream blocks until the background stop completesTest plan
🤖 Generated with Claude Code