Skip to content

Fix thread-safety issues in Franka control threads#1985

Open
fspindle wants to merge 3 commits into
lagadic:masterfrom
fspindle:add_franka_mutexes
Open

Fix thread-safety issues in Franka control threads#1985
fspindle wants to merge 3 commits into
lagadic:masterfrom
fspindle:add_franka_mutexes

Conversation

@fspindle

Copy link
Copy Markdown
Contributor

No description provided.

fspindle added 3 commits July 10, 2026 15:32
Several control_thread implementations read variables shared with the
calling thread (setVelocity()/setForceTorque()) without holding the
mutex that protects them, while the corresponding writes were already
locked. This was a data race between the 1kHz libfranka callback
thread and the user thread.

vpJointVelTrajGenerator_impl.cpp:
- joint_velocity_callback: take a local copy of dq_des under mutex
  lock before use, instead of reading the shared array directly
  (mirrors the existing v_cart_des pattern).
- Move the vpJointVelTrajGenerator instance out of the lambdas: it
  was declared `static` inside joint_velocity_callback and
  cartesian_velocity_callback, so two concurrent control_thread
  executions (e.g. two robots) would share and corrupt the same
  trajectory generator state. It is now a local variable of
  control_thread, captured by reference.
- cartesian_velocity_callback: fix applyVel() being called with
  dq_des_local (a copy of the JOINT_STATE-only dq_des parameter,
  irrelevant in cartesian mode) instead of dq_des_ (the velocity
  actually computed from v_cart_des via the Jacobian pseudo-inverse).
  This bug made cartesian velocity control ineffective. Removed the
  now-unused dq_des_local/lock block and log the correct dq_des_
  in dq-des.log instead.

vpForceTorqueGenerator_impl.cpp:
- force_joint_control_callback: take a local copy of tau_J_des under
  mutex lock before use.
- force_cart_control_callback: take a single local snapshot of
  ft_cart_des under mutex lock at the start of the cycle, reused for
  both control and logging (previously read without protection).
- Replace the BEGIN_VISP_NAMESPACE-scope global ft_cart_des_prev,
  which would be shared/raced across concurrent control_thread
  instances, with a variable local to control_thread. Restore the
  tau_diff_prev.log semantics (diff against the previous cycle's
  desired force/torque) that were incorrectly reduced to always
  logging zero when the global variable was first removed.
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.72%. Comparing base (ad2ff38) to head (bf6542f).
⚠️ Report is 14 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1985      +/-   ##
==========================================
+ Coverage   48.71%   48.72%   +0.01%     
==========================================
  Files         532      532              
  Lines       69402    69398       -4     
  Branches    32436    32431       -5     
==========================================
+ Hits        33810    33816       +6     
+ Misses      31445    31438       -7     
+ Partials     4147     4144       -3     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant