Home > front end >  How to make cats.effect.Console with timeout?
How to make cats.effect.Console with timeout?

Time:12-25

I want to take input from console in 3 seconds, otherwise in timeout case return "Timeout!". I wrote this function:

def withTimeout: IO[String] =
  Console[IO].readLine.timeoutTo(3.seconds, IO.pure("Timeout!"))

But it doesn't stop after 3 seconds, but waits any input, and only after input returns "Timeout!". Is this happens because Console is not cancellable? How can I solve this issue?

CodePudding user response:

This is a known "cantfix" issue. The underlying problem is that reading from stdin on the JVM is not interruptible and we can't really do anything about that.

My recommendation is to use fs2.io.stdinUtf8 instead.

Further reading:

  • Related