Program freezes in Visual Studio debugger

Some prerequisites: I'm trying to track down an error that causes me serious 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 the VS 2013 debugger, the program crashes with an output similar to this:

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

Oddly enough, if I pause the debugger and check what happens, one of the threads is inside AcquireSRWLockExclusive (in NtWaitForAlertByThreadId), apparently there are no reasons why the program hangs. When I click resume, the program happily continues and prints a few more things until it is locked again.

Do you have any idea what is going on here?

Additional Information:

  • As far as I can tell, this error only exists in Windows 8.1.
  • I tried VS2013.4 and VS2015 RC.
  • I could play it on two different computers running Windows 8.1.
  • One of the machines was formatted: RAM, CPU and Disk were tested (I was thinking about a malfunction, because at first I could only see the error on this particular machine)
  • I could never play it in Windows 7.
  • It may be useful to change the code between comments to observe the error. When I added a microsecond sleep, I could finally reproduce the error on another computer.
  • With VS2015 RC, I could reproduce the same behavior with a simple std :: mutex. On VS2013, however, SRWLOCK seems to be mandatory for observing the error.
+7
c ++ c ++ 11 visual-c ++ visual-studio
source share
2 answers

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

+4
source share

For me, this seems like an error in Windows, I have different versions of the code that hang using the new Vista primitives under the debugger in Win 8.1 / Server 2012R2 after the April 2014 update. Also hanging is also a thread pool waiting function. It seems that it is mostly tied to the completed execution of another thread at the time of waiting / blocking. Here is simple code that always hangs under the debugger in NtWaitForAlertByThreadId ():

 #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #pragma optimize("",off) VOID CALLBACK _WorkCallback(PTP_CALLBACK_INSTANCE Instance, PVOID pUser, PTP_WORK Work) { for (int i = 0; i < INT_MAX / 256; i++) {} } DWORD WINAPI ThreadProc(LPVOID lpParameter) { for (int i = 0; i < INT_MAX / 256; i++) {} return 0; } #pragma optimize("",on) int _tmain(int argc, _TCHAR* argv[]) { LONGLONG c = 0; while(!_kbhit()) { PTP_WORK ptpw = CreateThreadpoolWork(&_WorkCallback, NULL, NULL); if (ptpw != NULL) { for(long i = 0; i < 3; i++) SubmitThreadpoolWork(ptpw); CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL); WaitForThreadpoolWorkCallbacks(ptpw, FALSE); CloseThreadpoolWork(ptpw); } printf("%I64d \r", c++); } _getch(); return 0; } 

Unfortunately, I have no idea where to report this to Microsoft.

+1
source share

All Articles