Skip to content

Commit fda2bf9

Browse files
committed
Treat dispatchYield as regular dispatch in CoroutineScheduler
* Always unpark a worker to avoid potential starvation in cases when coroutine was launched via UNDISPATCHED mechanism Fixes #4248
1 parent 68da30d commit fda2bf9

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ internal class CoroutineScheduler(
385385
*
386386
* [taskContext] -- concurrency context of given [block].
387387
* [fair] -- whether this [dispatch] call is fair.
388-
* If `true` then the task will be dispatched in a FIFO manner and no additional workers will be requested,
389-
* but only if the current thread is a corresponding worker thread.
388+
* If `true` then the task will be dispatched in a FIFO manner.
390389
* Note that caller cannot be ensured that it is being executed on worker thread for the following reasons:
391390
* - [CoroutineStart.UNDISPATCHED]
392391
* - Concurrent [close] that effectively shutdowns the worker thread.
@@ -408,13 +407,11 @@ internal class CoroutineScheduler(
408407
throw RejectedExecutionException("$schedulerName was terminated")
409408
}
410409
}
411-
val skipUnpark = fair && currentWorker != null
412410
// Checking 'task' instead of 'notAdded' is completely okay
413411
if (isBlockingTask) {
414412
// Use state snapshot to better estimate the number of running threads
415-
signalBlockingWork(stateSnapshot, skipUnpark = skipUnpark)
413+
signalBlockingWork(stateSnapshot)
416414
} else {
417-
if (skipUnpark) return
418415
signalCpuWork()
419416
}
420417
}
@@ -430,8 +427,7 @@ internal class CoroutineScheduler(
430427
}
431428

432429
// NB: should only be called from 'dispatch' method due to blocking tasks increment
433-
private fun signalBlockingWork(stateSnapshot: Long, skipUnpark: Boolean) {
434-
if (skipUnpark) return
430+
private fun signalBlockingWork(stateSnapshot: Long) {
435431
if (tryUnpark()) return
436432
// Use state snapshot to avoid accidental thread overprovision
437433
if (tryCreateWorker(stateSnapshot)) return

0 commit comments

Comments
 (0)