Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the "next_deadline" bug. #2

Merged
merged 3 commits into from
Aug 23, 2024
Merged

Fix for the "next_deadline" bug. #2

merged 3 commits into from
Aug 23, 2024

Conversation

serges147
Copy link
Collaborator

  • 1st commit is just unit test which reproduces the problem
  • 2nd commit - the fix, so CI is green again.

@serges147 serges147 self-assigned this Aug 22, 2024
// So, `next_deadline` should be `time_point::max()` b/c there will be nothing left pending after spin.
SteadyClockMock::advance(1000ms + 30ms);
const auto out = evl.spin();
EXPECT_THAT(out.next_deadline.time_since_epoch(), time_point::max().time_since_epoch());
Copy link
Collaborator Author

@serges147 serges147 Aug 22, 2024

Choose a reason for hiding this comment

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

At this line @ 350 we've got incorrect next deadline time before the fix (1000ms instead of correct max).

});

const auto out = evl.spin();
EXPECT_THAT(out.next_deadline.time_since_epoch(), duration::max());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And @ 375 as well.

.approx_now = time_point::min()};
if (tree_.empty())
{
result.approx_now = Clock::now();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In original implementation, we always had 2 consecutive now() calls on the very first loop iteration for the future scheduled event. Now we do only minimum (1) call per iteration.

Copy link
Member

Choose a reason for hiding this comment

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

Please tag this branch with [[unlikely]]. Maybe there are other places that could use this.

lateness = now - next;
if (lateness < duration::zero()) // Nope, even with the latest time sample we are still early -- exit.
result.approx_now = Clock::now(); // The number of calls to Clock::now() is minimized.
if (result.approx_now < deadline) // Nope, even with the latest time sample we are still early -- exit.
Copy link
Collaborator Author

@serges147 serges147 Aug 22, 2024

Choose a reason for hiding this comment

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

Note, that I also eliminated subtraction (which is over/under-flow prone strictly speaking). Now I subtract later, when I know for sure that now >= deadline (at line @ 333). Just in case, I will add separate unit test with deadline scheduling time near to timepoint::max - probably it will overflow even now...

EXPECT_THAT(1, evl->getTree().size()); // Second dropped.
EXPECT_TRUE(a);
EXPECT_THAT(13'000ms, a.value().deadline.time_since_epoch());
EXPECT_THAT(a.value().deadline.time_since_epoch(), 13'000ms);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In gtest it is always first actual value, and 2nd-ly the expected - result output (in case of failure) explicitly mentions what was expected and was actually found).

.approx_now = time_point::min()};
if (tree_.empty())
{
result.approx_now = Clock::now();
Copy link
Member

Choose a reason for hiding this comment

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

Please tag this branch with [[unlikely]]. Maybe there are other places that could use this.

@serges147 serges147 merged commit d5c8510 into master Aug 23, 2024
14 checks passed
@serges147 serges147 deleted the sshirokov/defer_bug branch August 23, 2024 08:35
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.

2 participants