rclpy and asyncio

rclpy and asyncio: no running event loop, hung service calls, and the thread bridge that works

Short answer: rclpy doesn’t run on asyncio. An async def callback is driven by rclpy’s own executor, so await asyncio.sleep() inside it raises RuntimeError: no running event loop. You can await rclpy futures there, like client.call_async(), but on a single-threaded executor the call hung in every setup I tried except one: the async callback in a ReentrantCallbackGroup. If you need real asyncio (websockets, aiohttp), give asyncio the main thread and run rclpy.spin in a background thread.

2. await client.call_async() in an async callback — measured results table
At a glance: the main measured table from this article. Full details below.

What I ran

One Python script, one node, run in the official ros:humble-ros-base (rclpy 3.3.22, Python 3.10.12) and ros:jazzy-ros-base (rclpy 7.1.12, Python 3.12.3) Docker images on 2026-10-26. Both distros gave the same results except where noted. The script and raw output are linked at the end.

1. await asyncio.sleep() in an rclpy callback

async def cb():
    await asyncio.sleep(0.1)

node.create_timer(0.5, cb)
rclpy.spin(node)
[A] cb start
[A] EXC RuntimeError no running event loop

That happened on every tick, on both distros. rclpy accepts the coroutine and steps it itself. No asyncio loop is running in that thread, so anything that needs one fails. A plain (non-async) callback that calls asyncio.run(asyncio.sleep(0.1)) does work. It took 0.10 s each time. But it blocks the executor for as long as the coroutine runs, so it only suits short one-off work.

If all you need is to wait inside an async callback, there’s an rclpy-native way that doesn’t block. Create a one-shot timer that completes an rclpy Future, and await that future. I ran 50 waits of 0.02 s on Humble (rclpy 3.3.22), Jazzy (7.1.12) and Lyrical (10.0.11). They took 1.02–1.03 s in total, the worst overshoot was 0.6–1.6 ms, and a 20 Hz timer on the same node kept firing. One catch: if the timer only calls cancel(), it stays on the node. After 50 waits the node held 51 timers. Calling destroy_timer left just the 20 Hz one:

from rclpy.task import Future

def async_sleep(node, sec):
    fut = Future()
    timer = None
    def done():
        fut.set_result(None)
        node.destroy_timer(timer)  # cancel() alone leaves the timer on the node
    timer = node.create_timer(sec, done)
    return fut

# inside an async callback:
#     await async_sleep(self, 0.5)

On Lyrical there’s also rclpy.experimental.AsyncNode, which runs the node on an asyncio loop without an rclpy executor. There, await node.get_clock().sleep(0.02) worked. 50 sleeps took 1.02 s, worst overshoot 1.5 ms, and a timer on the node kept ticking. It’s marked experimental and is only exported on Python 3.12 or newer.

2. await client.call_async() in an async callback

This is the pattern rclpy is built for: the client future is an rclpy future, so the executor can resume your coroutine when the response arrives. I gave each variant 3 seconds on a SingleThreadedExecutor. The service server was a trivial std_srvs/Trigger handler in the same process.

Setup Result
Timer, client and server all in the node’s default group Hung (no response in 3 s)
Server moved to a second node on the same executor Hung
Client given its own MutuallyExclusiveCallbackGroup Hung
Timer in a ReentrantCallbackGroup Response at once (success= True)

My reading of this: while the coroutine sits at await, rclpy still counts the timer callback as running, so it keeps its mutually exclusive group busy. The default group also holds the client (which has to process the response) and, in the first row, the server. Moving only one of them out of that group wasn’t enough. Putting the awaiting callback in a reentrant group was. The ROS 2 guide on callback groups gives the same rule for synchronous calls: the callback and the client making the call need to be in different groups, or in a reentrant group (Using callback groups).

from rclpy.callback_groups import ReentrantCallbackGroup

async def cb():
    res = await cli.call_async(Trigger.Request())

node.create_timer(0.5, cb, callback_group=ReentrantCallbackGroup())

3. spin_until_future_complete inside a callback

This is the sync-style version people try next. It fails, and the two distros fail differently:

  • Humble: no error. It waits the full timeout_sec=2.0 and returns with future.done() == False. If you don’t check done(), you get a silent 2-second stall and no result.
  • Jazzy: raises RuntimeError: Executor is already spinning right away.

Don’t call any spin* function from inside a callback. Use an async callback (section 2) or the thread split below.

4. asyncio on the main thread, rclpy in a thread

When you need actual asyncio libraries, let asyncio own the main thread and hand ROS messages over with loop.call_soon_threadsafe:

async def main():
    loop = asyncio.get_running_loop()
    q = asyncio.Queue()
    node.create_subscription(
        String, "chat",
        lambda m: loop.call_soon_threadsafe(q.put_nowait, m.data), 10)
    threading.Thread(target=rclpy.spin, args=(node,), daemon=True).start()
    while True:
        data = await q.get()
        ...  # await websockets, aiohttp, etc. here

rclpy.init()
node = Node("bridge")
asyncio.run(main())

Publishing 5 messages from the asyncio side and reading them back through the subscription gave received 5 ['msg0', 'msg1', 'msg2', 'msg3', 'msg4'] on both distros. Calling publisher.publish() straight from a coroutine worked here. To start a coroutine from a ROS callback, use asyncio.run_coroutine_threadsafe(coro, loop). Don’t call q.put_nowait directly from the ROS thread. asyncio.Queue isn’t thread-safe, which is why the callback goes through call_soon_threadsafe.

Which one to use

  • Only need to await service or action results: an async callback in a ReentrantCallbackGroup. No asyncio involved.
  • Need asyncio libraries: asyncio on the main thread, rclpy.spin in a daemon thread, and call_soon_threadsafe / run_coroutine_threadsafe across the boundary.
  • Only need to wait inside an async callback: await a one-shot timer future and destroy the timer when it fires. On Lyrical, AsyncNode with get_clock().sleep() is an experimental alternative.
  • Short one-off async call from a sync callback: asyncio.run() works, but it blocks every other callback on that executor until it returns.

FAQ

Why does await asyncio.sleep() in an rclpy callback raise “no running event loop”?

rclpy drives an async def callback with its own executor, not asyncio. No asyncio loop is running in that thread, so anything that needs one fails.

Why does await client.call_async() hang in an async callback?

On a SingleThreadedExecutor it hung in every setup I tried except one. With the timer callback in a ReentrantCallbackGroup, the response came at once.

How do I wait inside an async rclpy callback without blocking?

Await an rclpy Future that a one-shot timer completes, and call destroy_timer when it fires. 50 waits of 0.02 s took 1.02–1.03 s in total on Humble, Jazzy and Lyrical, and a 20 Hz timer on the same node kept firing.

Can I call spin_until_future_complete inside a callback?

No. On Humble it waited the full timeout_sec=2.0 and returned with the future not done. On Jazzy it raised RuntimeError: Executor is already spinning.

How do I use asyncio libraries like websockets or aiohttp with rclpy?

Give asyncio the main thread and run rclpy.spin in a daemon thread. Hand messages across with loop.call_soon_threadsafe, and start coroutines from ROS callbacks with asyncio.run_coroutine_threadsafe.

Not tested

MultiThreadedExecutor, action clients, Iron and Rolling, the rest of Lyrical’s AsyncNode API (I only measured its sleep), and third-party asyncio executors for rclpy. I only used a timer as the calling callback. Subscription callbacks may behave the same, but I didn’t measure them.

Script and raw output: evidence folder. Questions this answers: How to bridge rclpy with Python’s asyncio?, Deadlock with spin_until_future_complete in a timer. Related: rclpy Rate.sleep() blocking forever.