Atlas Robotics
Lesson 7/11 · about 12 min

Watch on YouTube ↗Watch on Facebook ↗

Callback, timer, executor: how a node really runs

ROS 2 node code has no while True loops at all — just a few functions that are "registered" and end with rclpy.spin(node). So who calls those functions, when, and in what order? Understanding this explains a series of confusing errors: the node "freezes" and does not receive messages, the timer runs slower than the setting, the a nested service call deadlocks.

After this lesson you will

  • Explain what callback is and why the ROS 2 node is written in the "register and wait" style
  • Know what spin() does, and how executor decides the order in which callbacks should run
  • Recognize 3 classic errors: callback running too long, sleep in callback, calling service and then waiting in callback itself

Callback: "when there is X, call function Y"

A ROS 2 node does not actively ask for data. It registers functions to be called when events occur:

EventRegister withExample in ros2_basics
Timer period expirescreate_timer(period, callback)sensor_publisher calls publish_reading every second
New message on topiccreate_subscription(Type, topic, callback, qos)data_logger received /sensor_data
Service requestedcreate_service(Type, name, callback)add_two_ints_server

The registered function is called callback. The code in __init__ only runs once to register; all the "real work" is in the callbacks.

spin() and executor

rclpy.spin(node) gives control to an executor — the loop runs forever, doing exactly one job:

repeat forever:
    wait for work (timer due / message received / request received)
    take one work item and call the corresponding callback
    wait until the callback FINISHES before taking the next item

The default Executor (SingleThreadedExecutor) has only one thread: only one callback runs at a time. Incoming messages are then queued (depth determined by QoS, previous lesson); If the queue is full, the old message will be discarded.

Most important consequence: a slow callback slows down the whole node. The image processing callback takes 200 ms, so the 50 Hz timer on the same node cannot run at exactly 50 Hz, and message /scan arrives in the meantime and has to wait.

3 classic mistakes

  1. Write while True or long loop in __init__ → spin() is never called → no callback runs, node is dead. If you need to work periodically, use a timer.
  2. time.sleep() in callback → blocks the entire executor; The simulation is even more wrong because sleep measures real time and not according to /clock simulation.
  3. Call the service and then wait for the result right in the callback (call() synchronously, or spin_until_future_complete inside the callback) with a 1-thread executor → hang forever: callback waits for response, but the response is only processed when the callback ends. Correct way: call_async() then process the result in the future callback, or use MultiThreadedExecutor + appropriate callback group.

When parallel execution is needed: MultiThreadedExecutor + callback group

from rclpy.executors import MultiThreadedExecutor
from rclpy.callback_groups import ReentrantCallbackGroup, MutuallyExclusiveCallbackGroup

executor = MultiThreadedExecutor(num_threads=4)
executor.add_node(node)
executor.spin()
  • MutuallyExclusiveCallbackGroup (default): callbacks in the same group do not run at the same time — safe when they share the same variable.
  • ReentrantCallbackGroup: callbacks in groups can run in parallel, even the same callback multiple times — faster but have to take care of locking shared data yourself.

Two callbacks located in two different groups can run in parallel. This is how large nodes (Nav2) both receive /scan and calculate the path and respond to the service without blocking each other.

Try it

Observe the real timer rhythm with the existing node:

# Terminal 1
ros2 run ros2_basics sensor_publisher --ros-args -p publish_rate_hz:=10.0
# Terminal 2 — actual frequency, must be ≈ 10 Hz
ros2 topic hz /sensor_data

Experiment (temporarily edit the code and then rebuild): add time.sleep(0.3) to the beginning of publish_reading in sensor_publisher.py. The timer is still set to 10 Hz, but ros2 topic hz is only about 3 Hz — the executor must wait for the previous callback to complete before calling the next time. Remember to delete that line after trying.

Self-check

What is Callback? What types of callbacks does A ROS 2 node have?

The function is registered for the executor to call when an event occurs: timer deadline, message arriving on a subscribed topic, request to service, (and goal/feedback of action).

What does rclpy.spin(node) do? What if I forget to call it?

Run executor: wait for events and call the corresponding callback, until turned off. If you forget to call, no callback will run — the node does not publish according to the timer, does not receive messages.

Timer set to 50 Hz but measured 20 Hz. Common causes?

Callback (of a timer or other callback on the same executor thread) runs too long, blocking the executor. Solution: make callbacks lighter, or separate heavy work to another callback group with MultiThreadedExecutor.

Why calling a synchronous service in a callback can cause the node to hang?

With a 1-thread executor, the callback is waiting for a response, but the response is only processed when the executor is free — that is, when the callback ends. The two sides wait forever for each other (deadlock).

Callbacks, timers, executors · Module 1: ROS 2 Core Concepts — Atlas Robotics