question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Time.kt uses Duration.toMillis in unsafe manner

See original GitHub issue

Using Duration.ofSeconds(Long.MAX_VALUE) as the timeout argument for kotlinx.coroutines.withTimeout will cause an ArithmaticOverflowException

This is because, in pure java Durations.ofSeconds(Long.MAX_VALUE).toMillis() will cause an Arithmatic overflow.

I’ve got a fix that basically involves adding an extension function toMillisOrMax that returns Long.MAX_VALUE when toMillis would otherwise overflow.

Use case:

I’m a big fan of null objects, and the way I’ve structured things is such that the “default” timeout is Duration.of(seconds = Long.MAX_VALUE, nanos = 999__999_999) (which is effectively Duration.MAX_VALUE), such that our down-stack code need not consider the case where no timeout is specified.

If you guys are interested in this fix I’ll submit a pull request to that involves simply:

private fun Duration.toMillisOrMax(): Long = when(this.seconds) {
    in 0L .. (Long.MAX_VALUE / MILLIS_PER_SECOND) -> duration.toMillis()
    else -> Long.MAX_VALUE
}

as a function in TimeKt, invoked instead of Duration.toMillis()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
fvascocommented, Nov 30, 2018

I simple workaround is to avoid time check at all if the duration exceed a large number.

So if the duration is greater than a century (example) then the code is executed without any timeout (this avoid some system calls and related work).

0reactions
aventlycommented, Mar 24, 2019

Obviously. But I tried to migrate some java only code to common from ktor websockets (it has only java part now). One of the blockers was java.time package. That’s why I think about migration from platform-specific to universal code in every project

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Duration.toMillis() in Java? - Educative.io
toMillis () is an instance method of the Duration class which is used to convert the duration time length to milliseconds. If the...
Read more >
java.time.Duration.toMillis() Method Example - Tutorialspoint
Duration.toMillis() Method Example, The java.time.Duration.toMillis() method gets the number of milliseconds in this duration.
Read more >
Automatic Repair of Timestamp Comparisons - Martin Pinzger
the time domain of a program using a Time Type System to identify the ... toMillis(time); ... different types of time variables: Timestamp...
Read more >
(PDF) Automatic Repair of Timestamp Comparisons | Francesco ...
Thus, we toMillis(time); 3 ... aim to repair comparison expressions between ... Backus- In this manner, it can be used for multiple programming...
Read more >
$243,9411 $245,65l1 $268,3781 $270,8071 $01 $139 ... - Millis MA |
(depending on Marijuana. Impact funding) and conduct an evaluation of how patrons are using Library space during these times. *. Complete installation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found