Fix rclpy async executor sleep.#1661
Conversation
The python asyncio sleep allows for a bit of clock resolution sloppiness (see https://github.com/python/cpython/blob/50476a7e87926cf6a9ea978943c4a3d5d771c95f/Lib/asyncio/base_events.py#L2036) which means that the asyncio sleep can actually wake up *before* the requested amount of time has passed. This is mostly a problem on Windows, where the tick time is 15ms, and we see some tests in rclpy fail a number of times there before eventually succeeding. There are 2 ways we can fix this: either by making the tests accept some slop, or by making the rclpy AsyncClock sleep go back to sleep if it wakes up early. The former is more like Python's asyncio, the latter is more like the rclcpp RCL_TIME sleep. I chose to go with the latter because this is a robotics framework and waking up too early is as bad as waking up too late. But everyone has to deal with the latter situation *anyway*, so it seems more predictable to always sleep at least the requested amount of time. That's what this commit does. Signed-off-by: Chris Lalancette <clalancette@gmail.com>
fujitatomoya
left a comment
There was a problem hiding this comment.
lgtm.
note that #1660 tries to mitigate the windows test failures from the same root cause. should we keep #1660 with this PR, or this PR should fix them up? IMO, that would be okay to keep the slop in the test. what do you think?
CC: @nadavelkabets
|
I'll look into this tonight when I get home, that's an interesting question I debated when writing the async timer. |
I'm sorry, I should have looked for open PRs before opening this, and I missed #1660 . My personal opinion is that we should decide whether we want to allow the wakeup sloppiness or not, and based on that decision, take one or the other. Taking both is not necessarily a bad thing, but it does mean that we aren't testing this particular invariant (either allow early wakeups, or never allow early wakeups). |
|
@clalancette is this something that we should backport ? |
Yeah, this would be a good one to get into Lyrical. |
|
@Mergifyio backport lyrical |
✅ Backports have been createdDetails
|
Description
The python asyncio sleep allows for a bit of clock resolution sloppiness (see https://github.com/python/cpython/blob/50476a7e87926cf6a9ea978943c4a3d5d771c95f/Lib/asyncio/base_events.py#L2036) which means that the asyncio sleep can actually wake up before the requested amount of time has passed. This is mostly a problem on Windows, where the tick time is 15ms, and we see some tests in rclpy fail a number of times there before eventually succeeding.
There are 2 ways we can fix this: either by making the tests accept some slop, or by making the rclpy AsyncClock sleep go back to sleep if it wakes up early. The former is more like Python's asyncio, the latter is more like the rclcpp RCL_TIME sleep.
I chose to go with the latter because this is a robotics framework and waking up too early is as bad as waking up too late. But everyone has to deal with the latter situation anyway, so it seems more predictable to always sleep at least the requested amount of time. That's what this commit does.
Is this user-facing behavior change?
No.
Did you use Generative AI?
Yes, Claude Opus 4.7
Additional Information
N/A