Skip to content

[moveit_ros] fix race condition when stopping trajectory execution - #3198

Merged
sea-bass merged 2 commits into
moveit:mainfrom
patrickKXMD:fix_traj_execution
Jan 12, 2025
Merged

sea-bass merged 2 commits into
moveit:mainfrom
patrickKXMD:fix_traj_execution

Conversation

@patrickKXMD

Copy link
Copy Markdown
Contributor

Description

It is not thread-safe calling stopExecution or publishing a "stop" to trajectory_execution_event while trajectory executing. Sometimes it crashes, here's a sample

Program terminated with signal SIGSEGV, Segmentation fault.

#0 0x0000ffffa367ac48 in _gnu_cxx::normal_iterator<moveit_msgs::msg::RobotTrajectory<std::allocator > const*, std::vector<moveit_msgs::msg::RobotTrajectory<std::allocator >, std::allocator<moveit_msgs::msg::RobotTrajectory<std::allocator > > > >::__normal_iterator (__i=, this=)
at /usr/include/c++/11/bits/stl_iterator.h:1028
b1028 : _M_current(__i) { }
[Current thread is 1 (Thread 0xffff1e7cf120 (LWP 4402))]
(gdb)
(gdb)
(gdb) bt
#0 0x0000ffffa367ac48 in _gnu_cxx::normal_iterator<moveit_msgs::msg::RobotTrajectory<std::allocator > const*, std::vector<moveit_msgs::msg::RobotTrajectory<std::allocator >, std::allocator<moveit_msgs::msg::RobotTrajectory<std::allocator > > > >::__normal_iterator (i=, this=)
at /usr/include/c++/11/bits/stl_iterator.h:1028
#1 std::vector<moveit_msgs::msg::RobotTrajectory
<std::allocator >, std::allocator<moveit_msgs::msg::RobotTrajectory
<std::allocator > > >::end (
this=) at /usr/include/c++/11/bits/stl_vector.h:839
#2 trajectory_execution_manager::TrajectoryExecutionManager::waitForRobotToStop (this=this@entry=0xaaaaeed29f20, context=..., wait_time=wait_time@entry=1)
at /workspace/source/planning/trajectory_execution_manager/src/trajectory_execution_manager.cpp:1592
#3 0x0000ffffa36829d4 in trajectory_execution_manager::TrajectoryExecutionManager::executeThread(std::function<void (moveit_controller_manager::ExecutionStatus const&)> const&, std::function<void (unsigned long)> const&, bool) (this=0xaaaaeed29f20, callback=..., part_callback=..., auto_clear=) at /usr/include/c++/11/bits/stl_vector.h:1043
#4 0x0000ffffba7431fc in ?? () from /lib/aarch64-linux-gnu/libstdc++.so.6
#5 0x0000ffffbb92d5c8 in start_thread (arg=0x0) at ./nptl/pthread_create.c:442
#6 0x0000ffffbb995edc in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:79

This CL introduces a lock for interactions with trajectories_ and can fix this issue.

Checklist

  • Required by CI: Code is auto formatted using clang-format
  • Extend the tutorials / documentation reference
  • Document API changes relevant to the user in the MIGRATION.md notes
  • Create tests, which fail without this PR reference
  • Include a screenshot if changing a GUI
  • While waiting for someone to review your request, please help review another open pull request to support the maintainers

@codecov-commenter

codecov-commenter commented Jan 4, 2025 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.

Project coverage is 46.01%. Comparing base (9f1a53d) to head (d32373b).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...ution_manager/src/trajectory_execution_manager.cpp 0.00% 6 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3198      +/-   ##
==========================================
+ Coverage   45.66%   46.01%   +0.35%     
==========================================
  Files         714      714              
  Lines       62296    62320      +24     
  Branches     7530     7532       +2     
==========================================
+ Hits        28444    28673     +229     
+ Misses      33686    33481     -205     
  Partials      166      166              

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

@sea-bass sea-bass 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.

Thanks!

In addition to looking at the CI failures since something isn't building, left a few suggestions.

Rolling is failing now until #3197, but the rest should pass.

@patrickKXMD
patrickKXMD force-pushed the fix_traj_execution branch 3 times, most recently from a57e9a4 to 87ee059 Compare January 6, 2025 02:57

@sjahr sjahr 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.

Thanks, once the remaining comment is addressed and the PR that updates the deprecated headers is merged I think this is good to go

@rhaschke rhaschke 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.

We might avoid introducing a new mutex and reuse execution_state_mutex_.
In all locations calling clear(), the flag execution_complete_ is True:

However, currently clear() is a public method. Thus, calling it directly will face the race condition.
I suggest changing its visibility to protected or even private.

@sea-bass sea-bass added backport-humble Mergify label that triggers a PR backport to Humble backport-jazzy Mergify label that triggers a PR backport to Jazzy labels Jan 6, 2025
@patrickKXMD

Copy link
Copy Markdown
Contributor Author

Hi @sea-bass @rhaschke , in the latest commit I got waitForRobotToStop covered by scoped lock, so there's no need to extract joint names.

@sea-bass sea-bass 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.

This looks really clean now -- as long as this still fixes your tests, looks great to me. Thanks!

@rhaschke rhaschke 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.

If you follow my suggestion #3198 (review), you should also change visibility of clear() to protected or private!

Also, as a safe-guard for future changes, you should add an assertion to clear(), e.g. performing the following change to the line:

RCLCPP_ERROR(logger_, "Cannot push a new trajectory while another is being executed");

-    RCLCPP_ERROR(logger_, "Cannot push a new trajectory while another is being executed");
+    RCLCPP_FATAL(logger_, "Expecting execution_complete_ to be true!");

@patrickKXMD

Copy link
Copy Markdown
Contributor Author

If you follow my suggestion #3198 (review), you should also change visibility of clear() to protected or private!

Also, as a safe-guard for future changes, you should add an assertion to clear(), e.g. performing the following change to the line:

RCLCPP_ERROR(logger_, "Cannot push a new trajectory while another is being executed");

-    RCLCPP_ERROR(logger_, "Cannot push a new trajectory while another is being executed");
+    RCLCPP_FATAL(logger_, "Expecting execution_complete_ to be true!");

Hi @rhaschke,
Thank you for your suggestion, I added an assertion in clear.

But I have concern for changing visibility of clear now. It has been public for a long time, changing it might break someone's code.
In fact moveit2 uses it's public visibility too.
e.g.

trajectory_execution_manager_->clear();

.def("clear", &trajectory_execution_manager::TrajectoryExecutionManager::clear,
R"(
Clear the trajectories to execute.
)")

@rhaschke

rhaschke commented Jan 8, 2025

Copy link
Copy Markdown
Contributor

All these calls to clear() should be replaced by stopExecution(true).

@patrickKXMD

Copy link
Copy Markdown
Contributor Author

All these calls to clear() should be replaced by stopExecution(true).

Hi I opened a new pr #3226 for this

@sea-bass
sea-bass requested a review from rhaschke January 11, 2025 13:04

@rhaschke rhaschke 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.

I suggest to merge #3226 as part of this PR.

@sea-bass
sea-bass enabled auto-merge January 12, 2025 17:39
@sea-bass
sea-bass added this pull request to the merge queue Jan 12, 2025
auto-merge was automatically disabled January 12, 2025 18:38

Pull Request is not mergeable

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 12, 2025
@sea-bass
sea-bass added this pull request to the merge queue Jan 12, 2025
Merged via the queue into moveit:main with commit 6595dee Jan 12, 2025
mergify Bot pushed a commit that referenced this pull request Jan 12, 2025
…3198)

Co-authored-by: Sebastian Castro <4603398+sea-bass@users.noreply.github.com>
(cherry picked from commit 6595dee)

# Conflicts:
#	moveit_ros/planning/trajectory_execution_manager/src/trajectory_execution_manager.cpp
mergify Bot pushed a commit that referenced this pull request Jan 12, 2025
…3198)

Co-authored-by: Sebastian Castro <4603398+sea-bass@users.noreply.github.com>
(cherry picked from commit 6595dee)
rhaschke pushed a commit to ubi-agni/moveit that referenced this pull request Jan 12, 2025
sea-bass pushed a commit that referenced this pull request Jan 12, 2025
…3198) (#3240)

Co-authored-by: Dongya Jiang <1983534+patrickKXMD@users.noreply.github.com>
sea-bass pushed a commit that referenced this pull request Jan 12, 2025
…ackport #3198) (#3239)

(cherry picked from commit 6595dee)

Co-authored-by: Dongya Jiang <1983534+patrickKXMD@users.noreply.github.com>
sussybot5258 pushed a commit to GreyCatAI/moveit2 that referenced this pull request Apr 19, 2025
…oveit#3198)

Co-authored-by: Sebastian Castro <4603398+sea-bass@users.noreply.github.com>
Markus-Simonsen pushed a commit to Markus-Simonsen/moveit2 that referenced this pull request Aug 12, 2025
…oveit#3198)

Co-authored-by: Sebastian Castro <4603398+sea-bass@users.noreply.github.com>
rhaschke added a commit to moveit/moveit that referenced this pull request Aug 24, 2025
* Fix race condition when stopping trajectory execution
   backport of moveit/moveit2#3198
* Move TrajectoryExecutionManager::clear() to private
   backport of moveit/moveit2#3226
helen9975 pushed a commit to personalrobotics/moveit2 that referenced this pull request Feb 17, 2026
…oveit#3198)

Co-authored-by: Sebastian Castro <4603398+sea-bass@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-humble Mergify label that triggers a PR backport to Humble backport-jazzy Mergify label that triggers a PR backport to Jazzy backport-moveit1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants