Skip to content

Commit 0e1e607

Browse files
committed
Keep consistent state of CoroutineScheduler if thread constructor throws an exception
Fixes Kotlin#748
1 parent 15407e3 commit 0e1e607

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,11 @@ internal class CoroutineScheduler(
460460
// Double check for overprovision
461461
if (cpuWorkers >= corePoolSize) return 0
462462
if (created >= maxPoolSize || cpuPermits.availablePermits() == 0) return 0
463-
// start & register new worker
464-
val newIndex = incrementCreatedWorkers()
463+
// start & register new worker, commit index only after successful creation
464+
val newIndex = createdWorkers + 1
465465
require(newIndex > 0 && workers[newIndex] == null)
466466
val worker = Worker(newIndex).apply { start() }
467+
require(newIndex == incrementCreatedWorkers())
467468
workers[newIndex] = worker
468469
return cpuWorkers + 1
469470
}

0 commit comments

Comments
 (0)