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

I/O polling hangs when computeWorkerThreadCount = 1 #4225

Closed
armanbilge opened this issue Jan 5, 2025 · 2 comments · Fixed by #4227
Closed

I/O polling hangs when computeWorkerThreadCount = 1 #4225

armanbilge opened this issue Jan 5, 2025 · 2 comments · Fixed by #4227
Labels
Milestone

Comments

@armanbilge
Copy link
Member

First discovered this when debugging Native multithreading. It's also affecting CE 3.6.0-RC1 on the JVM, so it must be a WSTP / polling system integration bug.

//> using dep org.typelevel::cats-effect::3.6.0-RC1

import cats.effect.*
import java.nio.ByteBuffer
import java.nio.channels.Pipe
import java.nio.channels.SelectionKey._
import scala.concurrent.duration._

object Bug extends IOApp.Simple {
  override def computeWorkerThreadCount = 1 // changing to 2 fixes

  def run = mkPipe.use { pipe =>
    for {
      selector <- getSelector
      buf <- IO(ByteBuffer.allocate(4))
      _ <- IO(pipe.sink.write(ByteBuffer.wrap(Array(1, 2, 3)))).background
        .surround {
          selector.select(pipe.source, OP_READ) *> IO(pipe.source.read(buf))
        }
      _ <- IO(pipe.sink.write(ByteBuffer.wrap(Array(42)))).background.surround {
        selector.select(pipe.source, OP_READ) *> IO(pipe.source.read(buf))
      }
    } yield assert(buf.array().toList == List[Byte](1, 2, 3, 42))
  }.timeout(1.second)

  def getSelector: IO[Selector] =
    IO.pollers
      .map(_.collectFirst { case selector: Selector => selector })
      .map(_.get)

  def mkPipe: Resource[IO, Pipe] =
    Resource
      .eval(getSelector)
      .flatMap { selector =>
        Resource.make(IO(selector.provider.openPipe())) { pipe =>
          IO(pipe.sink().close()).guarantee(IO(pipe.source().close()))
        }
      }
      .evalTap { pipe =>
        IO {
          pipe.sink().configureBlocking(false)
          pipe.source().configureBlocking(false)
        }
      }
}
@armanbilge armanbilge added this to the v3.6.0 milestone Jan 5, 2025
@armanbilge
Copy link
Member Author

Hm, I guess it doesn't hang completely. It's just suspiciously slow 🤔

@djspiewak
Copy link
Member

I wonder if this is illustrating an underlying issue that also occurs at higher worker counts

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

Successfully merging a pull request may close this issue.

2 participants