Skip to content

Commit 1fcffbf

Browse files
author
Abduqodiri Qurbonzoda
authored
Fix K/N EvenLoop.reschedule time conversion (Kotlin#4245)
A "point of time" value was wrongly passed to a function that expects a "duration of time". Additionally, the former was in nanos, while the latter is in milllis.
1 parent 9749b8c commit 1fcffbf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.exceptions.*
55
import kotlin.coroutines.*
66
import kotlin.test.*
7+
import kotlin.time.Duration.Companion.milliseconds
8+
import kotlin.time.Duration.Companion.seconds
79

810
class RunBlockingTest : TestBase() {
911

@@ -176,4 +178,20 @@ class RunBlockingTest : TestBase() {
176178
}
177179
}
178180
}
181+
182+
/** Tests that the delayed tasks scheduled on a closed `runBlocking` event loop get processed in reasonable time. */
183+
@Test
184+
fun testReschedulingDelayedTasks() {
185+
val job = runBlocking {
186+
val dispatcher = coroutineContext[ContinuationInterceptor]!!
187+
GlobalScope.launch(dispatcher) {
188+
delay(1.milliseconds)
189+
}
190+
}
191+
runBlocking {
192+
withTimeout(10.seconds) {
193+
job.join()
194+
}
195+
}
196+
}
179197
}

kotlinx-coroutines-core/native/src/EventLoop.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ internal actual abstract class EventLoopImplPlatform : EventLoop() {
1515
}
1616

1717
protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) {
18-
DefaultExecutor.invokeOnTimeout(now, delayedTask, EmptyCoroutineContext)
18+
val delayTimeMillis = delayNanosToMillis(delayedTask.nanoTime - now)
19+
DefaultExecutor.invokeOnTimeout(delayTimeMillis, delayedTask, EmptyCoroutineContext)
1920
}
2021
}
2122

0 commit comments

Comments
 (0)