Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to properly step through code in static constructor (VS2019, C#, .NET 4.7.2)

I am trying to step through the code of a static constructor while in break mode.

The project is C#/.NET 4.7.2/64-bit/WinForms. Visual Studio version is 2019 16.9.4 Community.

Visual Studio correctly breaks when it gets to the breakpoint set in the static constructor. I can then step through the code using the "Step Into" command (F11). As you can see, the static constructor calls a static method which does the heavy work.

The code contains a loop that should iterate 10 times. I should be able to step through all iterations. Instead, after the first iteration, debugging suddenly jumps to -- or "resumes" -- at some much later point, back in the calling class (or more accurately, the class that triggered the CLR to execute the static constructor). So I am unable to step through the remaining 9 iterations.

I am confident that all iterations are indeed executed, because I added some debug code to print something every time the finally block is executed. But, I am frustrated that I cannot step through the code. Seems like a VS bug of the ludicrous kind, since it's a pretty fundamental thing to be able to step through code when debugging.

Because the problem is so hard to describe well, I have created an animated GIF to visually show the debug session:

Visual Studio 2019 debug session example

I have searched the web far and wide and I can't find anyone else reporting the kind of issue I am here. Which has me wondering whether I am doing something wrong (e.g. do I need to adjust some debugger settings or something?). Any help or insight is much appreciated.

UPDATE

I cannot reproduce the issue in 32-bit Debug builds. The issue is (so far) only present in 64-bit Debug builds.

HOW TO REPRODUCE

I made a tiny demo project. Feel free to download and try debug for yourself. When the breakpoint is hit, use F11 to step through the code. See if you can get through the loop 10 times ;-)

Debug Test Project (VS2019)

On my end, the issue disappears if I change the build configuration from x64 to Any CPU. So it may be a 64-bit only issue.

The try/finally block really seems to reveal the issue. I'm not sure what other patterns might reveal the issue too.

BUG REPORT FILED

https://developercommunity.visualstudio.com/t/The-debugger-does-not-step-through-a-met/1407274

like image 937
Michal Cihelka Avatar asked Sep 06 '25 01:09

Michal Cihelka


1 Answers

It's a bug in the .NET runtime. You can track the bug here:

https://github.com/dotnet/runtime/issues/52328

like image 94
Michal Cihelka Avatar answered Sep 09 '25 00:09

Michal Cihelka