Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cats.effect.Console with timeout?

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?

like image 580
Max Smirnov Avatar asked Oct 27 '25 02:10

Max Smirnov


1 Answers

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:

  • https://github.com/typelevel/cats-effect/issues/1793
  • https://github.com/typelevel/cats-effect/issues/3077
like image 191
nimble agar Avatar answered Oct 29 '25 16:10

nimble agar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!