"ValueError: generator already executing" and rclpy service calls that deadlock in callbacks: executors and callback groups, measured

"ValueError: generator already executing" and rclpy service calls that deadlock in callbacks: executors and callback groups, measured

Traceback (most recent call last):
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 323, in spin
    self.spin_once()
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 867, in spin_once
    self._spin_once_impl(timeout_sec)
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 848, in _spin_once_impl
    handler, entity, node = self.wait_for_ready_callbacks(
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 784, in wait_for_ready_callbacks
    return next(self._cb_iter)
ValueError: generator already executing

On Jazzy and Lyrical the same mistake gives a different error, raised in the call you just made:

  File "/opt/ros/jazzy/lib/python3.12/site-packages/rclpy/executors.py", line 222, in _enter_spin
    raise RuntimeError('Executor is already spinning')
RuntimeError: Executor is already spinning

Short answer: both errors mean one executor is being spun from two places at once. On Humble that was a second rclpy.spin() on the global executor from another thread, a rclpy.spin_once() without a timeout from another thread, or executor.spin_once() without a timeout inside a callback of a MultiThreadedExecutor. Don't spin again to wait for a service. Spin once, in one place, and wait in a way the executor can still serve:

from std_srvs.srv import Trigger
from rclpy.callback_groups import MutuallyExclusiveCallbackGroup
from rclpy.executors import MultiThreadedExecutor

# in __init__
self.cli = self.create_client(Trigger, 'trig',
                              callback_group=MutuallyExclusiveCallbackGroup())
self.create_timer(0.2, self.on_timer)        # stays in the default group

def on_timer(self):
    resp = self.cli.call(Trigger.Request())  # blocking call, now safe

# in main
ex = MultiThreadedExecutor(num_threads=4)
ex.add_node(node)
ex.spin()

The client has its own callback group and the executor has more than one thread. That combination completed 10 of 10 calls on Humble, Jazzy and Lyrical, with a median of 1.4 to 1.5 ms per call. A blocking call() in the default group never completed, whatever the executor. If you would rather not block a thread, call_async() with add_done_callback() worked in every executor and group I tried.

A slow callback and a 10 Hz timer — measured results table
At a glance: the main measured table from this article. Full details below.

Find your symptom

What you see What I measured as the cause Fix that worked
ValueError: generator already executing (Humble) Two threads spinning the same executor: two rclpy.spin() calls, rclpy.spin() plus rclpy.spin_once() with no timeout, or ex.spin_once() inside an MTE callback Spin once, from one thread
RuntimeError: Executor is already spinning (Jazzy, Lyrical) Any second spin call on an executor that is already spinning, including from inside its own callback Same
Timer callback calls client.call() and never returns Client in the same mutually exclusive group as the callback, or a SingleThreadedExecutor Client in a separate group or Reentrant group, plus an MTE with 2+ threads
async def callback, await client.call_async() never returns Callback and client both in the node's default group Client in a separate group (works with an STE too)
Timer runs once and never again rclpy.spin_until_future_complete(node, future) called inside a callback call_async + add_done_callback
10 Hz timer runs at about 5.9 Hz with 1 s gaps A slow callback in the same group, or an executor with 1 thread Separate group plus 4 threads: 10.00 Hz
TypeError: Client.call() got an unexpected keyword argument 'timeout_sec' Humble's call() has no timeout Use call_async on Humble

What I ran

On 2026-11-01 I used the official ros:humble-ros-base, ros:jazzy-ros-base and ros:lyrical-ros-base images on a 2 vCPU Linux host, one distro at a time. A std_srvs/srv/Trigger server ran in its own process and answered at once. A second process held the node under test. Each case ran in a fresh process, and the main thread waited with a deadline, so a deadlock showed up as "0 of 10 calls within 8 s" instead of a hung test. Before each case, the node made one warm-up call on a throwaway executor. The reason is in the section on the first request below. The cases were:

  • A 5 Hz timer calls the service 10 times with call(), call_async() plus add_done_callback(), or await call_async() in an async def callback. The executor was a SingleThreadedExecutor (STE) or a MultiThreadedExecutor(num_threads=4) (MTE). The callback and client shared the node's default group, sat in separate MutuallyExclusiveCallbackGroups, or shared one ReentrantCallbackGroup.
  • Nested spinning: spin_once, spin_until_future_complete and rclpy.spin called from inside a callback or from a second thread.
  • A 10 Hz timer next to a 2 s timer whose callback sleeps 1 s, measured over 10 s.

A blocking call() inside a callback

Completed calls out of 10 within 8 s. All three distros gave the same counts:

Executor Default group Client in its own group One Reentrant group
SingleThreadedExecutor 0 (deadlock) 0 (deadlock) 0 (deadlock)
MultiThreadedExecutor, 4 threads 0 (deadlock) 10, median 1.42 to 1.51 ms 10, median 1.45 to 1.59 ms

call() blocks the thread that runs your callback until the response is processed. The response is processed by a callback too, and it needs a free thread and a callback group that isn't busy. An STE has one thread, and your callback holds it, so callback groups make no difference there. An MTE has free threads, but the node's default group is mutually exclusive, so the response waits until your callback returns, and your callback waits for the response. The Using Callback Groups guide says it plainly: "every node's default callback group is a Mutually Exclusive Callback Group", and if everything uses it, "that node essentially acts as if it was handled by a Single-Threaded Executor, even if a multi-threaded one is specified!"

Both fixes worked. A separate mutually exclusive group for the client still keeps the timer from overlapping itself. A Reentrant group lets the timer callback run again while an earlier call is still waiting. Pick the separate group unless you want that overlap.

Jazzy and Lyrical accept call(request, timeout_sec=2.0). In a deadlocked setup that doesn't fix anything, it only ends the wait. Jazzy returned None after 2.00 s, and Lyrical raised TimeoutError(). Humble raised TypeError("Client.call() got an unexpected keyword argument 'timeout_sec'"). If your code runs on several distros, don't rely on either behavior.

await in an async callback still holds the group

I expected await self.cli.call_async(req) in an async def timer callback to be safe, because the coroutine yields while it waits. With the callback and client in the default group it deadlocked: 0 of 10 calls on all three distros, with both an STE and an MTE. While the coroutine waits, the executor still counts the callback as running in its mutually exclusive group, so the client's response can't run in that group. My test showed it, and the result fits that explanation. When I moved the client to its own group, or used one Reentrant group, it completed 10 of 10 calls, even on an STE:

await call_async() Humble Jazzy Lyrical
STE, default group 0/10 0/10 0/10
STE, client in its own group 10/10, 1.13 ms 10/10, 1.07 ms 10/10, 0.93 ms
STE, Reentrant 10/10, 1.02 ms 10/10, 1.21 ms 10/10, 1.19 ms
MTE 4, default group 0/10 0/10 0/10
MTE 4, client in its own group 10/10, 1.39 ms 10/10, 1.35 ms 10/10, 1.26 ms
MTE 4, Reentrant 10/10, 1.49 ms 10/10, 1.45 ms 10/10, 1.47 ms

Latency is the median of 10 calls. If your awaits hang inside an asyncio event loop rather than in an rclpy callback, that's a different failure, covered in rclpy asyncio "no running event loop" and call_async hangs.

Where "generator already executing" comes from

On Humble, every executor keeps one Python generator that yields ready callbacks, and spin_once pulls the next item from it. If a second thread asks for the next item while the first thread is still inside the generator, Python raises ValueError: generator already executing. Here is what each pattern did on Humble:

Pattern (Humble) Result
rclpy.spin(node) in two threads Second spin raised ValueError. The 100 Hz timer had fired 30 times before that and never fired again.
rclpy.spin(node) in a thread, rclpy.spin_once(node) in a loop in main ValueError on the first iteration
Same, but rclpy.spin_once(node, timeout_sec=0.01) 401 iterations, no exception
Same, but rclpy.spin_until_future_complete(node, f, timeout_sec=1.0) 656 iterations, no exception
MTE callback calls ex.spin_once() The outer ex.spin() raised ValueError (traceback at the top), and the call never returned
STE callback calls ex.spin_once() No error, but it never returned. The timer fired once in 4 s.
STE or MTE callback calls ex.spin_once(timeout_sec=1.0) Returned after 1.000 s, no error. The timer fired 16 times in 4 s instead of about 20.
STE or MTE callback calls ex.spin_until_future_complete(f, timeout_sec=3.0) Returned after 3.000 s with the future still not done. The timer fired 6 times.

Look at the rows with a timeout. The same double spinning didn't raise when the second call passed a timeout. So "no error" on Humble doesn't mean the code is right. The nested calls with a timeout still stalled the timer, and the nested spin_until_future_complete never got its response. The error only shows up in some of these patterns, and the bug is in all of them.

Jazzy and Lyrical check this up front. Every second spin call on an executor that was already spinning raised RuntimeError: Executor is already spinning at once: spin_once with or without a timeout, spin_until_future_complete, from a callback or from another thread. Callbacks that raised it didn't block the executor, and the timer kept firing (20 times in 4 s). If you upgrade from Humble and this error appears, that code was already broken. Humble just didn't tell you.

One effect was the same on all three distros. After a second rclpy.spin(node) failed, the timer stopped for good: 30 ticks before the failure and 30 in total after 4.3 s. The first spin() was still running, but the node's timer no longer fired. When the error comes from a second rclpy.spin, restart the process. Catching the exception doesn't bring the node back.

spin_until_future_complete inside a callback stops the timer

A common pattern is rclpy.spin_until_future_complete(self, future) inside a timer callback while the node runs in its own executor. On all three distros, with an STE or an MTE, the call returned after its 3.0 s timeout with future.done() still False. After that the timer never fired again, so it fired once in 4 s. That matches the report in rclpy issue 1018, where a timer runs once and then hangs. According to the discussion in that issue, the module-level helper uses the global executor, not the one that spins your node, and removes the node from the executor when it returns. Don't call it from a callback. Use call_async and handle the response in a done callback.

A blocking rate.sleep() is a neighbouring case: rclpy rate.sleep() blocks forever: why and how to fix.

A slow callback and a 10 Hz timer

A second timer every 2 s ran a callback that slept for 1 s. I counted the 10 Hz timer's ticks over 10 s:

Executor, groups Humble Jazzy Lyrical
STE 5.90 Hz, max gap 1002 ms 5.90 Hz, 1001 ms 5.90 Hz, 1001 ms
MTE 1 thread, same group 5.89 Hz, 1002 ms 5.90 Hz, 1001 ms 5.90 Hz, 1001 ms
MTE 1 thread, separate groups 5.89 Hz, 1001 ms 5.90 Hz, 1000 ms 5.90 Hz, 1001 ms
MTE 1 thread, Reentrant 5.89 Hz, 1001 ms 5.90 Hz, 1000 ms 5.90 Hz, 1000 ms
MTE 4 threads, same (default) group 5.89 Hz, 1002 ms 5.90 Hz, 1001 ms 5.50 Hz, 1101 ms
MTE 4 threads, separate groups 9.99 Hz, 111 ms 10.00 Hz, 100 ms 10.00 Hz, 100 ms
MTE 4 threads, Reentrant 9.99 Hz, 100 ms 10.00 Hz, 100 ms 10.00 Hz, 100 ms

You need both changes. Four threads with one group behaved like one thread, and separate groups with one thread behaved like one thread. Only separate groups with 4 threads kept 10 Hz. In the blocked setups the fast timer fired 59 times in 10 s instead of 100, and each 1 s sleep showed up as a gap of about 1 s.

Fix options compared

Median latency of 10 calls from a timer callback to an idle Trigger server in another process:

Fix Executor needed Humble Jazzy Lyrical
call(), client in its own MutuallyExclusive group MTE, 2+ threads 1.51 ms 1.46 ms 1.42 ms
call(), one ReentrantCallbackGroup MTE, 2+ threads 1.59 ms 1.54 ms 1.45 ms
call_async() + add_done_callback(), default group STE 1.24 ms 1.12 ms 1.16 ms
call_async() + add_done_callback(), default group MTE 4 1.71 ms 1.52 ms 1.51 ms
await call_async(), client in its own group STE 1.13 ms 1.07 ms 0.93 ms
await call_async(), default group any deadlock, 0/10

All the working fixes were within a millisecond of each other, so pick by how the code reads, not by speed. call_async with a done callback was the only one that worked with no callback group changes at all. In my done-callback test, the timer skipped its tick while a call was pending, so at most one call was in flight. Without a guard like that, a fast timer and a slow server pile up requests. await reads like blocking code and works on one thread, as long as the client has its own group.

The first request after wait_for_service can get no reply

This one cost me a wrong result. In my first Humble run, without the warm-up call, the MTE plus Reentrant setup completed 0 of 10 calls. On the next run, with a warm-up, the same setup completed 10 of 10. The cause was the first request, not the callback group. In the warm-up step, the first request sent right after wait_for_service() returned got no reply within 2 s in 23 of 29 processes on Humble, 28 of 29 on Jazzy and 25 of 29 on Lyrical. A second or third request always got through. If your first call() hangs and later ones work, don't blame the callback groups yet. Retry with call_async and a timeout before you start the real work. I didn't find out where the first reply goes.

Version notes

  • Humble (rclpy 3.3.22): the double-spin error is ValueError: generator already executing, and it only appeared in some patterns. Several nested spins with a timeout ran without any error.
  • Jazzy (rclpy 7.1.12) and Lyrical (rclpy 10.0.11): RuntimeError: Executor is already spinning, raised from _enter_spin by every second spin call.
  • Client.call(timeout_sec=...): TypeError on Humble, returns None on Jazzy, raises TimeoutError on Lyrical.
  • The deadlock, await and slow-callback results were the same on all three distros. The only outlier was Lyrical's 5.50 Hz for 4 threads with one group.
  • Jazzy nodes needed more threads than Humble's. With --pids-limit 30 on the container, every Jazzy node failed with Failed to create std::thread: Resource temporarily unavailable, so I ran all distros with 128.

Not tested

rclcpp, action clients, subscriptions as the calling callback, a server inside the same node or process, EventsExecutor, MultiThreadedExecutor with the default thread count, RMWs other than the default, networks with latency, Iron and Rolling. All timings come from one 2 vCPU host with an idle server, so treat the millisecond numbers as relative.

FAQ

What causes ValueError: generator already executing in rclpy?

On Humble, two threads spinning the same executor at once. I got it with two rclpy.spin() calls, with rclpy.spin() plus a rclpy.spin_once() without a timeout, and with ex.spin_once() inside a MultiThreadedExecutor callback. Spin from one thread only.

Why does client.call() inside a timer callback hang forever?

The response needs a free thread and a free callback group. With a SingleThreadedExecutor, or with the client in the node's default group, it had neither, and 0 of 10 calls completed on Humble, Jazzy and Lyrical. Put the client in its own callback group and use a MultiThreadedExecutor with more than one thread.

Does a ReentrantCallbackGroup fix the deadlock?

Yes, with a MultiThreadedExecutor: 10 of 10 calls, median about 1.5 ms. With a SingleThreadedExecutor it still deadlocked, because the only thread was blocked in the callback.

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

While the coroutine waits, the callback still holds its mutually exclusive group, and in my test the client's response couldn't run in that group. With both in the default group it deadlocked on every distro. A separate group for the client fixed it, even on a SingleThreadedExecutor.

What does RuntimeError: Executor is already spinning mean?

It replaces the Humble ValueError on Jazzy and Lyrical. Your code called spin, spin_once or spin_until_future_complete on an executor that was already spinning. Remove the nested spin and use call_async with a done callback, or a separate callback group.

Measured on 2026-11-01 with ros:humble-ros-base (sha256:d9bcf2e6f72d..., rclpy 3.3.22, rclcpp 16.0.21, ros2cli 0.18.20, rmw 6.1.4, rmw_fastrtps_cpp 6.2.10, Python 3.10.12), ros:jazzy-ros-base (sha256:8eed0a4669bb..., rclpy 7.1.12, rclcpp 28.1.22, ros2cli 0.32.13, rmw 7.3.4, rmw_fastrtps_cpp 8.4.4, Python 3.12.3) and ros:lyrical-ros-base (sha256:3224ee150cb0..., rclpy 10.0.11, rclcpp 32.0.3, ros2cli 0.40.9, rmw 7.10.2, rmw_fastrtps_cpp 9.4.10, Python 3.14.4), with RMW_IMPLEMENTATION unset. Full digests are in digests.txt.

Script and raw output: evidence folder. Questions this answers: Node keeps crashing due to: ValueError: generator already executing, Spinning multiple nodes across multiple threads, How to implement a sync service client in a node?, Asynchronous client calls in a timer cause the timer to never execute a second time.