Race Condition With Coroutines

package com.glassthought.sandbox

import kotlinx.coroutines.*
import kotlin.random.Random

class RaceConditionInducer {
  private var value = 0

  // Suspending function to simulate some work and induce a race condition
  suspend fun incrementValue() {
    val storedValue = value

    // Simulate delay to increase the likelihood of race conditions
    delay(10)

    println("Incrementing value from storedValue=$storedValue to ${storedValue + 1}")
    value = storedValue + 1
  }

  fun getValue() = value
}

fun main() = runBlocking {
  val raceConditionInducer = RaceConditionInducer()

  // Launch multiple coroutines to increment the value
  val jobs = List(10) {
    GlobalScope.launch {
      raceConditionInducer.incrementValue()
    }
  }

  // Wait for all coroutines to complete
  jobs.forEach { it.join() }

  println("Final Value: ${raceConditionInducer.getValue()}")
}

Command to reproduce:

gt.sandbox.checkout.commit a82a70441214360361c9 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run"

Recorded output of command:

> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:compileKotlin UP-TO-DATE
> Task :app:compileJava NO-SOURCE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE

> Task :app:run
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Incrementing value from storedValue=0 to 1
Final Value: 1

BUILD SUCCESSFUL in 704ms
2 actionable tasks: 1 executed, 1 up-to-date

Backlinks