question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird fairness/scheduling issue

See original GitHub issue

Still not sure what’s going on here. I’ve been poking at this for a little while:

object InefficientProducerConsumer extends IOApp {

  def producer[F[_]: Spawn: Console](queueR: Ref[F, Queue[Int]], counter: Int): F[Unit] =
    for {
      _ <- if (counter % 10000 == 0)
        Console[F].println(s"Produced $counter items")
      else
        Applicative[F].unit

      _ <- queueR.getAndUpdate(_.enqueue(counter + 1))
      _ <- producer(queueR, counter + 1)
    } yield ()

  def consumer[F[_]: Monad: Console](queueR: Ref[F, Queue[Int]]): F[Unit] =
    for {
      _ <- Console[F].println("attempting to consume")
      iO <- queueR modify { queue =>
        queue.dequeueOption.fold((queue, Option.empty[Int])) {
          case (i, queue) => (queue, Option(i))
        }
      }

      _ <- if (iO.exists(_ % 10000 == 0))
        Console[F].println(s"Consumed ${iO.get} items")
      else
        Applicative[F].unit

      _ <- consumer(queueR)
    } yield ()

  override def run(args: List[String]): IO[ExitCode] =
    for {
      queueR <- Ref.of[IO, Queue[Int]](Queue.empty[Int])
      cf <- consumer(queueR).start
      pr <- producer(queueR, 0).start

      cfR <- cf.join
      _ <- IO.println(s"!!! found cfR = $cfR")

      prR <- pr.join
      _ <- IO.println(s"!!! found prR = $prR")
    } yield ExitCode.Success
}

If you run this, you’ll see a ton of attempting to consume, followed by infinite Produced printlns, which honestly doesn’t make any sense. It’s like the first fiber hogs the thread for a while, then the second fiber takes over and never lets it go.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
djspiewakcommented, May 25, 2021

Come on Daniel, say what you really mean, no reason to hide it! 😄😆🥳

lol Our runtime is legitimately so fast that it breaks examples like this. So for anyone following along at home, this wasn’t a problem before because the runtime used to be slow enough that the overhead dominated the race on both sides and things were somewhat fair. Now the Queue operations dominate by a significant margin, meaning that it’s just a pure race between two different Queue operations thrown on a naive scheduler that doesn’t support fairness (AtomicReference).

Or, if you want a tldr: our runtime is too fast for this

0reactions
vasilmkdcommented, May 24, 2021

Come on Daniel, say what you really mean, no reason to hide it! 😄😆🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fairness in Scheduling - Computer Science
In this paper we explore the issue in settings where there are long living processes which should be repeatedly scheduled for various tasks...
Read more >
Fairness in employee scheduling - Christine Chung
The scheduling problem most similar to our our own is probably the nurse rostering problem, as it is concerned with fairness to employees...
Read more >
Scheduling: Proportional Share - cs.wisc.edu
In this chapter, we'll examine a different type of scheduler known as a proportional-share scheduler, also sometimes referred to as a fair-share scheduler....
Read more >
example for weakly fair v.s. strongly fair scheduling in ...
Weakly fair scheduling means that only one process would run because the condition gets changed before the second process is able to run....
Read more >
Irregular Work Scheduling and Its Consequences
This report will inform recently proposed reforms of the Fair ... the incidence of irregular, unusual work schedules (these results are not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found