File tree 2 files changed +28
-2
lines changed
kotlinx-coroutines-core/jvm
2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -91,12 +91,11 @@ private class BlockingCoroutine<T>(
91
91
eventLoop?.incrementUseCount()
92
92
try {
93
93
while (true ) {
94
- @Suppress(" DEPRECATION" )
95
- if (Thread .interrupted()) cancelCoroutine(InterruptedException ())
96
94
val parkNanos = eventLoop?.processNextEvent() ? : Long .MAX_VALUE
97
95
// note: process next even may loose unpark flag, so check if completed before parking
98
96
if (isCompleted) break
99
97
parkNanos(this , parkNanos)
98
+ if (Thread .interrupted()) cancelCoroutine(InterruptedException ())
100
99
}
101
100
} finally { // paranoia
102
101
eventLoop?.decrementUseCount()
Original file line number Diff line number Diff line change @@ -65,6 +65,23 @@ class RunBlockingJvmTest : TestBase() {
65
65
finish(5 )
66
66
}
67
67
68
+ /* * Tests that [runBlocking] does not check for interruptions before the first attempt to suspend,
69
+ * as no blocking actually happens. */
70
+ @Test
71
+ fun testInitialPortionRunningDespiteInterruptions () {
72
+ Thread .currentThread().interrupt()
73
+ runBlocking {
74
+ expect(1 )
75
+ try {
76
+ Thread .sleep(Long .MAX_VALUE )
77
+ } catch (_: InterruptedException ) {
78
+ expect(2 )
79
+ }
80
+ }
81
+ assertFalse(Thread .interrupted())
82
+ finish(3 )
83
+ }
84
+
68
85
/* *
69
86
* Tests that [runBlockingNonInterruptible] is going to run its job to completion even if it gets interrupted
70
87
* or if thread switches occur.
@@ -138,6 +155,16 @@ class RunBlockingJvmTest : TestBase() {
138
155
finish(3 )
139
156
}
140
157
158
+ /* *
159
+ * Tests that starting [runBlockingNonInterruptible] in an interrupted thread does not affect the result.
160
+ */
161
+ @Test
162
+ fun testNonInterruptibleRunBlockingStartingInterrupted () {
163
+ Thread .currentThread().interrupt()
164
+ val v = runBlockingNonInterruptible { 42 }
165
+ assertEquals(42 , v)
166
+ }
167
+
141
168
private fun startInSeparateThreadAndInterrupt (action : (mayInterrupt: () -> Unit ) -> Unit ) {
142
169
val latch = CountDownLatch (1 )
143
170
val thread = thread {
You can’t perform that action at this time.
0 commit comments