Services: synchronous or asynchronous? How they work internally
Atlasbot · 16/8/2026 · 11 min read
Article "What is Service in ROS2?" introduced the basic request-response model. But there is an important detail that is often overlooked: calling a service synchronously (blocking) right inside another callback is one of the most common errors that causes a ROS2 node to "hang" for unknown reasons.
Key concepts
Client calling service has two types:
- Synchronization (blocking) — after calling, the code stops and waits until there is a response before continuing
- Async — call completes, receives a Future (object representing "future result"), code continues running immediately, handling response when it actually arrives via separate callback
Why does calling synchronously inside a callback easily cause the
Executor usually processes callbacks (timer, subscriber, service...) of a node sequentially on the same thread. If inside a running callback, the code calls the service in a blocking fashion and waits for a response — but that response needs to be processed by this node's own executor (for example, calling the service provided by that node, or depending on another callback that has not yet had its turn to run) — the result is that node waits for itself, never being able to exit. This is why ROS2 strongly recommends using asynchronous calls (call_async) instead of synchronous calls in most cases, as opposed to the more common practice in ROS1.
In short: Calling a synchronous service is only safe when it is definitely outside the executor (for example, in function
main()of a standalone script) — inside any node callback, always prioritizecall_asyncwith processing the result through a separate callback.
How it works
File .srv defines both request and response, separated by line ---:
# AddTwoInts.srv
int64 a
int64 b
---
int64 sum
Call the asynchronous service properly — no blocking, handle results via separate callback when Future completes:
future = self.client.call_async(request)
future.add_done_callback(self.handle_response) # Called when there is a response, not waiting here
def handle_response(self, future):
response = future.result()
self.get_logger().info(f'Kết quả: {response.sum}')
call_async(request)
↓
immediately returns a Future — code continues running WITHOUT waiting for the result
↓ (when response actually arrives)
executor itself calls handle_response(future)
Technical details
Parameters
Direct comparison of two ways to call services — choosing the wrong type for the right context is the source of most service-related node crashes:
| Criterion | Synchronization (blocking) | Asynchronous (call_async) |
|---|---|---|
| Does the code stop and wait? | Yes | No, pay Future immediately |
| It is safe to call in the | High deadlock risk | Safety (recommended) |
| Suitable for use in | Standalone Script, main() | Inside node/callback |
| Code complexity | Simpler than | Need to add callback to handle |
Register / Bit / Byte
Downstream, each ROS2 service request carries a GUID client identifier + sequence number attached to the header of the sent message — when the server finishes processing and publishes the response, it attaches the correct GUID + sequence number. The client uses this exact pair of information to accurately match which response corresponds to which of its own requests — a must-have mechanism because many different clients can call the same server, and the underlying DDS topic itself does not guarantee the order "which request, that response" without an accompanying identifier.
Practical applications
A more radical solution to the deadlock problem mentioned above (instead of just avoiding synchronous calls) is to use callback group (separate topic in the advanced ROS2 category) — placing the service client in a ReentrantCallbackGroup allows multiple callbacks of the same node to run overlapping on different threads, completely solving the "node waiting for itself" situation instead of just avoiding it by always use async. In complex robot control systems — where a node is sometimes both a service server and a client of another service — understanding these two tools (async + callback group) is mandatory to avoid node hanging errors that are difficult to reproduce and debug.
Practical example
# SAFE — synchronous call outside the executor, in a standalone script
def main():
rclpy.init()
node = rclpy.create_node('simple_client')
client = node.create_client(AddTwoInts, 'add_two_ints')
future = client.call_async(request)
rclpy.spin_until_future_complete(node, future) # Wait OUTSIDE the callback, it's safe
print(future.result().sum)
# RISK — calling synchronously INSIDE node's own callback
def some_subscriber_callback(self, msg):
future = self.client.call_async(request)
rclpy.spin_until_future_complete(self, future) # node waits for its own executor!
# → can hang indefinitely if the response needs to be processed by this executor
The two pieces of code look almost identical, but the second one has the risk of crashing the node because spin_until_future_complete is called from inside a callback that is taking over the node's own executor — the only difference is "where were you standing when calling" but the consequences are completely different.
Common mistakes
- Call
spin_until_future_completeor wait for synchronization inside node's own callback — as analyzed above, this is the most common cause of service-related node crashes. - Call
future.result()beforefuture.done()returns True — accessing the result when the Future is not yet completed causes an error or returnsNoneinstead of the real response — always handle the result in the callback registered viaadd_done_callback, do not arbitrarily readresult()immediately after callingcall_async. - Do not handle exceptions in the done callback — if the server encounters an error or the connection is interrupted midway,
future.result()can raise an exception — the callback handling the result should have a try/except to avoid crashing the entire node because of a failed service call. - Forget that the standalone script (not a long-term node) still needs
rclpy.spin_until_future_complete— just callingcall_asyncand reading the result immediately (no waiting) will always getNonebecause the response hasn't arrived yet.
Conclusion
Understanding the difference between synchronous and asynchronous service calls, and knowing exactly when each type is safe to use, is the most important debugging skill when a ROS2 node "hangs" for unknown reasons. Safety rule: always use call_async inside any node callback, only use synchronous calls when you are definitely outside the executor. The next article in this section is about Action — a mechanism combined from Service and Topic, inheriting both advantages and some similar pitfalls.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.