Skip to content

Commit e151ee7

Browse files
committed
Bind Async within task{} should start on the same thread
1 parent 73714c9 commit e151ee7

File tree

2 files changed

+19
-4
lines changed
  • src/FSharp.Core
  • tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control

2 files changed

+19
-4
lines changed

src/FSharp.Core/tasks.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ module MediumPriority =
459459
computation: Async<'TResult1>,
460460
continuation: ('TResult1 -> TaskCode<'TOverall, 'TResult2>)
461461
) : TaskCode<'TOverall, 'TResult2> =
462-
this.Bind(Async.StartAsTask computation, continuation)
462+
this.Bind(Async.StartImmediateAsTask computation, continuation)
463463

464464
member inline this.ReturnFrom(computation: Async<'T>) : TaskCode<'T, 'T> =
465-
this.ReturnFrom(Async.StartAsTask computation)
465+
this.ReturnFrom(Async.StartImmediateAsTask computation)

tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/Tasks.fs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,22 @@ type Basics() =
11601160
let result = t.Result
11611161
require (result = 8) "something weird happened"
11621162

1163+
[<Fact>]
1164+
member _.testAsyncsMixedWithTasks_ShouldNotSwitchContext() =
1165+
let t = task {
1166+
let a = Thread.CurrentThread.ManagedThreadId
1167+
let! b = async {
1168+
return Thread.CurrentThread.ManagedThreadId
1169+
}
1170+
let c = Thread.CurrentThread.ManagedThreadId
1171+
return $"Before: {a}, in async: {b}, after async: {c}"
1172+
}
1173+
let d = Thread.CurrentThread.ManagedThreadId
1174+
let actual = $"{t.Result}, after task: {d}"
1175+
1176+
require (actual = $"Before: {d}, in async: {d}, after async: {d}, after task: {d}") actual
1177+
1178+
11631179
[<Fact>]
11641180
// no need to call this, we just want to check that it compiles w/o warnings
11651181
member _.testDefaultInferenceForReturnFrom() =
@@ -1390,5 +1406,4 @@ module Issue12184f =
13901406
// The overload resolution for Bind commits to 'Task' via SRTP pattern since the type annotation is available
13911407
let! result = t
13921408
return result
1393-
}
1394-
1409+
}

0 commit comments

Comments
 (0)