From f2d690029c6dab9d586a9ba1a2e0af03dc7f3c70 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 14 Jul 2015 09:41:17 -0700 Subject: [PATCH] client (Win): when read stderr.txt, wait for write lock to be release first. Apparently, on Win, there is still a write lock on stderr.txt, and its buffer isn't flushed, until shortly after the app process exits. This is bizarre, but so be it. --- lib/util.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/util.cpp b/lib/util.cpp index 2ac4deef01..3f65303cc2 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -322,6 +322,30 @@ int read_file_malloc(const char* path, char*& buf, size_t max_len, bool tail) { int retval; double size; + // Win: if another process has this file open for writing, + // wait for up to 5 seconds. + // This is because when a job exits, the write to stderr.txt + // sometimes (inexplicably) doesn't appear immediately + +#ifdef _WIN32 + for (int i=0; i<5; i++) { + HANDLE h = CreateFileA( + path, + GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL + ); + if (h != INVALID_HANDLE_VALUE) { + CloseHandle(h); + break; + } + boinc_sleep(1); + } +#endif + retval = file_size(path, size); if (retval) return retval;