typelevel / typelevel/cats-effect
Batches of fibers may be scheduled unfairly in compute-heavy applications
Open
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 2.2k
- Forks
- 576
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 18
Description
This is the result of the following experiment:
- start
nfibers - each fiber has an
id - the fiber loops, infinitely incrementing an ID-bound value in a
Ref[IO, Map[String, Int]], where theStringis the fiber'sid, then yields withIO.cede.
The result is that the fibers that get started first get to do most of the incrementing (n = 20):

import cats.effect.{IO, IOApp, Ref}
import cats.implicits._
import scala.concurrent.duration._
object FairnessExperiment extends IOApp.Simple {
override def run: IO[Unit] = for {
ref <- Ref[IO].of(Map.empty[String, Int])
_ <- (1 to 20).map(i => loop(s"Fiber $i", ref)(0).start).toList.parSequence
_ <- (IO.sleep(1.second) >> ref.get >>= printCountsAsCsv).foreverM
} yield ()
def loop(id: String, ref: Ref[IO, Map[String, Int]])(i: Int): IO[Unit] = for {
_ <- ref.getAndUpdate(s => s.updatedWith(id)(v => Some(v.getOrElse(0) + 1)))
_ <- IO.cede
result <- loop(id, ref)(i + 1)
} yield result
private def printCountsAsCsv(counts: Map[String, Int]) = IO.delay {
val columns = counts.toList.sortBy(-_._2)
println(columns.map(_._1).mkString(","))
println(columns.map(_._2).mkString(","))
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the FairnessExperiment entry point in the issue and run the 20-fiber experiment to reproduce the unequal counts. Then trace the scheduler behavior around IO.cede and Ref updates; done means the experiment no longer gives a persistent advantage to fibers started first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100