Skip to content

Commit 121571b

Browse files
Retry deletion of temporary files to avoid race conditions on Windows.
Differential Revision: http://reviews.llvm.org/D18912 llvm-svn: 265948
1 parent a45d3e4 commit 121571b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,23 +1049,13 @@ def dumpSessionInfo(self):
10491049
# it silently replaces the destination. Ultimately this means that atomic renames are not
10501050
# guaranteed to be possible on Windows, but we need this to work anyway, so just remove the
10511051
# destination first if it already exists.
1052-
os.remove(dst)
1052+
remove_file(dst)
10531053

10541054
os.rename(src, dst)
10551055
else:
10561056
# success! (and we don't want log files) delete log files
10571057
for log_file in log_files_for_this_test:
1058-
try:
1059-
os.unlink(log_file)
1060-
except:
1061-
# We've seen consistent unlink failures on Windows, perhaps because the
1062-
# just-created log file is being scanned by anti-virus. Empirically, this
1063-
# sleep-and-retry approach allows tests to succeed much more reliably.
1064-
# Attempts to figure out exactly what process was still holding a file handle
1065-
# have failed because running instrumentation like Process Monitor seems to
1066-
# slow things down enough that the problem becomes much less consistent.
1067-
time.sleep(0.5)
1068-
os.unlink(log_file)
1058+
remove_file(log_file)
10691059

10701060
# ====================================================
10711061
# Config. methods supported through a plugin interface
@@ -1996,4 +1986,17 @@ def DebugPExpect(self, child):
19961986
@classmethod
19971987
def RemoveTempFile(cls, file):
19981988
if os.path.exists(file):
1989+
remove_file(file)
1990+
1991+
# On Windows, the first attempt to delete a recently-touched file can fail
1992+
# because of a race with antimalware scanners. This function will detect a
1993+
# failure and retry.
1994+
def remove_file(file, num_retries = 1, sleep_duration = 0.5):
1995+
for i in range(num_retries+1):
1996+
try:
19991997
os.remove(file)
1998+
return True
1999+
except:
2000+
time.sleep(sleep_duration)
2001+
continue
2002+
return False

0 commit comments

Comments
 (0)