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

Cancelation hangs in Dispatcher#unsafeToFutureCancelable #3898

Closed
armanbilge opened this issue Nov 17, 2023 · 2 comments
Closed

Cancelation hangs in Dispatcher#unsafeToFutureCancelable #3898

armanbilge opened this issue Nov 17, 2023 · 2 comments
Labels

Comments

@armanbilge
Copy link
Member

h/t @TimWSpence for original minimization.

//> using scala "3.3.1"
//> using dep "org.typelevel::cats-effect::3.5.2"

import cats.*
import cats.syntax.all.*
import cats.effect.*
import cats.effect.std.Dispatcher
import scala.concurrent.duration.*

object Repro extends IOApp.Simple:

  // also sequential
  val run = Dispatcher.parallel[IO].use { dispatcher =>
    IO.fromFuture {
      IO {
        println("starting")
        val (_, cancel) = dispatcher.unsafeToFutureCancelable(IO.never) // broken
        // val (_, cancel) = IO.never.unsafeToFutureCancelable()(runtime) // works
        println("started, now canceling")
        cancel()
      }
    }.flatMap(_ => IO(println("canceled"))).replicateA_(1000)
  }
durban added a commit to durban/cats-effect that referenced this issue Nov 20, 2023
@durban
Copy link
Contributor

durban commented Nov 20, 2023

There are (at least) two different problems here. #3900 seems to fix the problem for Dispatcher.parallel.

For Dispatcher.sequential, the problem seems to be that it doesn't actually fork the submitted action, so it can't really cancel it (https://github.com/typelevel/cats-effect/blob/series/3.x/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala#L214). I'm not sure what to do with that.

@armanbilge
Copy link
Member Author

If I remember correctly, a sequential dispatcher involves at least two fibers:

  1. a worker fiber, that actually runs tasks
  2. a supervisor fiber, that restarts the worker if the task it is running (self-)cancels

So even though the task is not actually forked, it should be possible to cancel it by simply canceling the worker fiber.

djspiewak added a commit that referenced this issue Nov 23, 2023
Fix #3898 for `Dispatcher.parallel`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants