fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Add Async.withCancellation

Open TheAngryByrd opened this issue 7 years ago • 9 comments

Add Async.withCancellation

I propose we add Async.withCancellation. This would allow using outside CancellationToken, such as the RequestAborted from HttpContext. The problem with Async.Start is it does not provide a way to return a value.

The existing way of approaching this problem in F# is provided here:

https://gist.github.com/eulerfx/c41d50c6fba8e88cf16a21ed7c3c14bd#file-async-withcancellation-fs

Inlined:

let withCancellation (ct:CancellationToken) (a:Async<'a>) : Async<'a> = async {
  let! ct2 = Async.CancellationToken
  use cts = CancellationTokenSource.CreateLinkedTokenSource (ct, ct2)
  let tcs = new TaskCompletionSource<'a>()
  use _reg = cts.Token.Register (fun () -> tcs.TrySetCanceled() |> ignore)
  let a = async {
    try
      let! a = a
      tcs.TrySetResult a |> ignore
    with ex ->
      tcs.TrySetException ex |> ignore }
  Async.Start (a, cts.Token)
  return! tcs.Task |> Async.AwaitTask }

Pros and Cons

The advantages of making this adjustment to F# are

  • This would be easily discoverable to more user
  • Potentially implemented in a cleaner way

The disadvantages of making this adjustment to F# are

  • None I know of

Extra information

Estimated cost (XS, S, M, L, XL, XXL): XS

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • [x] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • [x] This is not a breaking change to the F# language design
  • [x] I or my company would be willing to help implement and/or test this

TheAngryByrd avatar Aug 17 '18 18:08 TheAngryByrd

I like this, though the name would have to be PascalCase since it would likely be a static member on the Async type as per the current implementation. An unrelated issue might be moving those into a module, but they would also have to be similarly cased, otherwise we'd break everyone on an upgrade.

Would you like to write an RFC for this?

cartermp avatar Sep 10 '18 16:09 cartermp

Sure! I'll get to this tonight.

TheAngryByrd avatar Sep 10 '18 19:09 TheAngryByrd

@cartermp is it ok if this still isn't marked as approved in principal?

TheAngryByrd avatar Sep 10 '18 23:09 TheAngryByrd

This is ultimately @dsyme's decision, but I'd certainly welcome the addition. If the RFC isn't much work to write, then it probably wouldn't hurt to send the PR.

cartermp avatar Sep 10 '18 23:09 cartermp

Ok sending in a few

TheAngryByrd avatar Sep 10 '18 23:09 TheAngryByrd

There is a cancellationToken parameter in Async.Start already.

module Async =
  /// Async.Start with timeout in seconds
  let StartWithTimeout (timeoutSecs:int) (computation:Async<unit>) =
    let c = new System.Threading.CancellationTokenSource(timeoutSecs*1000)
    Async.Start(computation, cancellationToken = c.Token)

Also, should withCancellation actually start the async (task) or not?

Thorium avatar Jan 19 '19 19:01 Thorium

It would be great to get this added to FSharp.Core. I just spent way too long looking through docs for something like this thinking I must be missing something.

Another problem with Async.Start is that it runs in the threadpool. Is it possible to implement WithCancellation without switching to the threadpool? Would it be sufficient to just change the imlementation to use Async.StartImmediate?

olivercoad avatar Aug 16 '21 16:08 olivercoad

Another problem with Async.Start is that it runs in the threadpool. Is it possible to implement WithCancellation without switching to the threadpool? Would it be sufficient to just change the imlementation to use Async.StartImmediate?

Yes, it should use StartImmediate for sure

dsyme avatar Aug 16 '21 17:08 dsyme

It would be great to have this in F#, it took me quite some time to get request cancellation with Giraffe working.

ajuch avatar Jun 03 '25 07:06 ajuch