From e151ee7be5f44c9fb10c0f252e3af30659d9dc41 Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Tue, 20 Dec 2022 16:57:02 +0100 Subject: [PATCH 1/3] Bind Async within task{} should start on the same thread --- src/FSharp.Core/tasks.fs | 4 ++-- .../Microsoft.FSharp.Control/Tasks.fs | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/FSharp.Core/tasks.fs b/src/FSharp.Core/tasks.fs index a52cae27310..13eb2a43a59 100644 --- a/src/FSharp.Core/tasks.fs +++ b/src/FSharp.Core/tasks.fs @@ -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) diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/Tasks.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/Tasks.fs index 4af1df56d39..4ed91efb2fa 100644 --- a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/Tasks.fs +++ b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/Tasks.fs @@ -1160,6 +1160,22 @@ type Basics() = let result = t.Result require (result = 8) "something weird happened" + [] + 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 + + [] // no need to call this, we just want to check that it compiles w/o warnings member _.testDefaultInferenceForReturnFrom() = @@ -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 - } - + } \ No newline at end of file From 18ce6acd2b91248eaba730ec5df7bc9bb49fb643 Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Wed, 21 Dec 2022 11:28:22 +0100 Subject: [PATCH 2/3] Bump fsharp core minor --- eng/Versions.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/Versions.props b/eng/Versions.props index 68c623823f5..885cf2522b1 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -14,7 +14,7 @@ beta 7 - 0 + 1 0 0 From 95cde3791c595dd6fca793fb93f474bc6cd4dddf Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Thu, 22 Dec 2022 10:59:41 +0100 Subject: [PATCH 3/3] Revert minor version bump in eng/Versions.props The automatic versioning will cover for this with next product release. --- eng/Versions.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/Versions.props b/eng/Versions.props index 885cf2522b1..68c623823f5 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -14,7 +14,7 @@ beta 7 - 1 + 0 0 0