Repository navigation
Clock::sleep_for() to be time shifts resistant - #2386
AlexeyMerzlyakov wants to merge 1 commit into
Conversation
Signed-off-by: Alexey Merzlyakov <[email protected]>
| if (now() < curr) { | ||
| // Time jumped in the past: correcting until value on time shift | ||
| const Duration delta = curr - now(); | ||
| until -= delta; |
There was a problem hiding this comment.
Thank you for the PR! It looks like this is keeping track of time already slept when ROS time is active and jumps backwards, and then using that to credit the amount of time slept.
This doesn't seem correct to me. ROS time being active indicates something artificial messing with time, such as simulation or replaying a bag. Say a bag was playing and a node was waiting 60 seconds using sleep_for(), but someone kept replaying 5 seconds in the middle while debugging some other part of the system. If those 5 seconds were credited towards the "sleep_for" time then from the perspective of the simulated time the sleep period would be completed faster than 60 simulated seconds.
I think treating backwards time jumps as if no time had elapsed is the correct behavior in that case.
There was a problem hiding this comment.
Thank you for the review and reasonable notice. Yes, indeed from this point of view, I would like agree that we will miss this use-case, and thus vanilla clock->sleep_for() behavior seems to be correct.
However, developing a little bit further your example with replaying rosbag and some node that we are using for debugging. Imagine that this node is additionally publishing something using sleep_for while we are waiting 60 seconds, e.g.:
while (ok) {
pub_->publish(some_msg);
this->get_clock()->sleep_for(100ms);
}
Vanilla implementation of clock->sleep_for() might cause the problem to debug this node, as once ROS-time was jumped backward during the sleep, this->get_clock()->sleep_for(100ms) would not complete within expected period of time and no new messages will be arrived, when (as mentioned above) someone will re-play back rosbag file for debugging.
From one point of view, when sleep_for() API is used "out of" debugging, we should not react to time shifts to the past. However, from another point of view, when sleep_for() is used in debugging code, we will get stuck, and get non-reproducible program behavior.
So, I am still have some doubts, what is better to do here: leave Clock::sleep_for() to not consider time shifts (in our case: for ROS-time shifts, but I believe the logic of your reasoning could also be moved to System and non-activated ROS clocks), so we should remain sleep_for() as a wrapper for sleep_until();
or sleep_for() should be time shifts - resistant routine?
I think, this is some kind of fundamental agreement question of how we want this should work. And I could miss something else?
There was a problem hiding this comment.
Say a bag was playing and a node was waiting 60 seconds using sleep_for(), but someone kept replaying 5 seconds in the middle while debugging some other part of the system. If those 5 seconds were credited towards the "sleep_for" time then from the perspective of the simulated time the sleep period would be completed faster than 60 simulated seconds.
i am not sure why this is the problem. i think this 5 seconds has been already elapsed for the sleep_for before the time jumps backward, so that until should be deducted by this 5 seconds based on the new now()? i think that this behavior is correct for sleep_for, because it is not sleep_until. i may be mistaken, please let me know if i got anything wrong. thanks in advance.
There was a problem hiding this comment.
I tried looking at the C++ standard for this, and their description of sleep_for is ambiguous...
I quote (emphasis mine):
The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
So I think it's up to us, since a sim-time clock isn't a steady clock and makes no guarantees.
Now, in my opinion, a time jump backward in time counts as sleeping for negative time (because if time moving forward counts towards time slept for, why shouldn't time moving backwards count as anything other than (negative) time slept for?
So I lean towards @sloretz's opinion (in fact, I think time should be debited instead of being treated as no time in the case of a time jump backwards).
There was a problem hiding this comment.
@methylDragon thanks for sharing.
Just FYI, those are system or monotonic clocks.
standard recommends that a steady clock is used to measure the duration as implementation.
besides, nanosleep(2) says
POSIX.1 specifies that nanosleep() should measure time against the CLOCK_REALTIME clock. However, Linux measures the
time using the CLOCK_MONOTONIC clock. This probably does not matter, since the POSIX.1 specification for clock_set‐
time(2) says that discontinuous changes in CLOCK_REALTIME should not affect nanosleep():
Now, in my opinion, a time jump backward in time counts as sleeping for negative time (because if time moving forward counts towards time slept for, why shouldn't time moving backwards count as anything other than (negative) time slept for?
that is reasonable for ROS time. (maybe i should not have used above as reference behavior here, ROS time is the different story.)
There was a problem hiding this comment.
i am not sure why this is the problem. i think this 5 seconds has been already elapsed for the sleep_for before the time jumps backward, so that until should be deducted by this 5 seconds based on the new now()?
Current implementation of Clock::sleep_for() using once calculated until time value at its beginning, and then calling Clock::sleep_until(until). If ROS or system time was moved backwards during the sleep, until value won't be deducted.
The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
but:
This probably does not matter, since the POSIX.1 specification for clock_set‐time(2) says that discontinuous changes in CLOCK_REALTIME should not affect nanosleep()
So, what is the decision here: should we follow POSIX-way to leave all sleep-for-s to be time change independent, or we'd better to leave sleep_for() implementation as-is for active ROS clocks? (following the same logic, we'd also leave the system clock (and inactive ROS clocks as well) the same way. In this case if developer wants to have time-shift resistance sleep_for, the Clock object should be chosen of RCL_STEADY_TIME type intentionally).
There was a problem hiding this comment.
In my opinion, having sleep_for be a wrapper around sleep_until is reasonable since we are selecting the clock to use for each sleep call, and so the idiosyncrasies of each particular clock will apply. The idea of time being a discretely incrementing resource and therefore a resource that can contribute towards a sleep_for even though the "start + duration < now" (according to the clock), seems wrong to me.
Put another way, I think a test like this should be totally reasonable:
auto sleep_duration = 1s;
auto start = ros_time_clock.now();
ros_time_clock.sleep_for(sleep_duration);
auto now = ros_time_clock.now();
ASSERT(now >= start + sleep_duration);And not just for a ROS Time based clock, but any clock. Though I think there are some sleep_for like calls on Windows that are allowed to be shorter than the specified duration, but I think that's actually insane, personally. I don't believe we rely on those at all, except maybe through another library.
In the given example of a while(ok) { ... sleep_for(...); } loop, I think it's totally appropriate for that sleep_for to never exit if ROS time is looping within its start time and theoretical end time. That might be surprising to some, but I think it's the most reasonable behavior, especially if you consider the same situation, but rather than looping time in a bag you instead pause time. I don't see a good reason why iterating in a short loop should produce different behavior from being paused, personally.
Developers using ROS time need to be aware of these issues and use a steady clock in loops like these if they don't want that behavior.
There was a problem hiding this comment.
Thanks @wjwwood @fujitatomoya @sloretz @methylDragon for the opinions! I think this dotted all the i's, so the ticket could be closed: current Clock::sleep_for() behavior is as we expect it to be in ROS2; and it proper for the reasons listed above.
|
@ros2/team for other's thoughts 🧇 |
|
Closed for the reasons discussed in #2386 (comment) thread |
This PR addresses the #2383 issue.
Currently,
Clock::sleep_for()is a wrapper forClock::sleep_until()routine. Due-time once being calculated won't be changed if system or ROS-clock was shifted from one time (date) to another.The suggested patch - is the re-implementation attempt of
Clock::sleep_for()to have a resistance to time shifts.The implementation considerations are:
sleep_for()implementation could consider the sleep for a necessary amount of time, similar how doesrclcpp::sleep_for()do it. For all of these cases, the speed of time flow will be normal, and be the same as how steady time flows. So, for these cases, we are usingchrono::steady_clock, which will sleep for a necessary amount of time independently of system or ROS time changes./clocktopic for time corrections),sleep_for()should check the clock the same way assleep_until()does it, but also respecting backwards time shifts in a clock jump callbacks. If updated ROS-time was jumped backwards than previous noted time, we are correcting until-point to fit actual sleep timings.Overall implementation scheme looks like following: