Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program hangs in Visual Studio debugger

Some background: I am trying to track a bug which is causing me major headaches. After many dead ends (see this question) I finally ended up with this code:

#include <thread>
#include <vector>
#include <iosfwd>
#include <sstream>
#include <string>
#include <windows.h>

int main()
{
    SRWLOCK srwl;
    InitializeSRWLock(&srwl);
    for(size_t i=0;i<1000;++i)
    {
        std::vector<std::thread>threads;
        for(size_t j=0;j<100;++j)
        {
            OutputDebugString(".");
            threads.emplace_back([&](){
                AcquireSRWLockExclusive(&srwl);
                //Code below modifies the probability to see the bug.
                std::this_thread::sleep_for(std::chrono::microseconds(1));
                std::wstringstream wss;
                wss<<std::this_thread::get_id();
                wss.str();
                //Code above modifies the probability to see the bug.
                ReleaseSRWLockExclusive(&srwl);});
        }
        for(auto&t:threads){t.join();}
        OutputDebugString((std::to_string(i)+"\n").data());
    }
    return 0;
}

When I run this code inside VS 2013 debugger the program hangs with an output like this one:

....................................................................................................0
....................................................................................................1
....................................................................................................2
...........................

Strangely enough, If I pause the debugger and inspect what is going on, one of the threads is inside AcquireSRWLockExclusive (in NtWaitForAlertByThreadId) apparently there is no reason why the program is hanging. When I click resume, the program happily continues and print some more stuff until it is blocked again.

Do you have any Idea what is going on here ?

Some more info:

  • As far as I can tell, this bug only exists on Windows 8.1.
  • I tried VS2013.4 and VS2015 RC.
  • I could reproduce it on two different computers under Windows 8.1.
  • One of the machine was formatted, the RAM, CPU and Disk tested (I thought of a malfunction because at first I could only observe the bug on this particular machine)
  • I could never reproduce it on Windows 7.
  • It may be useful to modify the code between the comments to observe the bug. When I added the microsecond sleep, I could at last reproduce the bug on another computer.
  • With VS2015 RC I could reproduce the same behavior with a simple std::mutex. On VS2013 however the SRWLOCK seems mandatory to observe the bug.
like image 589
Arnaud Avatar asked May 12 '15 15:05

Arnaud


People also ask

How do I fix the Debug executable in Visual Studio?

Just go to File->Open->Project/Solution and browse to the .exe file. Like you would if it was a . sln file. Visual Studio will then open that EXE as a project.

How do I force stop debugging in Visual Studio?

To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.

Why is my Vscode debugger not working?

The most common problem is that you did not set up launch.json or there is a syntax error in that file. Alternatively, you might need to open a folder, since no-folder debugging does not support launch configurations.


1 Answers

This issue is caused by an OS scheduler bug introduced in the spring 2014 update to Windows 8.1. A hotfix for this problem was released in May 2015 and available at https://support.microsoft.com/en-us/kb/3036169.

like image 106
Antosha Avatar answered Sep 28 '22 16:09

Antosha