August Feng

Asynchronous but not yielding

About

I was experimenting some server/client architecture on the same program. The program was writen with task {}, yet the client never had execution time.

Reproducer

I reproduced the behavior and found out that although the tasks are hot, there needs to be a yield before the runtime will execute the next line.

In the program below, the execution does not get past the first busyWait.

open System.Threading.Tasks

let busyWait filename = task {
    while System.IO.File.Exists(filename) |> not do
        // do! Task.Yield() // XXX: this will yield control back to the main function
        ()
    printfn $"{filename} exists!"
}

[<EntryPoint>]
let main _ =
    let a = busyWait "a.txt"
    printfn "helloworld"
    let b = busyWait "b.txt"
    Task.WaitAll(a,b)
    0

My server/client architecture has a bunch of asynchronous operations that should've yielded.

I'll do a bit more investigation, but I'll be attacking with a better understanding now. :)