Skip to content

Commit d644d56

Browse files
committed
Fix Executor.asDispatcher() sometimes forgetting to cancel futures
1 parent 6116009 commit d644d56

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

kotlinx-coroutines-core/jvm/src/Executors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandle
205205
override fun invoke(cause: Throwable?) {
206206
// Don't interrupt when cancelling future on completion, because no one is going to reset this
207207
// interruption flag and it will cause spurious failures elsewhere
208-
if (cause != null) future.cancel(false)
208+
future.cancel(false)
209209
}
210210
override fun toString() = "CancelFutureOnCancel[$future]"
211211
}

kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,21 @@ class ExecutorAsCoroutineDispatcherDelayTest : TestBase() {
3838
executor.shutdown()
3939
assertEquals(1, callsToSchedule)
4040
}
41+
42+
@Test
43+
fun testCancelling() = runTest {
44+
val executor = STPE()
45+
launch(start = CoroutineStart.UNDISPATCHED) {
46+
suspendCancellableCoroutine<Unit> { cont ->
47+
expect(1)
48+
(executor.asCoroutineDispatcher() as Delay).scheduleResumeAfterDelay(1_000_000, cont)
49+
cont.cancel()
50+
expect(2)
51+
}
52+
}
53+
expect(3)
54+
assertTrue(executor.getQueue().isEmpty())
55+
executor.shutdown()
56+
finish(4)
57+
}
4158
}

0 commit comments

Comments
 (0)