[llvm-bugs] [Bug 43389] New: possible deadlock in termination path on Windows
via llvm-bugs
llvm-bugs at lists.llvm.org
Fri Sep 20 13:24:52 PDT 2019
https://bugs.llvm.org/show_bug.cgi?id=43389
Bug ID: 43389
Summary: possible deadlock in termination path on Windows
Product: libc++
Version: unspecified
Hardware: PC
OS: Windows NT
Status: NEW
Severity: enhancement
Priority: P
Component: All Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: compnerd at compnerd.org
CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
Consider the following program:
```
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <thread>
int main()
{
std::setvbuf(stdout, NULL, _IOLBF, 0x1000);
std::cout << "Hello, world!";
_lock_file(stdout);
std::thread t([&] { std::_Exit(0); });
t.join();
return 0;
}
```
The `std::_Exit` will invoke the DLL destructors, from the invoking thread and
after terminating all other threads in the process. Because the main thread
was holding the lock on `stdout` when it was terminated, this will result in an
orphaned CriticalSection which the DLL destructor will attempt to acquire.
Fortunately, Windows will identify that the CriticalSection is orphaned and
terminate the program. This is not however guaranteed, and if the underlying
libc does not use a CriticalSection to handle the locking, the application may
deadlock.
This is visible to the user by having the program be terminated by a ^C sent to
the application while the application is writing to the console (and thus
acquiring the lock to the output stream). The destructor will attempt to flush
the stream before program termination, which will attempt to re-acquire the now
orphaned lock.
https://github.com/llvm-mirror/libcxx/blob/master/src/iostream.cpp#L135-L148
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190920/fef41e37/attachment.html>
More information about the llvm-bugs
mailing list