Skip to content

Commit

Permalink
Bind of Async<> within task{} should start on the same thread (#14499)
Browse files Browse the repository at this point in the history
* Bind Async within task{} should start on the same thread
The automatic versioning will cover for this with next product release.
  • Loading branch information
T-Gro authored Dec 27, 2022
1 parent c3aacd0 commit 35971aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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
}

}

0 comments on commit 35971aa

Please sign in to comment.