From fb9d36e050aab36b93752e549ab8a6ddf745392a Mon Sep 17 00:00:00 2001 From: Bob Burger Date: Wed, 21 Feb 2018 16:39:26 -0500 Subject: [PATCH] Use spin loop instead of Sleep for more robust race condition mitigation original commit: fb9f854d449ee96cccb63a12629a729046f0bee1 --- c/windows.c | 40 +++++++++++++++++++++----------- release_notes/release_notes.stex | 3 +-- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/c/windows.c b/c/windows.c index 42bea92de1..f8e31e0286 100644 --- a/c/windows.c +++ b/c/windows.c @@ -408,14 +408,20 @@ int S_windows_rename(const char *oldpathname, const char *newpathname) { int S_windows_rmdir(const char *pathname) { wchar_t wpathname[PATH_MAX]; - int rc; if (MultiByteToWideChar(CP_UTF8,0,pathname,-1,wpathname,PATH_MAX) == 0) - rc =_rmdir(pathname); - else - rc = _wrmdir(wpathname); - if (0 == rc) - Sleep(0); // Give Windows time to delete the directory. - return rc; + return _rmdir(pathname); + else { + int rc; + if (!(rc = _wrmdir(wpathname))) { + // Spin loop until Windows deletes the directory. + int n; + for (n = 100; n > 0; n--) { + if (_wrmdir(wpathname) && (errno == ENOENT)) break; + } + return 0; + } + return rc; + } } int S_windows_stat64(const char *pathname, struct STATBUF *buffer) { @@ -436,14 +442,20 @@ int S_windows_system(const char *command) { int S_windows_unlink(const char *pathname) { wchar_t wpathname[PATH_MAX]; - int rc; if (MultiByteToWideChar(CP_UTF8,0,pathname,-1,wpathname,PATH_MAX) == 0) - rc = _unlink(pathname); - else - rc = _wunlink(wpathname); - if (0 == rc) - Sleep(0); // Give Windows time to delete the file. - return rc; + return _unlink(pathname); + else { + int rc; + if (!(rc = _wunlink(wpathname))) { + // Spin loop until Windows deletes the file. + int n; + for (n = 100; n > 0; n--) { + if (_wunlink(wpathname) && (errno == ENOENT)) break; + } + return 0; + } + return rc; + } } char *S_windows_getcwd(char *buffer, int maxlen) { diff --git a/release_notes/release_notes.stex b/release_notes/release_notes.stex index 1459130cfc..8a5bb3fb5f 100644 --- a/release_notes/release_notes.stex +++ b/release_notes/release_notes.stex @@ -1546,8 +1546,7 @@ in fasl files does not generally make sense. In Windows, deleting a file or directory briefly leaves the file or directory in a state where a subsequent create operation fails with -permission denied. This race condition is now mitigated by a call to -Sleep. +permission denied. This race condition is now mitigated. [This bug applies to all versions up to 9.5 on Windows 7 and later.] \subsection{Incorrect handling of offset in