Repository navigation
Closed
Description
Bug report
Required Info:
- Operating System:
- Ubuntu 16.04 and Ubuntu 18.04
- Installation type:
- source
- Version or commit hash:
- DDS implementation:
- Fast-RTPS
- Client library (if applicable):
- rclpy
Steps to reproduce issue
With a ROS 2 workspace built and sourced:
- Create
foo_service.pywith the following content:
import asyncio
from example_interfaces.srv import AddTwoInts
import rclpy
async def add_two_ints_callback(request, response):
response.sum = request.a + request.b
await asyncio.sleep(1)
print('Service request handled')
return response
def main():
rclpy.init()
node = rclpy.create_node('foo_service')
srv = node.create_service(AddTwoInts, 'add_two_ints', add_two_ints_callback)
rclpy.spin(node)
rclpy.shutdown()
if __name__ == '__main__':
main()-
Run the service:
python3 foo_service.py -
Run a service client:
ros2 run examples_rclpy_minimal_client client
Expected behavior
Service call succeeds without error.
Actual behavior
Service server crashes from assertion error:
AssertionError: yield from wasn't used with future
Trace:
Traceback (most recent call last):
File "service.py", line 27, in <module>
main()
File "service.py", line 22, in main
rclpy.spin(node)
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/__init__.py", line 119, in spin
executor.spin_once()
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/executors.py", line 573, in spin_once
raise handler.exception()
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/task.py", line 207, in __call__
self._handler.send(None)
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/executors.py", line 324, in handler
await call_coroutine(entity, arg)
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/executors.py", line 282, in _execute_service
response = await await_or_execute(srv.callback, request, srv.srv_type.Response())
File "/home/jacob/ws/actions_ws/install/rclpy/lib/python3.5/site-packages/rclpy/executors.py", line 89, in await_or_execute
return await callback(*args)
File "service.py", line 11, in add_two_ints_callback
await asyncio.sleep(1)
File "/usr/lib/python3.5/asyncio/tasks.py", line 516, in sleep
return (yield from future)
File "/usr/lib/python3.5/asyncio/futures.py", line 362, in __iter__
assert self.done(), "yield from wasn't used with future"
AssertionError: yield from wasn't used with future
Additional information
The error appears related to the call to asyncio.sleep(1) in the service server callback.
If we replace it with time.sleep(1), the code executes without error.
Reacted by Cameron Evans, Gautam Jain and Fabian Freihube
Activity
jacobperron commentedon Mar 14, 2019
I think the only way asyncio.sleep() can be used is if we refactor executors so that they use asyncio event loops. I'm not well-versed with the decision to define our own
FutureandTaskclasses, but perhaps it's possible to replace them with equivalents in asyncio.sloretz commentedon Mar 14, 2019
There are two standard modules for executors/tasks/futures: concurrent.futures which is thread safe but does not support coroutines, and asyncio which supports coroutines but is not threadsafe. The rclpy executor supports both multiple threads and coroutines, so it has to have its own :(
TheDelus commentedon May 21, 2021
Are there plans to support pythons async and await syntax in the future? Not beeing able to use pythons own system of coroutines and async execution is one of a the big things holding us back migrating to ROS2. With ROS1 we use the workaround provided by locusrobotics/aiorospy.
Sorry for hijacking this is issue, but it appears to be the most fitting place for my question.
sloretz commentedon May 26, 2021
@TheDelus
asynccoroutines withawaitcan be used in rclpy now for pretty much any callback (subscription callback, timer, guard condition, etc). I don't know of any demos doing that, but it is tested in rclpy. There's a hidden requirement that if all coroutines areawaiting something, they'll only be woken again when something wakes the wait set. Meaning, any coroutines you make should be waiting for something like a timer or a guard condition or the receipt of a message.As for this issue, I think I'll close it.
asyncioforces everything being done in a single thread. Given the desire to let users have rclpy call callbacks in multiple threads, I don't think it can ever be made to fit.gstorer commentedon Jul 4, 2021
@sloretz I am curious exactly what is missing from asyncio to prevent it being used. Between Executing code in thread or process pools, Running in Threads and Scheduling From Other Threads it seems to me like asyncio has all the features required to do what the ROS2 executor is doing.
We have some ROS systems and are looking to build some data collection and management infrastructure to tie them together. We are planning to use services written in modern Python to do this and it would have been nice to have them interface directly with ROS. But not having proper asyncio support might mean we end up having shims to another communications stack instead.
sloretz commentedon Jul 6, 2021
@gstorer I haven't seen
to_thread()before. Maybe it's worth looking at asyncio in Python 3.9 to see if ROS 2 can make more use of it now. How do you envision it being used?Maybe a dedicated thread would block on
rcl_wait()and feed tasks to an asyncio event loop? Then anyone who wants to run stuff in another thread would useto_thread()and await the future? If you have an idea please open a new issue for it.Add smoke test adapted from ROS 1 smoke test (#585)
Add smoke test adapted from ROS 1 smoke test (#585)