Skip to content

Fix rclpy async executor sleep.#1661

Merged
mjcarroll merged 1 commit into
rollingfrom
clalancette/fix-rclpy-async-sleep
May 14, 2026
Merged

Fix rclpy async executor sleep.#1661
mjcarroll merged 1 commit into
rollingfrom
clalancette/fix-rclpy-async-sleep

Conversation

@clalancette

Copy link
Copy Markdown
Contributor

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

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>
@clalancette

clalancette commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@fujitatomoya fujitatomoya left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@nadavelkabets

Copy link
Copy Markdown
Contributor

I'll look into this tonight when I get home, that's an interesting question I debated when writing the async timer.

@clalancette

Copy link
Copy Markdown
Contributor Author

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?

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).

@mjcarroll mjcarroll merged commit 1e5e267 into rolling May 14, 2026
3 checks passed
@christophebedard christophebedard deleted the clalancette/fix-rclpy-async-sleep branch May 14, 2026 16:37
@ahcorde

ahcorde commented May 21, 2026

Copy link
Copy Markdown
Contributor

@clalancette is this something that we should backport ?

@clalancette

Copy link
Copy Markdown
Contributor Author

@clalancette is this something that we should backport ?

Yeah, this would be a good one to get into Lyrical.

@clalancette

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport lyrical

@mergify

mergify Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

backport lyrical

✅ Backports have been created

Details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants