Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit c19e37e

Browse files
committed
Flush C stdio streams upon process termination
Due to what can only be described as a CRT bug, stdout and amazingly even stderr are not always flushed upon process termination, especially when the system is under high threading pressure. I have found two repros for this: 1) In lib\Support\Threading.cpp, change sys::Mutex to an std::recursive_mutex and run check-clang. Usually between 30 and 40 tests will fail. 2) Add OutputDebugStrings in code that runs during static initialization and static shutdown. This will sometimes generate similar failures. After a substantial amount of troubleshooting and debugging, I found that I could reproduce this from the command line without running check-clang. Simply make the mutex change described in #1, then manually run the following command many times by running it once, then pressing Up -> Enter very quickly: 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 Sporadically they will fail, and attaching a debugger to a failed instance indicates that stdin of FileCheck.exe is empty. Note that due to the repro in #2, we can rule out a bug in the STL's mutex implementation, and instead conclude that this is a real flake in the windows test harness. Test Plan: Without patch: Ran check-clang 10 times and saw over 30 Unexpected failures on every run. With patch: Ran check-clang 10 times and saw 0 unexpected failures across all runs. Reviewers: rnk Differential Revision: http://reviews.llvm.org/D4021 Patch by Zachary Turner! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210225 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bb97c3d commit c19e37e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

tools/c-arcmt-test/c-arcmt-test.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,20 @@ typedef struct thread_info {
9797
void thread_runner(void *client_data_v) {
9898
thread_info *client_data = client_data_v;
9999
client_data->result = carcmttest_main(client_data->argc, client_data->argv);
100-
#ifdef __CYGWIN__
101-
fflush(stdout); /* stdout is not flushed on Cygwin. */
102-
#endif
100+
}
101+
102+
static void flush_atexit(void) {
103+
// stdout, and surprisingly even stderr, are not always flushed on process
104+
// and thread exit, particularly when the system is under heavy load.
105+
fflush(stdout);
106+
fflush(stderr);
103107
}
104108

105109
int main(int argc, const char **argv) {
106110
thread_info client_data;
107111

112+
atexit(flush_atexit);
113+
108114
#if defined(_WIN32)
109115
if (getenv("LIBCLANG_LOGGING") == NULL)
110116
putenv("LIBCLANG_LOGGING=1");

tools/c-index-test/c-index-test.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,14 +4116,20 @@ typedef struct thread_info {
41164116
void thread_runner(void *client_data_v) {
41174117
thread_info *client_data = client_data_v;
41184118
client_data->result = cindextest_main(client_data->argc, client_data->argv);
4119-
#ifdef __CYGWIN__
4120-
fflush(stdout); /* stdout is not flushed on Cygwin. */
4121-
#endif
4119+
}
4120+
4121+
static void flush_atexit(void) {
4122+
// stdout, and surprisingly even stderr, are not always flushed on process
4123+
// and thread exit, particularly when the system is under heavy load.
4124+
fflush(stdout);
4125+
fflush(stderr);
41224126
}
41234127

41244128
int main(int argc, const char **argv) {
41254129
thread_info client_data;
41264130

4131+
atexit(flush_atexit);
4132+
41274133
#ifdef CLANG_HAVE_LIBXML
41284134
LIBXML_TEST_VERSION
41294135
#endif

0 commit comments

Comments
 (0)