Skip to content

Fix shutdown race in Interfaces containing a TCPServer - #632

Draft
urfeex wants to merge 2 commits into
UniversalRobots:masterfrom
urfeex:fix_shutdown_race
Draft

urfeex wants to merge 2 commits into
UniversalRobots:masterfrom
urfeex:fix_shutdown_race

Conversation

@urfeex

@urfeex urfeex commented Nov 19, 2026

Copy link
Copy Markdown
Member

This affects

  • Reverse Interface
  • Script Command Interface
  • Trajectory Point Interface

This fixes a shutdown-race with an in-flight message coming in to the TCPServer

in

- Reverse Interface
- Script Command Interface
- Trajectory Point Interface

This fixes a shutdown-race with an in-flight message coming in to the TCPServer
Copilot AI balanced review requested due to automatic review settings November 19, 2026 13:32
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.96%. Comparing base (6172363) to head (674c20b).
⚠️ Report is 7 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/comm/tcp_server.cpp 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #632      +/-   ##
==========================================
+ Coverage   83.40%   83.96%   +0.55%     
==========================================
  Files         127      128       +1     
  Lines        8559     8569      +10     
  Branches     3913     3913              
==========================================
+ Hits         7139     7195      +56     
+ Misses        998      964      -34     
+ Partials      422      410      -12     
Flag Coverage Δ
python_scripts 49.69% <ø> (ø)
start_ursim 88.86% <ø> (+0.82%) ⬆️
unit 64.18% <94.44%> (+0.11%) ⬆️
ur20-latest 81.62% <94.44%> (+0.18%) ⬆️
ur5-3.14.3 81.30% <94.44%> (+0.10%) ⬆️
ur5e-10.11.0 77.25% <94.44%> (+0.21%) ⬆️
ur5e-10.12.0 ?
ur5e-10.7.0 76.83% <94.44%> (+0.26%) ⬆️
ur5e-5.9.4 81.58% <94.44%> (+0.15%) ⬆️
ur7e-10.13.0 78.56% <94.44%> (+0.13%) ⬆️
ur7e-10.14.0 78.74% <94.44%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

In-flight callbacks can access destroyed subclass state, shutdown can terminate if wakeup socket allocation fails, and deterministic teardown regression tests are missing.

3 open issues
What changed in this PR

Addresses shutdown races in TCP-backed control interfaces by stopping the server worker before callback-related members are destroyed.

Changes:

  • Add explicit shutdown calls to the reverse, script-command, and trajectory-point interface destructors.
  • Update public destructor declarations and lifecycle documentation.
  • Remove redundant log messages when the TCP server’s listening socket is already closed.
File Description
src/​control/​trajectory_point_interface.cpp Stop the worker before trajectory callbacks are destroyed.
src/​control/​script_command_interface.cpp Stop the worker before tool-contact callback state is destroyed.
src/​control/​reverse_interface.cpp Stop the worker before callback-related base members are destroyed.
src/​comm/​tcp_server.cpp Remove redundant already-closed socket logging.
include/​ur_client_library/​control/​trajectory_point_interface.h Declare and document the explicit destructor.
include/​ur_client_library/​control/​script_command_interface.h Declare and document the explicit destructor.
include/​ur_client_library/​control/​reverse_interface.h Replace the defaulted destructor declaration and update its documentation.

Review effort: Balanced

// The TCP worker calls messageCallback / disconnectionCallback, which use members declared after
// server_. Join that thread here so those members are still alive. ~TCPServer would otherwise do
// it too late, after they have already been destroyed.
server_.shutdown();
// The TCP worker calls messageCallback / disconnectionCallback, which use members declared after
// server_. Join that thread here so those members are still alive. ~TCPServer would otherwise do
// it too late, after they have already been destroyed.
server_.shutdown();
// The TCP worker calls messageCallback / disconnectionCallback, which use members declared after
// server_. Join that thread here so those members are still alive. ~TCPServer would otherwise do
// it too late, after they have already been destroyed.
server_.shutdown();
Copilot AI balanced review requested due to automatic review settings November 26, 2026 08:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The teardown tests do not reliably detect callback-list destruction and can hang on timeout because the callback-release guard is installed too late.

5 open issues

Review effort: Balanced

Comment on lines +125 to +135
auto destroyed = last_copy_destroyed;
auto is_entered = entered;
auto may_finish = release;
auto saw = saw_destroyed;
auto ok = token_ok;
is_entered->store(true);
while (!may_finish->load())
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
saw->store(destroyed->load());
void destroyWhileCallbackIsPaused(std::unique_ptr<Interface>& interface, CallbackLifetime& lifetime,
const std::shared_ptr<std::atomic<bool>>& destructor_started)
{
waitFor([&]() { return lifetime.entered->load(); }, std::chrono::seconds(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants