ROS 2 callback groups control which callbacks an executor may run concurrently. They are essential for protecting shared state and avoiding deadlocks in multi-threaded nodes. They do not change DDS or ROS 2 topic reliability, recover lost samples, enlarge a subscription queue, or guarantee a callback deadline. Mixing these concerns can produce nodes that are thread-safe yet still lose data, or reliable yet deadlock internally.

Two group types

A Mutually Exclusive Callback Group prevents callbacks in that group from executing in parallel. The effect is similar to running those callbacks through a single-threaded executor, even when the node belongs to a MultiThreadedExecutor. ROS 2 gives each node a default callback group of this type. If every entity uses the default group, adding executor threads does not create callback-level parallelism for that node.

A Reentrant Callback Group allows callbacks in the group to execute concurrently. Different callbacks can overlap, and multiple instances of the same callback can run at once. Callbacks in different groups can also execute in parallel, regardless of whether those groups are mutually exclusive or reentrant, subject to executor threads and available work.

Entities pass their callback group to callbacks they create. A service or action client can create hidden future done-callbacks. This matters because a developer may see only a timer callback in source code while a synchronous client call depends on another callback that must be scheduled before the timer can return.

The classic deadlock

Put a timer and a synchronous service client in the same Mutually Exclusive group. The timer runs and waits for the service result. The result's done-callback belongs to the same group, so it cannot run while the timer callback is active. The timer waits for the done-callback, and the group prevents the done-callback from executing: a deadlock.

ROS 2's official guide recommends putting the waiting callback and the client in different callback groups or using a Reentrant group when synchronous calls are required. An asynchronous call is often safer. Merely replacing the default mutually exclusive group with another single shared mutually exclusive group changes nothing.

What callback groups do not do

Subscription reliability is a communication QoS policy. RELIABLE requests retransmission behavior from the middleware; BEST_EFFORT does not. Callback groups act after data is available for executor handling. Moving a subscription from a Mutually Exclusive group to a Reentrant group does not turn best-effort transport into reliable delivery.

History and depth govern how many samples the middleware attempts to retain. A blocked callback in one mutually exclusive group can allow a subscription queue to fill. RELIABLE delivery may create backpressure; BEST_EFFORT may drop samples; the precise behavior depends on the stack and QoS. The group did not lose or recover the samples by itself. It only influenced when application callbacks could run.

Callback groups also provide no real-time deadline guarantee. A Reentrant callback may be eligible to run but wait because all executor threads are busy. A mutually exclusive control callback may be protected from overlap yet delayed by a long callback already holding the group. Operating-system scheduling, executor selection, callback execution time, memory allocation, and middleware behavior all affect latency.

Design from shared-state rules

List the state each callback reads or writes. Put callbacks that touch the same non-thread-safe state in one Mutually Exclusive group, or add an explicit synchronization design. Use separate mutually exclusive groups when callbacks may run in parallel with each other but each callback must not overlap itself. Use a Reentrant group only when the callback and every dependency it touches are safe for simultaneous execution.

Do not use group boundaries as an undocumented lock graph. Name groups by the protected resource or concurrency contract, such as motion_state_group or diagnostics_reentrant_group. Retain references to groups as required by the client library so the executor can schedule their callbacks.

Separate fast and blocking work. A subscription callback can validate and enqueue a bounded work item, then return. A worker or another executor handles disk access, planning, or remote calls. Keep a control timer away from callbacks that can wait on network responses. This makes the callback budget visible and reduces head-of-line blocking.

Pair scheduling with communication QoS

For each subscription, choose reliability, history, depth, deadline, and lifespan from the data contract. Then choose a callback group from the concurrency contract. A camera preview may use best effort and a reentrant processing group. A command subscription may use reliable delivery and a mutually exclusive group that protects the state machine. Those are independent decisions.

Monitor both layers. Communication metrics include matched endpoints, lost or rejected samples, deadline events, and message age. Scheduling metrics include ready-to-start delay, callback execution time, group wait time, executor thread utilization, and work-queue depth. A single “callback late” counter cannot explain whether the source was transport, queueing, or execution.

Test concurrency and delivery separately

Instrument callbacks with start and finish events plus a group identifier. Generate two callbacks simultaneously and verify allowed overlap. Run the same reentrant callback twice and confirm its state remains correct. Block a mutually exclusive callback and verify that other groups can still progress when executor threads are available.

Reproduce the synchronous-client deadlock with a bounded test, then apply separate groups and confirm the future done-callback executes. Test the asynchronous design too. Use sanitizers or race detectors where supported, because a configuration that “runs faster” can hide data races.

Next, stress the subscription queues independently. Publish faster than callbacks can process under BEST_EFFORT and RELIABLE profiles. Measure received sequences, middleware status, message age, and callback latency. Changing only callback groups should alter scheduling and throughput, not the declared transport reliability contract.

Callback groups answer “which callbacks may execute at the same time?” Communication QoS answers “which samples the middleware attempts to deliver and retain?” Executors answer “when eligible work gets a thread.” Keep those questions separate, and a ROS 2 robot node can be both concurrency-safe and explicit about delivery behavior.