diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2018-04-11 09:30:39 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-04-11 09:30:39 +0200 |
commit | cb43f64ec4a81b1ffabc7c20f67f71b8d6c58e98 (patch) | |
tree | cbc6c1480a4781bc23ed736baba284ec8f4f0237 | |
parent | 904252e99a9d3217eda01159ab83e9384db54590 (diff) | |
download | psutil-cb43f64ec4a81b1ffabc7c20f67f71b8d6c58e98.tar.gz |
have safe_rmpath() on windows retry for 1 sec in case of failure as a workaround for orphaned open handles
-rw-r--r-- | psutil/tests/__init__.py | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index dcdbd4fa..80404a33 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -357,7 +357,7 @@ def create_proc_children_pair(): subp = pyrun(s) child1 = psutil.Process(subp.pid) data = wait_for_file(_TESTFN2, delete=False, empty=False) - os.remove(_TESTFN2) + safe_rmpath(_TESTFN2) child2_pid = int(data) _pids_started.add(child2_pid) child2 = psutil.Process(child2_pid) @@ -663,7 +663,7 @@ def wait_for_file(fname, delete=True, empty=False): if not empty: assert data if delete: - os.remove(fname) + safe_rmpath(fname) return data @@ -685,12 +685,34 @@ def call_until(fun, expr): def safe_rmpath(path): "Convenience function for removing temporary test files or dirs" + def retry_fun(fun): + # On Windows it could happen that the file or directory has + # open handles or references preventing the delete operation + # to succeed immediately, so we retry for a while. See: + # https://bugs.python.org/issue33240 + stop_at = time.time() + 1 + while time.time() < stop_at: + try: + return fun() + except WindowsError as _: + err = _ + if err.errno != errno.ENOENT: + raise + else: + warn("ignoring %s" % (str(err))) + time.sleep(0.01) + raise err + try: st = os.stat(path) if stat.S_ISDIR(st.st_mode): - os.rmdir(path) + fun = functools.partial(shutil.rmtree, path) + else: + fun = functools.partial(os.remove, path) + if POSIX: + fun() else: - os.remove(path) + retry_fun(fun) except OSError as err: if err.errno != errno.ENOENT: raise |