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

FSharp.Core: Add Async.RunImmediate(Async<'T>, CancellationToken) #14532

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vzarytovskii
Copy link
Member

@vzarytovskii vzarytovskii commented Jan 2, 2023

See fsharp/fslang-suggestions#1042

Do we need more tests around cancellation/exception handling?

Whitespace cleanup was intentional

@vzarytovskii vzarytovskii requested a review from a team as a code owner January 2, 2023 16:34
@T-Gro
Copy link
Member

T-Gro commented Jan 2, 2023

You can also (or I can do it in a follow-up PR) replace:

~\tests\FSharp.Test.Utilities\Utilities.fs func RunImmediate
~\src\Compiler\Utilities\illib.fs func RunImmediate
~\src\Compiler\Facilities\BuildGraph.fs NodeCode.RunImmediate to use it (right now it used Async.StartImmediateAsTask(work, cancellationToken = ct).Result, which I believe attempted to do similar thing by starting on the same thread, but made exception handling worse)

EDIT:
~\vsintegration\src\FSharp.LanguageService\LanguageServiceConstants.fs also has it's version
~\tests\service\Common.fs also has it's version
and first branch of ~\vsintegration\src\FSharp.Editor\Common\Extensions.fs RunImmediateExceptOnUI as well.

@vzarytovskii
Copy link
Member Author

vzarytovskii commented Jan 2, 2023

You can also (or I can do it in a follow-up PR) replace:

~\tests\FSharp.Test.Utilities\Utilities.fs func RunImmediate ~\src\Compiler\Utilities\illib.fs func RunImmediate ~\src\Compiler\Facilities\BuildGraph.fs NodeCode.RunImmediate to use it (right now it used Async.StartImmediateAsTask(work, cancellationToken = ct).Result, which I believe attempted to do similar thing by starting on the same thread, but made exception handling worse)

EDIT: ~\vsintegration\src\FSharp.LanguageService\LanguageServiceConstants.fs also has it's version ~\tests\service\Common.fs also has it's version and first branch of ~\vsintegration\src\FSharp.Editor\Common\Extensions.fs RunImmediateExceptOnUI as well.

We can't reuse it everywhere just yet, since FSHARPCORE_USE_PACKAGE exists (it's the case when FSharp.Compiler.Service solution is being built).
Probably, the safest will be to replace it when we ship new FSharp.Core to nuget.

Comment on lines +1504 to +1507
static member RunImmediate(computation: Async<'T>, ?cancellationToken: CancellationToken) =
let cancellationToken = defaultArg cancellationToken Async.DefaultCancellationToken
AsyncPrimitives.RunImmediate cancellationToken computation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ResultCell version here instead of TCS

@vzarytovskii vzarytovskii requested a review from dsyme January 2, 2023 17:43
Copy link
Member

@T-Gro T-Gro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to see tests showing the exception stack traces between RunImmediate and RumSynchronously.

@vzarytovskii
Copy link
Member Author

Would be good to see tests showing the exception stack traces between RunImmediate and RumSynchronously.

Not sure I understand exactly what do you mean? Just to compare frames? Let me see if we already have such tests for existing run methods. There were no changes in how things are scheduled/executed.

@KevinRansom KevinRansom changed the title FSharp.Core: Add Async.Runimmediate(Async<'T>, CancellationToken) FSharp.Core: Add Async.RunImmediate(Async<'T>, CancellationToken) Jan 3, 2023
@@ -1501,6 +1501,10 @@ type Async =

computation.Invoke newCtxt)

static member RunImmediate(computation: Async<'T>, ?cancellationToken: CancellationToken) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should be preview right?

[<Experimental("Experimental library feature, requires '--langversion:preview'")>]

@@ -47,6 +47,42 @@ namespace Microsoft.FSharp.Control
[<CompiledName("FSharpAsync")>]
type Async =

/// <summary>Runs the asynchronous computation and await its result.</summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <summary>Runs the asynchronous computation and await its result.</summary>
/// <summary>Runs the asynchronous computation and awaits its result.</summary>

let actual = $"{t}, after task: {d}"

if not (actual = $"Before: {d}, in async: {d}, after async: {d}, after task: {d}") then
failwith actual
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a while to understand what's going on here, I'd just give vars clear names ("threadIdBefore", "threadIdAfter"...) and assert them all against Thread.CurrentThread.ManagedThreadId.

@psfinaki
Copy link
Member

psfinaki commented Jan 4, 2023

I'd also suggest either here or in a followup to go through the codebase and make use of the new method.

@vzarytovskii vzarytovskii marked this pull request as draft February 24, 2023 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants