<div dir="ltr"><div class="gmail_extra"><br><br><div class="gmail_quote">2014-06-05 4:13 GMT+04:00 Reid Kleckner <span dir="ltr"><<a href="mailto:reid@kleckner.net" target="_blank" class="cremed">reid@kleckner.net</a>></span>:<br>


<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: rnk<br>
Date: Wed Jun  4 19:13:43 2014<br>
New Revision: 210225<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=210225&view=rev" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=210225&view=rev</a><br>
Log:<br>
Flush C stdio streams upon process termination<br>
<br>
Due to what can only be described as a CRT bug, stdout and amazingly<br>
even stderr are not always flushed upon process termination, especially<br>
when the system is under high threading pressure.  I have found two<br>
repros for this:<br>
<br>
1) In lib\Support\Threading.cpp, change sys::Mutex to an<br>
std::recursive_mutex and run check-clang.  Usually between 30 and 40<br>
tests will fail.<br>
<br>
2) Add OutputDebugStrings in code that runs during static initialization<br>
and static shutdown.  This will sometimes generate similar failures.<br>
<br>
After a substantial amount of troubleshooting and debugging, I found<br>
that I could reproduce this from the command line without running<br>
check-clang.  Simply make the mutex change described in #1, then<br>
manually run the following command many times by running it once, then<br>
pressing Up -> Enter very quickly:<br>
<br>
D:\src\llvm\build\vs2013\Debug\bin\c-index-test.EXE -cursor-at=D:\src\llvm\tools\clang\test\Index\targeted-preamble.h:2:15 D:\src\llvm\tools\clang\test\Index\targeted-cursor.c -include D:\src\llvm\build\vs2013\tools\clang\test\Index\Output\targeted-cursor.c.tmp.h -Xclang -error-on-deserialized-decl=NestedVar1      -Xclang -error-on-deserialized-decl=TopVar    | D:\src\llvm\build\vs2013\Debug\bin\FileCheck.EXE D:\src\llvm\tools\clang\test\Index\targeted-cursor.c -check-prefix=PREAMBLE-CURSOR1<br>



<br>
Sporadically they will fail, and attaching a debugger to a failed<br>
instance indicates that stdin of FileCheck.exe is empty.<br>
<br>
Note that due to the repro in #2, we can rule out a bug in the STL's<br>
mutex implementation, and instead conclude that this is a real flake in<br>
the windows test harness.<br>
<br>
Test Plan:<br>
Without patch: Ran check-clang 10 times and saw over 30 Unexpected failures on every run.<br>
With patch: Ran check-clang 10 times and saw 0 unexpected failures across all runs.<br>
<br>
Reviewers: rnk<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D4021" target="_blank" class="cremed">http://reviews.llvm.org/D4021</a><br>
<br>
Patch by Zachary Turner!<br>
<br>
Modified:<br>
    cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c<br>
    cfe/trunk/tools/c-index-test/c-index-test.c<br>
<br>
Modified: cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c?rev=210225&r1=210224&r2=210225&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c?rev=210225&r1=210224&r2=210225&view=diff</a><br>



==============================================================================<br>
--- cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c (original)<br>
+++ cfe/trunk/tools/c-arcmt-test/c-arcmt-test.c Wed Jun  4 19:13:43 2014<br>
@@ -97,14 +97,20 @@ typedef struct thread_info {<br>
 void thread_runner(void *client_data_v) {<br>
   thread_info *client_data = client_data_v;<br>
   client_data->result = carcmttest_main(client_data->argc, client_data->argv);<br>
-#ifdef __CYGWIN__<br>
-  fflush(stdout);  /* stdout is not flushed on Cygwin. */<br>
-#endif<br>
+}<br>
+<br>
+static void flush_atexit(void) {<br>
+  // stdout, and surprisingly even stderr, are not always flushed on process<br>
+  // and thread exit, particularly when the system is under heavy load.<br>
+  fflush(stdout);<br>
+  fflush(stderr);<br>
 }<br>
<br>
 int main(int argc, const char **argv) {<br>
   thread_info client_data;<br>
<br>
+  atexit(flush_atexit);<br>
+<br>
 #if defined(_WIN32)<br>
   if (getenv("LIBCLANG_LOGGING") == NULL)<br>
     putenv("LIBCLANG_LOGGING=1");<br>
<br>
Modified: cfe/trunk/tools/c-index-test/c-index-test.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=210225&r1=210224&r2=210225&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=210225&r1=210224&r2=210225&view=diff</a><br>



==============================================================================<br>
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)<br>
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Jun  4 19:13:43 2014<br>
@@ -4116,14 +4116,20 @@ typedef struct thread_info {<br>
 void thread_runner(void *client_data_v) {<br>
   thread_info *client_data = client_data_v;<br>
   client_data->result = cindextest_main(client_data->argc, client_data->argv);<br>
-#ifdef __CYGWIN__<br>
-  fflush(stdout);  /* stdout is not flushed on Cygwin. */<br>
-#endif<br>
+}<br>
+<br>
+static void flush_atexit(void) {<br>
+  // stdout, and surprisingly even stderr, are not always flushed on process<br>
+  // and thread exit, particularly when the system is under heavy load.<br>
+  fflush(stdout);<br>
+  fflush(stderr);<br>
 }<br>
<br>
 int main(int argc, const char **argv) {<br>
   thread_info client_data;<br>
<br>
+  atexit(flush_atexit);<br>
+<br>
 #ifdef CLANG_HAVE_LIBXML<br>
   LIBXML_TEST_VERSION<br>
 #endif<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank" class="cremed">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div><div class="gmail_extra">FYI</div><div class="gmail_extra"><div class="gmail_extra">tools/clang/tools/c-index-test/c-index-test.c:4122:3: warning: C++ style comments are not allowed in ISO C90 [enabled by default]</div>

<div class="gmail_extra">   // stdout, and surprisingly even stderr, are not always flushed on process</div><div class="gmail_extra">   ^</div><div><br></div><div>Fixed in r210325.</div></div></div>