Skip to content

64-bit fscAnyCpu.exe uses more stack than 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022) #12322

Closed

Description

@kentcb

A project that is part of a reasonably complex solution is failing to build in 2022 due to the compiler crashing. I've managed to boil things down to a simple, standalone repro shown below.

Repro steps

The following simplified code typifies that which is causing the compiler to crash:

module repro_fscanycpu_failure

//
// This builds fine in VS2019, but fails under VS2022 with:
//
// 1>  Process is terminated due to StackOverflowException.
// 1>  C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools\Microsoft.FSharp.Targets(317,9): error MSB6006: "fscAnyCpu.exe" exited with code -1073741571.
//
// Opening this file in the VS2022 editor will also crash, presumably for the same underlying reason.
//

type ReproBuilder () =
    member _.Delay x = x ()
    member _.Yield  (x) = x
    member _.Combine (x, y) = x + y

let repro = ReproBuilder ()

let callSite () =
    repro {
        // Commenting out some of the below is enough to avoid StackOverflow on my machine.
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        0
        1
        2
        3
        4
    }

Expected behavior

It should build in 2022 as it does in 2019.

Actual behavior

It fails with:

1>C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools\Microsoft.FSharp.Targets(317,9): error MSB6006: "fscAnyCpu.exe" exited with code -1073741571.

Known workarounds

  1. Short-term fix: Use VS2019.

  2. Recommended: Make your inputs smaller, probably half the size will do, divide your large expressions into two

Related information

Provide any related information (optional):

  • Windows 10
  • .NET Core

Activity

  1. smoothdeveloper commented on Nov 1, 2021

    @smoothdeveloper
    Contributor

    @kentcb just in case it has an impact, can you try compiling the same project in Release mode (assuming it was Debug mode you tried)?

  2. kentcb commented on Nov 1, 2021

    @kentcb
    Author

    @smoothdeveloper Alas, same problem.

  3. eriawan commented on Nov 1, 2021

    @eriawan
    Member

    @kentcb
    I think this is something related to TFM of the nuget package you used.
    Have you updated the microsoft.servicefabric nuget package? I've used version 8.1.335 and I have no problem.

    See also
    https://www.nuget.org/packages/Microsoft.ServiceFabric/8.1.335

  4. cartermp commented on Nov 1, 2021

    @cartermp
    Contributor

    @kentcb if you're allowed to build with the /bl flag and upload the binlog then that makes it a lot easier for folks to diagnose.

  5. vzarytovskii commented on Nov 1, 2021

    @vzarytovskii
    Member

    @kentcb if you're allowed to build with the /bl flag and upload the binlog then that makes it a lot easier for folks to diagnose.

    Yeah, binlog or minimal repro would be ideal.

  6. kentcb commented on Nov 1, 2021

    @kentcb
    Author

    @eriawan Yes, I did try upgrading to latest Microsoft.ServiceFabric.Services.Remoting to no avail. I've also tried to extract this into a minimal repro, but no luck.

    Happy to upload a binlog, but is there somewhere private I can upload it or someone I can share a OneDrive link with?

  7. kentcb commented on Nov 1, 2021

    @kentcb
    Author

    By building with more diagnostics, I could see that fsAnyCpu.exe was terminating due to stack overflow when building my Types project. By process of elimination, I identified which file was causing the crash. Even opening this file in the editor would eventually crash VS, so I had to edit it in VS2019, but doing so allowed me to identify the code that was triggering the regression...

    It's a custom CE we use to define codecs. We use it extensively throughout our entire solution, and yet this one particular case was causing a SO. By playing around with it, I determined that there was no particular line triggering the SO, but rather the sheer number of them. It was happy with up to 38 lines, but the 39th would trigger a crash (either in editor, or in compiler, so presumably the same SO crash).

    Of course, there must be something our CE is doing, or something about what we're doing on each line to trigger the SO. And of course, something about FSC must have changed such that this is crashing where it never previously did. That's where I'm at and will possibly handball this issue internally.

  8. jimfoye commented on Nov 2, 2021

    @jimfoye

    Perhaps related to #9231?

  9. kentcb commented on Nov 2, 2021

    @kentcb
    Author

    @jimfoye Hmm, maybe, but I use Windows and sounds like it's a non-issue there. I'll try and find some time to repro this in a standalone project later in the week.

  10. kentcb commented on Nov 4, 2021

    @kentcb
    Author

    @vzarytovskii @cartermp Please see updated issue description for a simple repro

  11. dsyme commented on Nov 5, 2021

    @dsyme
    Contributor

    @kentcb Thanks for the repro. We'll work on it, we don't have a large-input stress test of that size, and we must be doing something quadratic or worse.

  12. ly29 commented on Nov 9, 2021

    @ly29

    Noticed something similar which crashes Visual Studio 2022 hard.

  13. 4 remaining items

  14. dsyme commented on Nov 17, 2021

    @dsyme
    Contributor

    For the first repro up above:

    • F# 5 net472 VS2019 shipped fsc.exe 32-bit blows stack with > ~80 nested Combine/Yield in computation expression

      "c:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools\fsc.exe" a.fs

    • F# 6 net472 VS2022 shipped fsc.exe 32-bit blows stack with > ~75 nested Combine/Yield in computation expression

      "c:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools\fsc.exe" a.fs

    • F# 6 net472 VS2022 shipped fscAnyCpu.exe 64-bit blows stack with > ~40 nested Combine/Yield in computation expression

      "c:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools\fscAnyCpu.exe" a.fs

    • F# 5 net5 SDK fsc.dll 64-bit blows stack with > ~65 nested Combine/Yield in computation expression

      dotnet "\Program Files\dotnet\sdk\5.0.403\FSharp\fsc.dll" a.fs

    • F# 6 net5 SDK fsc.dll 64-bit blows stack with > ~75 nested Combine/Yield in computation expression

      dotnet "\Program Files\dotnet\sdk\6.0.100\FSharp\fsc.dll" a.fs

    So it seems that F# 6 code is not really at fault (e.g. net472, net5 process about the same), but rather the switch to 64-bit compiler for .NET Framework by default is causing 2x stack to be used.

    Note that we're seeing the problems in large computation expressions in particular because these do not produce SyntaxTree and TypedTree results that are processed "linearly" in the compiler (e.g. unlike large lists, or large lists of let and sequential execution), rather they are treated as large nested method application expressions.

    @KevinRansom Note that we have a disparity in input size processing capabilities between .NET SDK and .NET Framework in any case and so there is an implicit dependency on the .NET Framework compiler being picked up by uses here. We should keep trying to pin down input processing sizes and make sure that .NET Core compiler is always able to process inputs as least as large as .NET Framework.

    I get the same error (fscAnyCpu.exe exiting with -1073741571) in VS 2022 for a solution that builds in VS 2019. It does seem to be computation-expression related—in my case, it seems to be the gen computation expression from FsCheck.

    @brianrourkeboll Do you have a public repro?

    Yeah I am getting the -1073741571 error when building FSharp.Data in VS 2022.

    Thanks, we will use this to form the basis of a repro too

  15. dsyme commented on Nov 17, 2021

    @dsyme
    Contributor

    Note that this workaround:

        <FSharpPreferNetFrameworkTools>true</FSharpPreferNetFrameworkTools>
        <FSharpPrefer64BitTools>false</FSharpPrefer64BitTools>
    

    is not sufficient to stop IDE failures, because the IDE is now running as 64-bit. Instead you should simply make your problematic inputs smaller as a workaround.

    I suppose we must work on linearizing the processing of the above.

  16. dsyme commented on Nov 17, 2021

    @dsyme
    Contributor

    @jimfoye I investigated the failure building Debug FSharp.Data - see fsprojects/FSharp.Data#1411 for the details, and it's related to the separate problem #12413

  17. changed the title [-]Project that builds in VS2019 fails to build in VS2022[/-] [+]64-bit fscAnyCpu.exe uses more stack and can't compile inputs accepted by 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022)[/+] on Nov 17, 2021
  18. changed the title [-]64-bit fscAnyCpu.exe uses more stack and can't compile inputs accepted by 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022)[/-] [+]64-bit fscAnyCpu.exe uses more stack,can't compile inputs accepted by 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022)[/+] on Nov 17, 2021
  19. changed the title [-]64-bit fscAnyCpu.exe uses more stack,can't compile inputs accepted by 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022)[/-] [+]64-bit fscAnyCpu.exe uses more stack than 32-bit fsc.exe (was: Project that builds in VS2019 fails to build in VS2022)[/+] on Nov 17, 2021
  20. brianrourkeboll commented on Nov 17, 2021

    @brianrourkeboll
    Contributor

    @dsyme I'm not sure if this is what you were looking for, or whether it adds anything beyond @kentcb's minimal repro above, but the following snippet with 115 let!s in FsCheck's gen computation expression will crash VS 2022 if you open it in an F# script file (which however compiles fine when passed into dotnet fsi directly). The ~114 let! limit for the gen computation expression accords with what I see in my actual projects (i.e., files with fewer let!s are fine, but files with more cause VS 2022 to crash).

    Repro
    #r "nuget: FsCheck"
    
    open FsCheck
    
    let g =
        gen {
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            let! _ = Arb.generate<int>
            return ()
        }

    If you remove one let!, leaving only 114, it will not crash VS.


    Separately, adding <FSharpPrefer64BitTools>false</FSharpPrefer64BitTools> to the affected fsproj files as you suggested above does indeed allow them to build in VS (as long as you don't open the files with the big computation expressions).


    It may also be worth noting that I have another (custom, unrelated) computation expression in a separate project which in one case is used with more than 200 and!s—this also does not compile in VS 2022, but it does not crash VS, and the failure to compile seems to be for a different reason:

    error FS0073: internal error: Expression is too large and/or complex to emit. Method name: '...'. Recursive depth: 140.

    I at first suspected that the difference between this case and that of gen is that most of this computation expression builder's methods are marked inline, while those for gen are not. That appears not to be the case, though: it just seems that the compiler will tolerate more and!s (closer to ~300) than let!s.

    In this case, however, setting FSharpPreferNetFrameworkTools to false allows my project to build without error in VS 2022 (it builds without error in VS 2019 and with dotnet build using the .NET 5 or 6 SDKs).

  21. dsyme commented on Nov 18, 2021

    @dsyme
    Contributor

    @brianrourkeboll Thank you

    Realistically our three shipped compilers (netfx 32, netfx 64, net6) do have different capacity limits along some input dimensions. Ideally the capacity limits would be identical across all three , but that's really, really hard to arrange without avoiding the use of stack altogether.

    I will look at the specific case of these computation expressions. We do have the option of simply switching to a new stack, for example, at a particular expression depth.

  22. dsyme commented on Nov 18, 2021

    @dsyme
    Contributor

    This is fixed in #12420, at least for practical purposes, given the current repros.

  23. jayhill commented on Nov 18, 2021

    @jayhill

    Wow — impressive @dsyme!

    Would this also somehow mitigate this situation and repro, in which only let bindings — not custom CEs — are in play?

    That is the same issue that I'm experiencing.

  24. dsyme commented on Nov 18, 2021

    @dsyme
    Contributor

    Would this also somehow mitigate this situation and repro, in which only let bindings — not custom CEs — are in play?

    No, but we can get that fixed too

  25. dsyme commented on Nov 19, 2021

    @dsyme
    Contributor

    @jayhill @patroeper Thank you for the repro, it was a new problem related to writing debug scopes. I'm not sure why our existing testing didn't catch it as we certainly have test with hundreds or thousands of let . Hmmmm....

    In any case, a fix and testing is included in #12420

  26. dsyme commented on Nov 19, 2021

    @dsyme
    Contributor

    @jayhill @patroeper A note on your repro: you are using --debug:full (<DebugType>full</DebugType>) instead of --debug:portable (portable`). For some variations of your example, and among other problems, this casuses a stack overflow in the C++ code we use to emit old-style PDBs. The solution for this is to move to emit portable PDBs, so add this to your project files:

    <DebugType>portable</DebugType>
    

    I think some of our fixes to debugging scopes exposed this problem. It's not a thing we can fix, and it is likely we will just accept these new capacity limits for old-style PDBs and instead ask people to switch to emit portable PDBs.

  27. baronfel commented on Jan 7, 2022

    @baronfel
    Member

    @dsyme should this be closed since #12420 was merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions