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

ZPool hangs during parallel item acquisition after invalidation #8820

Closed
daemas opened this issue May 6, 2024 · 5 comments · Fixed by #8833
Closed

ZPool hangs during parallel item acquisition after invalidation #8820

daemas opened this issue May 6, 2024 · 5 comments · Fixed by #8833

Comments

@daemas
Copy link
Contributor

daemas commented May 6, 2024

The following program hangs. Which, I guess, is not supposed to happen.

package test

import zio.{Scope, ZIO, ZIOAppArgs, ZIOAppDefault, ZPool}

import java.time.Duration

object TestPool extends ZIOAppDefault {

  private val program: ZIO[Any, Throwable, Unit] = ZIO.scoped {
    for {
      pool <- ZPool.make(
        ZIO.succeed("item").tap(r => ZIO.logInfo(s"Made $r")),
        0 to 1,
        Duration.ofSeconds(10)
      )
      getZIO = ZIO.scoped {
        for {
          _ <- ZIO.logInfo(s"Acquiring...")
          v <- pool.get
          _ <- ZIO.logInfo(s"Got: $v")
          _ <- pool.invalidate(v)
          _ <- ZIO.logInfo(s"Invalidated")
        } yield v
      }

      res <- ZIO.foreachPar(Seq.fill(2)(0)) { _ => getZIO.tap(r => ZIO.logInfo(s"Received: $r")) }
    } yield ()
  }

  override def run: ZIO[ZIOAppArgs & Scope, Any, Any] = program
}

The log is the following:

thread=#zio-fiber-8 message="Acquiring..." location=test.TestPool.program.getZIO file=TestPool.scala line=18
thread=#zio-fiber-7 message="Acquiring..." location=test.TestPool.program.getZIO file=TestPool.scala line=18
thread=#zio-fiber-9 message="Made item" location=test.TestPool.program file=TestPool.scala line=12
thread=#zio-fiber-7 message="Got: item" location=test.TestPool.program.getZIO file=TestPool.scala line=20
thread=#zio-fiber-7 message="Invalidated" location=test.TestPool.program.getZIO file=TestPool.scala line=22
thread=#zio-fiber-7 message="Received: item" location=test.TestPool.program file=TestPool.scala line=26

(program hangs here)

If I change ZIO.foreachPar to just ZIO.foreach, everything works as expected:

thread=#zio-fiber-4 message="Acquiring..." location=test.TestPool.program.getZIO file=TestPool.scala line=18
thread=#zio-fiber-7 message="Made item" location=test.TestPool.program file=TestPool.scala line=12
thread=#zio-fiber-4 message="Got: item" location=test.TestPool.program.getZIO file=TestPool.scala line=20
thread=#zio-fiber-4 message="Invalidated" location=test.TestPool.program.getZIO file=TestPool.scala line=22
thread=#zio-fiber-4 message="Received: item" location=test.TestPool.program file=TestPool.scala line=26
thread=#zio-fiber-4 message="Acquiring..." location=test.TestPool.program.getZIO file=TestPool.scala line=18
thread=#zio-fiber-8 message="Made item" location=test.TestPool.program file=TestPool.scala line=12
thread=#zio-fiber-4 message="Got: item" location=test.TestPool.program.getZIO file=TestPool.scala line=20
thread=#zio-fiber-4 message="Invalidated" location=test.TestPool.program.getZIO file=TestPool.scala line=22
thread=#zio-fiber-4 message="Received: item" location=test.TestPool.program file=TestPool.scala line=26

Process finished with exit code 0

I use scala 3.4.1 and sbt 1.9.7.
I tried ZIO 2.0.22 and ZIO 2.1.0-RC5, with the same outcome.

@jdegoes
Copy link
Member

jdegoes commented May 8, 2024

/bounty $100

Copy link

algora-pbc bot commented May 8, 2024

💎 $100 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #8820 with your implementation plan
  2. Submit work: Create a pull request including /claim #8820 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @daemas May 9, 2024, 4:29:22 PM #8833

@daemas
Copy link
Contributor Author

daemas commented May 9, 2024

/attempt #8820

Copy link

algora-pbc bot commented May 9, 2024

💡 @daemas submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented May 21, 2024

@daemas: You've been awarded a $100 bounty by ZIO! 👉 Complete your Algora onboarding to collect the bounty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants