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

Bind of Async<> within task{} should start on the same thread #14499

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/FSharp.Core/tasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ module MediumPriority =
computation: Async<'TResult1>,
continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
) : TaskCode<'TOverall, 'TResult2> =
this.Bind(Async.StartAsTask computation, continuation)
this.Bind(Async.StartImmediateAsTask computation, continuation)

member inline this.ReturnFrom(computation: Async<'T>) : TaskCode<'T, 'T> =
this.ReturnFrom(Async.StartAsTask computation)
this.ReturnFrom(Async.StartImmediateAsTask computation)
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,22 @@ type Basics() =
let result = t.Result
require (result = 8) "something weird happened"

[<Fact>]
member _.testAsyncsMixedWithTasks_ShouldNotSwitchContext() =
let t = task {
let a = Thread.CurrentThread.ManagedThreadId
let! b = async {
return Thread.CurrentThread.ManagedThreadId
}
let c = Thread.CurrentThread.ManagedThreadId
return $"Before: {a}, in async: {b}, after async: {c}"
}
let d = Thread.CurrentThread.ManagedThreadId
let actual = $"{t.Result}, after task: {d}"

require (actual = $"Before: {d}, in async: {d}, after async: {d}, after task: {d}") actual


[<Fact>]
// no need to call this, we just want to check that it compiles w/o warnings
member _.testDefaultInferenceForReturnFrom() =
Expand Down Expand Up @@ -1390,5 +1406,4 @@ module Issue12184f =
// The overload resolution for Bind commits to 'Task' via SRTP pattern since the type annotation is available
let! result = t
return result
}

}