ros2 runtime_error ai_generated true

rclcpp::exceptions::RCLError: failed to publish parameter event: cannot call publish while in a callback group

ID: ros2/rclcpp-parameter-event-callback-deadlock

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2024-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ros2-humble active
ros2-iron active
ros2-rolling active

Root Cause

A parameter event callback tries to publish a message on the same topic that triggered the callback, causing a deadlock in the callback group's executor.

generic

中文

参数事件回调尝试在触发回调的同一主题上发布消息,导致回调组执行器死锁。

Official Documentation

https://docs.ros2.org/latest/api/rclcpp/classrclcpp_1_1ParameterEventHandler.html

Workarounds

  1. 90% success Use a separate callback group for the parameter event subscriber: auto param_event_group = create_callback_group(rclcpp::CallbackGroupType::Reentrant); auto sub = create_subscription<rcl_interfaces::msg::ParameterEvent>('/parameter_events', 10, callback, param_event_group);
    Use a separate callback group for the parameter event subscriber: auto param_event_group = create_callback_group(rclcpp::CallbackGroupType::Reentrant); auto sub = create_subscription<rcl_interfaces::msg::ParameterEvent>('/parameter_events', 10, callback, param_event_group);
  2. 85% success Publish the parameter event asynchronously using a timer: in the callback, schedule a timer to publish the message after the callback returns.
    Publish the parameter event asynchronously using a timer: in the callback, schedule a timer to publish the message after the callback returns.

中文步骤

  1. 为参数事件订阅者使用单独的回调组:auto param_event_group = create_callback_group(rclcpp::CallbackGroupType::Reentrant); auto sub = create_subscription<rcl_interfaces::msg::ParameterEvent>('/parameter_events', 10, callback, param_event_group);
  2. 使用定时器异步发布参数事件:在回调中,安排一个定时器在回调返回后发布消息。

Dead Ends

Common approaches that don't work:

  1. 90% fail

    MutuallyExclusive groups still deadlock if the same callback tries to publish to the triggering topic.

  2. 70% fail

    The event callback is still in the same callback group; publishing from another node doesn't resolve the deadlock.