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

Async computation hanging in lastest F# 4.1 builds #3254

Closed
eiriktsarpalis opened this issue Jun 24, 2017 · 16 comments
Closed

Async computation hanging in lastest F# 4.1 builds #3254

eiriktsarpalis opened this issue Jun 24, 2017 · 16 comments
Labels
Area-Library Issues for FSharp.Core not covered elsewhere Bug Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. Ready Regression

Comments

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Jun 24, 2017

While going through the discussion in #3219 I noticed that a particular computation seems to be hanging when run in F# Interactive 4.1

cc @matthid

Repro steps

In F# Interactive 4.1

open System
open System.Threading
open System.Threading.Tasks

let test() =
    let tcs = new TaskCompletionSource<int>()
    let cts = new CancellationTokenSource()
    let _ = cts.Token.Register(fun () -> tcs.SetResult 42)
    async {
        cts.CancelAfter 500
        let! result = tcs.Task |> Async.AwaitTask
        return result
    } |> fun a -> Async.RunSynchronously(a, cancellationToken = cts.Token)

function test does execute as expected.

Expected behavior

Should fail with OperationCanceledException

Actual behavior

Hangs indefinitely

Workarounds

In compiled code, use FSharp.Core nuget package 4.1.17 (containing a version of FSharp.Core.dll 4.4.1.0 that does not contain this problem). Avoid using FSharp.Core nuget package 4.2.1 (which contains this problem).

In F# Interactive code, use a latest update to F# Interactive and your compiler tools (e.g.Visual Studio 2017 Update 3)

Related information

This only occurs in F# interactive 4.1 (32bit and 64bit builds) in both windows and mono/linux. It does not happen in console applications running FSharp.Core 4.4.1.0. I can't reproduce the issue in F# Interactive 4.0.

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

@eiriktsarpalis If you want I can take care of this as well, as I'm already looking at the code and writing some tests. Even if we don't want to accept my PR as a whole we can probably cherry-pick this fix afterwards (or I can send a separate PR after the discussion)...

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

Hm I can't actually reproduce this. I have Microsoft (R) F# Interactive version 4.1 with

> typeof<option<_>>.Assembly.FullName;;
val it : string =
  "FSharp.Core, Version=4.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
> test();;
System.OperationCanceledException: The operation was canceled.
   at Microsoft.FSharp.Control.AsyncBuilderImpl.commit[a](AsyncImplResult`1 res)
   at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronouslyInAnotherThread[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
   at Microsoft.FSharp.Control.CancellationTokenOps.RunSynchronously[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
   at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken)
   at <StartupCode$FSI_0040>.$FSI_0040.main@()
Stopped due to error

Should fsi use another FSharp.Core version?

@forki
Copy link
Contributor

forki commented Jun 24, 2017

@matthid as a general rule in this project: please split things up in very very small parts. That increases the chances to get something in in time. Bug pull requests often rot for very long time since people are afraid to merge.

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

@forki Yeah I know, it's just for discussion and POC once we decide what is acceptable and what not I can split it up. Because this might mean doing things differently...

@realvictorprm
Copy link
Contributor

realvictorprm commented Jun 24, 2017

@matthid, @eiriktsarpalis I can reproduce this with the latest master starting fsi any cpu in debug mode.
It's waiting for a result... no end

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

@eiriktsarpalis I can now reproduce in a unit test :)

@eiriktsarpalis
Copy link
Member Author

Weird, but it looks like I cannot reproduce when I launch fsi from the command line, only from Visual Studio. I can't reproduce in ionide on windows, but I it happens consistently in ionide on mono. Strange...

@realvictorprm
Copy link
Contributor

@eiriktsarpalis We need a version dif because it looks like this bug was introduced after the last update for the fsi on windows.

@eiriktsarpalis
Copy link
Member Author

@realvictorprm Does VF# run a different build of fsi 4.1 than the one installed in the dev tools? Running

System.Reflection.Assembly.GetEntryAssembly().Location

seems to indicate that VF# launches some temp copy

C:\Users\eirik\AppData\Local\assembly\dl3\OW481XT9.V2P\5H7WMBY5.CXB\89e45b56\fa90b7d2_1becd201\fsiAnyCpu.exe (SHA abf85160c0cfbe880497a9990ee0748e3c14f64a)

whereas cli and ionide give the expected path

C:\Program Files (x86)\Microsoft SDKs\F#\4.1\Framework\v4.0\FsiAnyCpu.exe (SHA cbb55cae8752ea1525ec4aa0165e5ee51850f6c9)

@eiriktsarpalis
Copy link
Member Author

@realvictorprm My linux box uses the latest fsharp OSS built from source, which seems consistent with a recent bug causing this.

@eiriktsarpalis eiriktsarpalis changed the title Async computation hanging in F# Interactive 4.1 Async computation hanging in lastest F# 4.1 builds Jun 24, 2017
@realvictorprm
Copy link
Contributor

realvictorprm commented Jun 24, 2017

@eiriktsarpalis With the Visual F# solution you will always use the latest (nightly) version.
However if you're installing the Visual F# tools you're going to use the installed sdk stuff (so far I know).

@eiriktsarpalis
Copy link
Member Author

@realvictorprm confirming that this reproduces with the latest master in F# OSS (commit 4f50d4f22e090a59889412f96fdefa0cc209a1c9). There's no version number as such printed in fsi other than the obvious (4.1)

@realvictorprm
Copy link
Contributor

@eiriktsarpalis I realized so too 😢 Currently trying to figure out the version of fsi on my pc. I do know that the last change to the file has been made on 10th May 😄

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

@eiriktsarpalis Problem is probably here: https://github.com/Microsoft/visualfsharp/blob/master/src/fsharp/FSharp.Core/control.fs#L1560

What the cancellationToken does at this place is cancel the Task which is RETURNED by ContinueWith which means the continuation is never called. I'd remove this parameter.

Bug was introduced here: 7dfa689

@matthid
Copy link
Contributor

matthid commented Jun 24, 2017

Btw it's a bit strange to call different overloads of ContinueWith depending on compiler directives. The helper function continueWithExtra even has different signatures as far as I can see...

matthid added a commit to matthid/visualfsharp that referenced this issue Jun 24, 2017
@dsyme dsyme added Bug Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. Area-Library Issues for FSharp.Core not covered elsewhere Regression Ready labels Jun 26, 2017
@dsyme dsyme closed this as completed in c6a070b Jun 27, 2017
@dsyme
Copy link
Contributor

dsyme commented Jun 27, 2017

Fixed - a huge thank you to both of you @matthid and @eiriktsarpalis

dsyme pushed a commit that referenced this issue Jul 25, 2017
* add another test for #3254, ported from paket. related to #3350 and fsprojects/Paket#2553

* Fix exception message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Library Issues for FSharp.Core not covered elsewhere Bug Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. Ready Regression
Projects
None yet
Development

No branches or pull requests

5 participants