Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:04:29+00:00 2026-06-17T15:04:29+00:00

When calling Async.RunSynchronously with a timeout and a CancellationToken, the timeout value seems to

  • 0

When calling Async.RunSynchronously with a timeout and a CancellationToken, the timeout value seems to be ignored. I can work around this by calling CancelAfter on the CancellationToken, but ideally I’d like to be able to distinguish between exceptions that occur in the workflow, TimeOutExceptions and OperationCanceledExceptions.

I believe the sample code below demonstrates this.

open System
open System.Threading

let work = 
    async {
        let endTime = DateTime.UtcNow.AddMilliseconds(100.0)
        while DateTime.UtcNow < endTime do
            do! Async.Sleep(10)
            Console.WriteLine "working..."
        raise ( Exception "worked for more than 100 millis" )
    }


[<EntryPoint>]
let main argv = 
    try
        Async.RunSynchronously(work, 50)
    with
        | e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)

    let cts = new CancellationTokenSource()

    try
        Async.RunSynchronously(work, 50, cts.Token)
    with
        | e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)  


    cts.CancelAfter(80)
    try
        Async.RunSynchronously(work, 50, cts.Token)
    with
        | e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)  

    Console.ReadKey(true) |> ignore

    0

The outputs the following, showing that the timeout is only effective in the first case (where no CancelationToken is specified)

working...
working...
TimeoutException: The operation has timed out.
working...
working...
working...
working...
working...
working...
working...
Exception: worked for more than 100 millis
working...
working...
working...
working...
working...
working...
OperationCanceledException: The operation was canceled.

Is this the intended behaviour? Is there any way get the behaviour I’m after?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:04:30+00:00Added an answer on June 17, 2026 at 3:04 pm

    I’m not sure if this is intended behaviour – at least, I do not see any reason why it would be. However, this behaviour is implemented directly in the handling of parameters of RunSynchronously. If you look at the library source code, you can see:

    static member RunSynchronously (p:Async<'T>,?timeout,?cancellationToken) =
      let timeout,token =
        match cancellationToken with
        | None -> timeout,(!defaultCancellationTokenSource).Token                
        | Some token when not token.CanBeCanceled -> timeout, token                
        | Some token -> None, token
    

    In your case (with both timeout and a cancellation token that can be cancelled), the code goes through the last branch and ignores the timeout. I think this is either a bug or it is something that should be mentioned in the documentation.

    As a workaround, you can create a separate CancellationTokenSource to specify the timeout and link it to the main cancellation source so that the caller provides (using CreateLinkedTokenSource). When you get OperationCancelledException, you can then detect whether the source was an actual cancellation or a timeout:

    type Microsoft.FSharp.Control.Async with
      static member RunSynchronouslyEx(a:Async<'T>, timeout:int, cancellationToken) =
        // Create cancellation token that is cancelled after 'timeout'
        let timeoutCts = new CancellationTokenSource()
        timeoutCts.CancelAfter(timeout)
    
        // Create a combined token that is cancelled either when 
        // 'cancellationToken' is cancelled, or after a timeout
        let combinedCts = 
          CancellationTokenSource.CreateLinkedTokenSource
            (cancellationToken, timeoutCts.Token)
    
        // Run synchronously with the combined token
        try Async.RunSynchronously(a, cancellationToken = combinedCts.Token)
        with :? OperationCanceledException as e ->
          // If the timeout occurred, then we throw timeout exception instead
          if timeoutCts.IsCancellationRequested then
            raise (new System.TimeoutException())
          else reraise()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling an asmx service repeatedly using the Async methods and am getting this
I am calling this zip_threading class in another class. string a = zip_threading(?,?) but
Calling image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) will maintain the aspect ratio. But I need
Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
Calling getActionBar returns null . This has been frequently reported so I've made sure
Is it possible to set async: false when calling $.getJSON() so that the call
I have an asynchronous method like this public async void Method() { await //
I've used async methods today, calling methods asynchronously and using the caller's callback methods.
Iam calling the Ajax function function validateemp(){ var exists=; $.ajax({ url: emp.php, async: false,
I need to get some stuff sorted out about all this async stuff. Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.