Skip to content

Commit 88d09ee

Browse files
committed
Fix bug in locking mechanism
Should fix astropy#9642
1 parent 918abcd commit 88d09ee

2 files changed

Lines changed: 90 additions & 24 deletions

File tree

‎astropy/utils/data.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,12 +1412,27 @@ def clear_download_cache(hashorurl=None, pkgname='astropy'):
14121412
The package name to use to locate the download cache. i.e. for
14131413
``pkgname='astropy'`` the default cache location is
14141414
``~/.astropy/cache``.
1415-
14161415
"""
1417-
14181416
zapped_cache = False
14191417
try:
1420-
with _cache(pkgname, write=True) as (dldir, url2hash):
1418+
with contextlib.ExitStack() as stack:
1419+
try:
1420+
dldir, url2hash = stack.enter_context(
1421+
_cache(pkgname, write=True))
1422+
except OSError as e:
1423+
# Problem arose when trying to open the cache
1424+
msg = 'Not clearing data cache - cache inaccessible due to '
1425+
estr = '' if len(e.args) < 1 else (': ' + str(e))
1426+
warn(CacheMissingWarning(msg + e.__class__.__name__ + estr))
1427+
return
1428+
except RuntimeError: # Couldn't get lock
1429+
if hashorurl is None:
1430+
# Release lock by blowing away cache
1431+
# Need to get locations
1432+
dldir, _ = _get_download_cache_locs(pkgname)
1433+
else:
1434+
# Can't do specific deletion without the lock
1435+
raise
14211436
if hashorurl is None:
14221437
if os.path.exists(dldir):
14231438
shutil.rmtree(dldir)
@@ -1458,7 +1473,7 @@ def clear_download_cache(hashorurl=None, pkgname='astropy'):
14581473
# It's fine for this to fail.
14591474
return
14601475
else:
1461-
msg = 'Not clearing data cache - cache inaccessible due to '
1476+
msg = 'Not clearing data from cache - problem arose '
14621477
estr = '' if len(e.args) < 1 else (': ' + str(e))
14631478
warn(CacheMissingWarning(msg + e.__class__.__name__ + estr))
14641479
return
@@ -1531,6 +1546,7 @@ def _cache_lock(pkgname, need_write=False):
15311546
lockdir = os.path.join(_get_download_cache_locs(pkgname)[0], 'lock')
15321547
pidfn = os.path.join(lockdir, 'pid')
15331548
assume_cache_readonly = False
1549+
got_lock = False
15341550
try:
15351551
msg = f"Config file requests {conf.download_cache_lock_attempts} tries"
15361552
for waited in _keep_trying(conf.download_cache_lock_attempts):
@@ -1552,7 +1568,7 @@ def _cache_lock(pkgname, need_write=False):
15521568
else:
15531569
break
15541570
else:
1555-
# Got the lock!
1571+
got_lock = True
15561572
# write the pid of this process for informational purposes
15571573
with open(pidfn, 'w') as f:
15581574
f.write(str(os.getpid()))
@@ -1573,7 +1589,8 @@ def _cache_lock(pkgname, need_write=False):
15731589
yield
15741590

15751591
finally:
1576-
if not assume_cache_readonly and os.path.exists(lockdir):
1592+
# clear_download_cache might have deleted the lockdir
1593+
if got_lock and os.path.exists(lockdir):
15771594
if os.path.isdir(lockdir):
15781595
# if the pid file is present, be sure to remove it
15791596
if os.path.exists(pidfn):

‎astropy/utils/tests/test_data.py‎

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import py.path
2727
import pytest
28+
from _pytest.monkeypatch import MonkeyPatch
2829

2930
from astropy.utils import data
3031
from astropy.config import paths
@@ -34,6 +35,7 @@
3435
CacheDamaged,
3536
conf,
3637
_cache,
38+
_cache_lock,
3739
compute_hash,
3840
download_file,
3941
cache_contents,
@@ -190,23 +192,30 @@ def no_mkdir(p):
190192

191193

192194
_shelve_possible_backends = ["dbm.dumb", "dbm.ndbm", "dbm.gnu"]
195+
for n in _shelve_possible_backends:
196+
if n not in dbm._modules:
197+
try:
198+
dbm._modules[n] = __import__(n, fromlist=['open'])
199+
except ImportError:
200+
pass
193201

194202

195203
@contextlib.contextmanager
196204
def shelve_backend(name):
197205
"""Ensure that shelve has access only to one backend."""
198-
defaultmod = dbm._defaultmod
199-
modules = dbm._modules
200-
try:
206+
# WARNING: there is something weird and fragile about this.
207+
# Using it can cause weird order-dependent test failures in
208+
# the second shelve_backend test if the two meet certain
209+
# mysterious criteria.
210+
with MonkeyPatch().context() as mp:
201211
try:
202-
dbm._defaultmod = __import__(name, fromlist=['open'])
203-
except ImportError:
204-
pytest.skip(f"module {name} not available")
205-
dbm._modules = {name: dbm._defaultmod}
212+
m = dbm._modules[name]
213+
except KeyError:
214+
pytest.skip(f"Backend {name} not available")
215+
216+
mp.setattr(dbm, "_defaultmod", m)
217+
mp.setattr(dbm, "_modules", {name: m})
206218
yield
207-
finally:
208-
dbm._defaultmod = defaultmod
209-
dbm._modules = modules
210219

211220

212221
@pytest.mark.remote_data(source="astropy")
@@ -286,15 +295,21 @@ def test_download_file_threaded_many(b, temp_cache, valid_urls):
286295
also does this but coverage tools lose track of which paths are explored.
287296
288297
"""
298+
u, c = next(valid_urls)
299+
# Ensure download cache exists in appropriate format
289300
with shelve_backend(b):
290-
urls = list(islice(valid_urls, N_THREAD_HAMMER))
291-
with ThreadPoolExecutor(max_workers=len(urls)) as P:
292-
r = list(P.map(lambda u: download_file(u, cache=True),
293-
[u for (u, c) in urls]))
294-
check_download_cache()
295-
assert len(r) == len(urls)
296-
for r, (u, c) in zip(r, urls):
297-
assert get_file_contents(r) == c
301+
download_file(u, cache=True)
302+
clear_download_cache(u)
303+
304+
# Run test outside of non-thread-safe context manager
305+
urls = list(islice(valid_urls, N_THREAD_HAMMER))
306+
with ThreadPoolExecutor(max_workers=len(urls)) as P:
307+
r = list(P.map(lambda u: download_file(u, cache=True),
308+
[u for (u, c) in urls]))
309+
check_download_cache()
310+
assert len(r) == len(urls)
311+
for r, (u, c) in zip(r, urls):
312+
assert get_file_contents(r) == c
298313

299314

300315
def test_download_file_threaded_many_partial_success(
@@ -1790,9 +1805,16 @@ def test_cache_with_different_shelve_backends(b, temp_cache, valid_urls):
17901805
uc = list(islice(valid_urls, FEW))
17911806
for u, c in uc:
17921807
download_file(u, cache=True)
1808+
assert is_url_in_cache(u)
1809+
1810+
for u, _ in uc:
1811+
assert is_url_in_cache(u)
17931812

17941813
check_download_cache()
17951814

1815+
for u, _ in uc:
1816+
assert is_url_in_cache(u)
1817+
17961818
for u, c in uc:
17971819
assert get_file_contents(
17981820
download_file(u, cache=True, sources=[])) == c
@@ -1837,3 +1859,30 @@ def test_wrong_backend_reports_useful_error(b1b2, temp_cache, valid_urls):
18371859
assert b1 in str(e.value)
18381860
with pytest.raises(CacheDamaged):
18391861
check_download_cache()
1862+
clear_download_cache()
1863+
clear_download_cache()
1864+
1865+
1866+
# What happens when the lock can't be obtained in a timely manner?
1867+
def test_lock_unavailable_raises_runtimeerror_and_clear_frees_lock(
1868+
temp_cache, valid_urls):
1869+
u, c = next(valid_urls)
1870+
download_file(u, cache=True)
1871+
with _cache_lock("astropy", need_write=True):
1872+
with conf.set_temp("download_cache_lock_attempts", 0):
1873+
with pytest.raises(RuntimeError):
1874+
with _cache_lock("astropy", need_write=True):
1875+
pass
1876+
# Ensure that the cache doesn't accidentally get unlocked!
1877+
with pytest.raises(RuntimeError):
1878+
with _cache_lock("astropy", need_write=True):
1879+
pass
1880+
# Download
1881+
with pytest.raises(RuntimeError):
1882+
is_url_in_cache(u), "if locked, act like cache empty"
1883+
with pytest.raises(RuntimeError):
1884+
download_file(u, cache=True, sources=[])
1885+
with pytest.raises(RuntimeError):
1886+
download_file(u, cache=True)
1887+
clear_download_cache() # breaks lock
1888+
is_url_in_cache(u)

0 commit comments

Comments
 (0)