feat: post_event/attach_control queue for thread-safe session control#271
feat: post_event/attach_control queue for thread-safe session control#271abhichothani42 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a thread-safe “session control” event queue in SessionManager so that push-based control sources (VR buttons, etc.) can post intents from their own threads while the session state continues to be mutated only from the orchestrating loop.
Changes:
- Added
SessionManager::post_event()(thread-safe enqueue) andSessionManager::attach_control()(installs callbacks, starts/stops sources via shutdown). - Implemented phase-aware draining of queued control events from
monitor_episode()andwait_for_reset(). - Added a new unit test suite (
test_session_manager_control) and wired it into the test build.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/runtime/session_manager.cpp |
Implements control-source lifecycle shutdown, event enqueueing, and phase-aware draining in episode/reset loops. |
include/trossen_sdk/runtime/session_manager.hpp |
Exposes the new public APIs and adds private queue/flags used by control-event draining. |
tests/test_session_manager_control.cpp |
Adds unit coverage for queueing/draining behavior and attach/shutdown lifecycle. |
tests/CMakeLists.txt |
Builds and registers the new test binary with CTest. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * wait_for_reset() loop, so drive the session with those rather than | ||
| * start_async_monitoring(). | ||
| */ | ||
| void attach_control(hw::session_control::SessionControlCapable& source); |
There was a problem hiding this comment.
Can we have a shared_ptr here as we already have those for all hardware components. A raw pointer has the exact problems as mentioned in the note. Using shared pointer would gaurantee that the source outlives the session manager.
There was a problem hiding this comment.
dead938
Done. attach_control now takes std::shared_ptr and the manager holds it in control_sources_, so the source is guaranteed to outlive the manager's use of it.
a69375d to
dead938
Compare
a1887fa to
3e45218
Compare

Summary
SessionControlCapable sources (VR buttons, keyboard, future foot pedals, …) each run on their own thread and fire callbacks from that thread. But SessionManager's episode methods (start_episode, stop_episode, discard_*, shutdown) are single-threaded. Previously the VR demos called stop_episode() directly from the VR reader thread, racing the main loop that owns the session.
This PR adds a thread-safe event queue so a control source can only post an intent; SessionManager drains it and mutates the session only on the orchestrating thread.
Changes
Test Plan
make build)make test)pre-commit run --all-files)Breaking Changes
None. The new API is additive. With no attached control sources the queue stays empty, so existing keyboard-only demos (monitor_episode/wait_for_reset) behave exactly as before.
Related Issues