typelevel / cats-effect

The pure asynchronous runtime for Scala
https://typelevel.org/cats-effect/
Apache License 2.0
2k stars 513 forks source link

`Dispatcher#unsafeRunTimed` should also `cancel()` if `Await.result` is `interrupt`ed #3967

Open armanbilge opened 6 months ago

armanbilge commented 6 months ago

i.e. if it throws an InterruptedException.

https://github.com/typelevel/cats-effect/blob/97b0174090d9c3837055695f504cd3d62cba69ff/std/jvm/src/main/scala/cats/effect/std/DispatcherPlatform.scala#L58-L66

djspiewak commented 6 months ago

This is a good idea. We should probably also do it in IO. unsafeRunTimed is a bit bizarre because it's the only flavor of cancelation which doesn't backpressure and instead just yolo-disconnects from the computation. We need to keep that semantic (because it is designed to do precisely that), but this doesn't mean we shouldn't at least try to queue up the cancelation.

armanbilge commented 6 months ago

We should probably also do it in IO

I thought so, but it would be changing the explicitly documented API.

https://github.com/typelevel/cats-effect/blob/328f61d15b429e5f87c1d35a5ab4c72b00ecd583/core/jvm/src/main/scala/cats/effect/IOPlatform.scala#L47-L48

For the record, I can't think why we wouldn't want to improve it 😅

djspiewak commented 5 months ago

Yeah I'm in favor of improving both.

kotoji commented 5 months ago

@armanbilge @djspiewak I'm a beginner, and I think it might take me some time, but I'd like to give it a try. If it's okay with you, could you please assign the issue to me?

djspiewak commented 5 months ago

It's all yours! Do you feel like you have a good idea on how to start? Feel free to ask questions here or in Discord (in the cats-effect-dev channel) and we'll be happy to help.

kotoji commented 5 months ago

@djspiewak Thank you very much. I'm planning to start by doing some research to understand the content of the issue better. I might ask some basic questions, but I appreciate your support.

kotoji commented 4 months ago

@djspiewak I would like to confirm my understanding of the approach for this issue.

The main content of this issue, as I understand it, is that we want to perform a cancel() operation when an InterruptedException is issued in the following places:

  1. DispatcherPlatform#unsafeRunTimed
  2. IOPlatform#unsafeRunTimed

Next, I will express my thoughts on how we should address this in both (1) and (2).

For (1), my thought is that we should handle an InterruptedException in the same way we do for a TimeoutException, by performing a cancel().

For (2), based on my interpretation of the following comment, https://github.com/typelevel/cats-effect/issues/3967#issuecomment-1904419420

I thought the approach might be something like this:

I'm not sure how to proceed with this, but I would like to confirm if my understanding is correct.

Or perhaps, even in the case of a timeout, is it acceptable to perform a cancel() instead of just being yolo-disconnected? In this case, I believe using unsafeToFutureCancelable() like in DispatcherPlatform#unsafeRunTimed seems to be a viable approach.