Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dequeue#size drops below 0 on concurrent takeFront, offerBack, size #4170

Closed
kamilkloch opened this issue Nov 17, 2024 · 2 comments
Closed

Comments

@kamilkloch
Copy link
Contributor

kamilkloch commented Nov 17, 2024

import cats.effect.std.Dequeue
import cats.effect.{IO, IOApp}

import scala.concurrent.duration.DurationInt

object DequeueTests extends IOApp.Simple {

  def run: IO[Unit] = {
    Dequeue.bounded[IO, Int](1024).flatMap { q =>
      val supplier = q.offerBack(1).debug("offerBack").delayBy(200.millis)
      val consumer = q.takeFront.debug("takeFront")
      val checkSize = q.size.debug("size").flatMap(n => if (n < 0) IO.raiseError(new Exception(s"q.size = $n")) else IO.unit)

      consumer.background.surround(supplier >> checkSize)
    }
  }
}

Output:

offerBack: Succeeded: ()
takeFront: Succeeded: 1
size: Succeeded: -1
java.lang.Exception: q.size = -1

(Run on CE 3.6-ecf93db)

@kamilkloch kamilkloch changed the title Dequeue#size drops below 0 on concurent takeFront, offerBack, size Dequeue#size drops below 0 on concurrent takeFront, offerBack, size Nov 17, 2024
@reardonj
Copy link
Contributor

I am very suspicious of this line:

State(update(queue), size, rest, offerers) -> taker.complete(()).void

if I understand correctly, when there is a pending taker when an offer happens, the state does not increment the size. However, when _take ultimately gets called, it does decrement the size in the state. So when offer is called when a taker is pending, size gets decremented by 1 once both effects have finished executing, instead of getting incremented then decremented.

armanbilge added a commit to armanbilge/cats-effect that referenced this issue Nov 18, 2024
@durban
Copy link
Contributor

durban commented Nov 18, 2024

Was fixed by #4171.

@durban durban closed this as completed Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants