Skip to content

withTimeoutOrNull throw TimeoutCancellationException #498

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

Closed
kobe2000 opened this issue Aug 16, 2018 · 3 comments
Closed

withTimeoutOrNull throw TimeoutCancellationException #498

kobe2000 opened this issue Aug 16, 2018 · 3 comments
Labels

Comments

@kobe2000
Copy link

Hi,
I understand that withTimeoutOrNull never throw timeout exception, does it?

I will try to reproduce it.

The code sample is

val ch = cudChannel.openSubscription()
try {
    var lastPingTime = System.currentTimeMillis()
    while (true) {
        val cud = withTimeoutOrNull(1000) {
            ch.receive()
        }
        if (cud != null) {
            wc.writeString(cud.first)
            wc.writeBoolean(cud.second)
        } else if (System.currentTimeMillis() - lastPingTime > 6000) {
            wc.writeString("")
            wc.writeBoolean(false)
        } else {
            continue
        }
        lastPingTime = System.currentTimeMillis()
        withTimeout(30000) {
            rc.readInt()
        }
    }
} finally {
    ch.cancel()
}

The stacktrace is

kotlinx.coroutines.experimental.TimeoutCancellationException: Timed out waiting for 1000 MILLISECONDS
        at kotlinx.coroutines.experimental.ScheduledKt.TimeoutCancellationException(Scheduled.kt:202)
        at kotlinx.coroutines.experimental.TimeoutCoroutine.run(Scheduled.kt:100)
        at kotlinx.coroutines.experimental.EventLoopBase$DelayedRunnableTask.run(EventLoop.kt:322)
        at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:148)
        at kotlinx.coroutines.experimental.DefaultExecutor.run(DefaultExecutor.kt:60)
        at java.lang.Thread.run(Thread.java:745)
@qwwdfsad
Copy link
Collaborator

Hi, could you please a self-contained example?
What types are rc and wc?

@kobe2000
Copy link
Author

kobe2000 commented Aug 16, 2018

Hi,
rc wc are socket read channel and write channel. the cudChannel is a BroadcastChannel.
This is the first time I meet the problem, I worry that the self-contained example maybe just a single piece of withTimeoutOrNull API invocation code

@qwwdfsad
Copy link
Collaborator

 val channel = Channel<Int>(1)
        val value = withTimeoutOrNull(timeout) {
            channel.receive()
        }

        println(value)

prints null for timeout = 100 and throws exception for timeout = 1

qwwdfsad added a commit that referenced this issue Aug 16, 2018
qwwdfsad added a commit that referenced this issue Aug 21, 2018
#499)

* Implement withTimeoutOrNull via withTimeout to avoid timing bugs and races.
Remove deprecated API

Fixes #498
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