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

delay(MAX_VALUE) breaks with non-monothonic clocks #1312

Closed
elizarov opened this issue Jul 2, 2019 · 1 comment
Closed

delay(MAX_VALUE) breaks with non-monothonic clocks #1312

elizarov opened this issue Jul 2, 2019 · 1 comment
Assignees
Labels

Comments

@elizarov
Copy link
Contributor

elizarov commented Jul 2, 2019

There's an circumstantial evidence that some systems may have non-monotonic implementation of System.nanoTime(). In this case the current implementation for delay(Long.MAX_VALUE) corrupts internal scheduling data structures and causes further delays to behave erratically.

@elizarov elizarov added the bug label Jul 2, 2019
@elizarov elizarov self-assigned this Jul 2, 2019
elizarov added a commit that referenced this issue Jul 6, 2019
There are two levels of protection:

* All delays for longer than Long.MAX_VALUE/2 nanos (~146 years) are
  considered to be "infinite", so adding then to the heap queue is not
  even attempted, thus minimizing a number of cases when we have
  "close to wraparound" times in there. This also works as optimization
  reducing effort, so delay(Long.MAX_VALUE) works faster.
* The invariant for delayed task's in the heap queue is defined so that
  all scheduled tasks are at most Long.MAX_VALUE nanos apart, thus
  always guaranteeing that they can be compared in a stable way despite
  wraparounds in time. The invariant is maintained by keeping
  "last observed nowTime" and making sure that all scheduled tasks
  in the queue are "after" this time.

Fixes #1312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants