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 K/N EvenLoop.reschedule time conversion #4245

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import kotlinx.coroutines.testing.*
import kotlinx.coroutines.exceptions.*
import kotlin.coroutines.*
import kotlin.test.*
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

class RunBlockingTest : TestBase() {

Expand Down Expand Up @@ -176,4 +178,20 @@ class RunBlockingTest : TestBase() {
}
}
}

/** Tests that the delayed tasks scheduled on a closed `runBlocking` event loop get processed in reasonable time. */
@Test
fun testReschedulingDelayedTasks() {
val job = runBlocking {
val dispatcher = coroutineContext[ContinuationInterceptor]!!
GlobalScope.launch(dispatcher) {
delay(1.milliseconds)
}
}
runBlocking {
withTimeout(10.seconds) {
job.join()
}
}
}
}
3 changes: 2 additions & 1 deletion kotlinx-coroutines-core/native/src/EventLoop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ internal actual abstract class EventLoopImplPlatform : EventLoop() {
}

protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) {
DefaultExecutor.invokeOnTimeout(now, delayedTask, EmptyCoroutineContext)
val delayTimeMillis = delayNanosToMillis(delayedTask.nanoTime - now)
DefaultExecutor.invokeOnTimeout(delayTimeMillis, delayedTask, EmptyCoroutineContext)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure what is the correct way to test this fix.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can think of this simple test:

    /** Tests that the delayed tasks scheduled on a closed `runBlocking` event loop get processed in reasonable time. */
    @Test
    fun testReschedulingDelayedTasks() {
        val job = runBlocking {
            val dispatcher = coroutineContext[ContinuationInterceptor]!!
            GlobalScope.launch(dispatcher) {
                delay(1.milliseconds)
            }
        }
        runBlocking {
            withTimeout(10.seconds) {
                job.join()
            }
        }
    }

This would miss the error in the opposite direction (like immediately executing the tasks instead), but testing that is trickier, so maybe let's leave it at that?

A good file for this test could be kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the test. It failed before the fix and succeeds now.

}
}

Expand Down