-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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(Duration)
improperly coerces nanoseconds and microseconds
#3920
Comments
I am happy to raise a PR if this is indeed a bug! |
The proposal to round sub-millisecond components up sounds completely reasonable. |
Prior to this commit Durations used in for delays or timeouts lost their nanosecond granularity when being converted to a millisecond Long value. This effectively meant that delays could resume prior to when they were scheduled to do so. This commit solves this by rounding a Duration with nanosecond components up to the next largest millisecond. Closes Kotlin#3920
When merged, #3921 should solve this bug, but it's worth considering that the delegation of I don't know if having nanosecond or microsecond granularity has a real use case, so maybe not worth the effort. |
Prior to this commit Durations used in for delays or timeouts lost their nanosecond granularity when being converted to a millisecond Long value. This effectively meant that delays could resume prior to when they were scheduled to do so. This commit solves this by rounding a Duration with nanosecond components up to the next largest millisecond. Closes Kotlin#3920
Microseconds granularity has a use-case in device operation scheduling. It is not frequently used, but I see some cases, where it could appear. |
@SPC-code, that's irrelevant to this PR, as the precision of Please file an issue if you have a use case for sub-millisecond scheduling precision. |
This resolves a bug in kotlinx-coroutines that this project exposed. See Kotlin/kotlinx.coroutines#3920 for more details.
Describe the bug
I am currently unsure if this is desired behavior, so if it is, please let me know.
When invoking a delay with a
Duration
, the value of that duration is coerced to milliseconds and then delegated todelay(Long)
. It is not documented behavior that delaying with a duration loses granularity for nanoseconds or microseconds. For example, delaying with998.75909ms
gets coerced to a call of998ms
when delegating todelay(Long)
.The offending code is the following:
What happened? What should have happened instead?
The symptom of this issue is that when working on implementing a cron-like API for delay based job scheduling, I was having jobs invoked up to 600us before they should have been invoked. The end result was that jobs scheduled to occur at the beginning of a second in a certain time zone would invoke early once and then as expected afterwards in quick succession.
The above function
Duration.toDelaymillis
should likely coerce durations which have a nanosecond component upduration.inWholeMilliseconds + 1
.Locally this implementation is working well for me.
Provide a Reproducer
The text was updated successfully, but these errors were encountered: